List of usage examples for java.util List size
int size();
From source file:com.thesmartweb.swebrank.Main.java
/** * @param args the command line arguments *///w w w .j a v a 2 s . c o m public static void main(String[] args) { Path input_path = Paths.get("//mnt//var//DBs//inputsL10//nba//");//input directory String output_parent_directory = "//mnt//var//DBs//outputsConfL10//nba//";//output directory String config_path = "//mnt//var//DBs//config//";//input directory //---Disable apache log manually---- //System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog"); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger"); //--------------Domain that is searched---------- String domain = ""; //------------------search engine related options---------------------- List<String> queries = null; int results_number = 0;//the number of results that are returned from each search engine List<Boolean> enginechoice = null; //list element #0. True/False Bing //list element #1. True/False Google //list element #2. True/False Yahoo! //list element #3. True/False Merged //-----------Moz options--------------------- List<Boolean> mozMetrics = null; //The list is going to contain the moz related input in the following order //list element #1. True/False, True we use Moz API, false not //list element #2. True if we use Domain Authority //list element #3. True if we use External MozRank //list element #4. True if we use MozRank //list element #5. True if we use MozTrust //list element #6. True if we use Subdomain MozRank //list element #7. True if we use Page Authority //only one is used (the first to be set to true) boolean moz_threshold_option = false;//set to true we use the threshold Double moz_threshold = 0.0;//if we want to have a threshold in moz int top_count_moz = 0;//if we want to get the moz top-something results //---------------Semantic Analysis method---------------- List<Boolean> ContentSemantics = null; int SensebotConcepts = 0;//define the amount of concepts that sensebot is going to recognize List<Double> SWebRankSettings = null; //------(string)directory is going to be used later----- String output_child_directory; //-------we get all the paths of the txt (input) files from the input directory------- DataManipulation getfiles = new DataManipulation();//class responsible for the extraction of paths Collection<File> inputs_files;//array to include the paths of the txt files inputs_files = getfiles.getinputfiles(input_path.toString(), "txt");//method to retrieve all the path of the input documents //------------read the txt files------------ for (File input : inputs_files) { ReadInput ri = new ReadInput();//function to read the input boolean check_reading_input = ri.perform(input); if (check_reading_input) { domain = ri.domain; //---------- queries = ri.queries; results_number = ri.results_number; enginechoice = ri.enginechoice; //------------ mozMetrics = ri.mozMetrics; moz_threshold_option = ri.moz_threshold_option; moz_threshold = ri.moz_threshold.doubleValue(); //--------------- ContentSemantics = ri.ContentSemantics; SWebRankSettings = ri.SWebRankSettings; } int top_visible = 0;//option to set the amount of results you can get in the merged search engine //------if we choose to use a Moz metric or Visibility score for our ranking, we need to set the results_number for the search engines to its max which is 50 //-----we set the top results number for moz or Visibility rank---- if (mozMetrics.get(0) || enginechoice.get(3)) { if (mozMetrics.get(0)) { top_count_moz = results_number; } //if moz is true, top_count_moz gets the value of result number if (enginechoice.get(3)) { top_visible = results_number; } //if merged engine is true, top_visible gets the value of result number results_number = 50;//this is the max amount of results that you can get from the search engine APIs } //-----if we want to use Moz we should check first if it works if (mozMetrics.get(0)) { Moz Moz = new Moz(); //---if it works, moz remains true, otherwise it is set to false mozMetrics.add(0, Moz.check(config_path)); //if it is false and we have chosen to use Visibility score with Moz, we reset back to the standard settings (ranking and not merged) //therefore, we reset the number of results from 50 to the top_count_moz which contained the original number of results if (!mozMetrics.get(0)) { if (!enginechoice.get(3)) { results_number = top_count_moz; } } } //----------we set the wordLists that we are going to use--------------------- List<String> finalList = new ArrayList<String>();//finalList is going to contain all the content in the end Total_analysis ta = new Total_analysis();//we call total analysis int iteration_counter = 0;//the iteration_counter is used in order to count the number of iterations of the algorithm and to be checked with perf_limit //this list of arraylists is going to contain all the wordLists that are produced for every term of the String[] query, //in order to calculate the NGD scores between every term of the wordList and the term that was used as query in order to produce the spesific wordList List<ArrayList<String>> array_wordLists = new ArrayList<>(); List<String> wordList_previous = new ArrayList<>(); List<String> wordList_new = new ArrayList<>(); double convergence = 0;//we create the convergence percentage and initialize it String conv_percentages = "";//string that contains all the convergence percentages DataManipulation wordsmanipulation = new DataManipulation();//method to manipulate various word data (String, list<String>, etc) do { //if we run the algorithm for the 1st time we already have the query so we skip the loop below that produces the new array of query if (iteration_counter != 0) { wordList_previous = wordList_new; //we add the previous wordList to the finalList finalList = wordsmanipulation.AddAList(wordList_previous, finalList); List<String> query_new_list_total = new ArrayList<>(); int iteration_previous = iteration_counter - 1; Combinations_Engine cn = new Combinations_Engine();//call the class to combine the terms produced for (String query : queries) { List<String> ids = new ArrayList<>(); if (enginechoice.get(0)) { String id = domain + "/" + query + "/bing" + "/" + iteration_previous; ids.add(id); } if (enginechoice.get(1)) { String id = domain + "/" + query + "/google" + "/" + iteration_previous; ids.add(id); } if (enginechoice.get(2)) { String id = domain + "/" + query + "/yahoo" + "/" + iteration_previous; ids.add(id); } ElasticGetWordList ESget = new ElasticGetWordList();//we call this class to get the wordlist from the Elastic Search List<String> maxWords = ESget.getMaxWords(ids, SWebRankSettings.get(9).intValue(), config_path);//we are going to get a max amount of words int query_index = queries.indexOf(query); int size_query_new = SWebRankSettings.get(10).intValue();//the amount of new queries we are willing to create //we create the new queries for every query of the previous round by combining the words produced from this query List<String> query_new_list = cn.perform(maxWords, SWebRankSettings.get(7), queries, SWebRankSettings.get(6), query_index, size_query_new, config_path); //we add the list of new queries to the total list that containas all the new queries query_new_list_total.addAll(query_new_list); System.out.println("query pointer=" + query_index + ""); } //---------------------the following cleans a list from null and duplicates query_new_list_total = wordsmanipulation.clearListString(query_new_list_total); //--------------we create the new directory that our files are going to be saved String txt_directory = FilenameUtils.getBaseName(input.getName()); output_child_directory = output_parent_directory + txt_directory + "_level_" + iteration_counter + "//"; //----------------append the wordlist to a file------------------ wordsmanipulation.AppendWordList(query_new_list_total, output_child_directory + "queries_" + iteration_counter + ".txt"); if (query_new_list_total.size() < 1) { break; } //if we don't create new queries we end the while loop //total analysis' function is going to do all the work and return back what we need ta = new Total_analysis(); ta.perform(wordList_previous, iteration_counter, output_child_directory, domain, enginechoice, query_new_list_total, results_number, top_visible, mozMetrics, moz_threshold_option, moz_threshold.doubleValue(), top_count_moz, ContentSemantics, SensebotConcepts, SWebRankSettings, config_path); //we get the array of wordlists array_wordLists = ta.getarray_wordLists(); //get the wordlist that includes all the new queries wordList_new = ta.getwordList_total(); //---------------------the following cleans a list from null and duplicates------------- wordList_new = wordsmanipulation.clearListString(wordList_new); //----------------append the wordlist to a file-------------------- wordsmanipulation.AppendWordList(wordList_new, output_child_directory + "wordList.txt"); //the concergence percentage of this iteration convergence = ta.getConvergence();//we are going to use convergence score to check the convergence //a string that contains all the convergence percentage for each round separated by \n character conv_percentages = conv_percentages + "\n" + convergence; //a file that is going to include the convergence percentages wordsmanipulation.AppendString(conv_percentages, output_child_directory + "convergence_percentage.txt"); //we add the new wordList to the finalList finalList = wordsmanipulation.AddAList(wordList_new, finalList); //we set the query array to be equal to the query new total that we have created queries = query_new_list_total; //we increment the iteration_counter in order to count the iterations of the algorithm and to use the perf_limit iteration_counter++; } else {//the following source code is performed on the 1st run of the loop //------------we extract the parent path of the file String txt_directory = FilenameUtils.getBaseName(input.getName()); //----------we create a string that is going to be used for the corresponding directory of outputs output_child_directory = output_parent_directory + txt_directory + "_level_" + iteration_counter + "//"; //we call total analysis function performOld ta.perform(wordList_new, iteration_counter, output_child_directory, domain, enginechoice, queries, results_number, top_visible, mozMetrics, moz_threshold_option, moz_threshold.doubleValue(), top_count_moz, ContentSemantics, SensebotConcepts, SWebRankSettings, config_path); //we get the array of wordlists array_wordLists = ta.getarray_wordLists(); //get the wordlist that includes all the new queries wordList_new = ta.getwordList_total(); //---------------------the following cleans a list from null and duplicates wordList_new = wordsmanipulation.clearListString(wordList_new); //----------------append the wordlist to a file wordsmanipulation.AppendWordList(wordList_new, output_child_directory + "wordList.txt"); //----------------------------------------- iteration_counter++;//increase the iteration_counter that counts the iterations of the algorithm } } while (convergence < SWebRankSettings.get(5).doubleValue() && iteration_counter < SWebRankSettings.get(8).intValue());//while the convergence percentage is below the limit and the iteration_counter below the performance limit if (iteration_counter == 1) { finalList = wordsmanipulation.AddAList(wordList_new, finalList); } //--------------------content List---------------- if (!finalList.isEmpty()) { //---------------------the following cleans the final list from null and duplicates finalList = wordsmanipulation.clearListString(finalList); //write the keywords to a file boolean flag_file = false;//boolean flag to declare successful write to file flag_file = wordsmanipulation.AppendWordList(finalList, output_parent_directory + "total_content.txt"); if (!flag_file) { System.out.print("can not create the content file for: " + output_parent_directory + "total_content.txt"); } } //we are going to save the total content with its convergence on the ElasticSearch cluster in a separated index //Node node = nodeBuilder().client(true).clusterName("lshrankldacluster").node(); //Client client = node.client(); //get the elastic search indexes in a list List<String> elasticIndexes = ri.GetKeyFile(config_path, "elasticSearchIndexes"); Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "lshrankldacluster") .build(); Client client = new TransportClient(settings) .addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); JSONObject objEngineLevel = new JSONObject(); objEngineLevel.put("TotalContent", finalList);//we save the total content objEngineLevel.put("Convergences", conv_percentages);//we save the convergence percentages IndexRequest indexReq = new IndexRequest(elasticIndexes.get(0), "content", domain);//we save also the domain indexReq.source(objEngineLevel); IndexResponse indexRes = client.index(indexReq).actionGet(); //node.close(); client.close(); //----------------------convergence percentages writing to file--------------- //use the conv_percentages string if (conv_percentages.length() != 0) { boolean flag_file = false;//boolean flag to declare successful write to file flag_file = wordsmanipulation.AppendString(conv_percentages, output_parent_directory + "convergence_percentages.txt"); if (!flag_file) { System.out.print("can not create the convergence file for: " + output_parent_directory + "convergence_percentages.txt"); } } } }
From source file:acmi.l2.clientmod.l2_version_switcher.Main.java
public static void main(String[] args) { if (args.length != 3 && args.length != 4) { System.out.println("USAGE: l2_version_switcher.jar host game version <--splash> <filter>"); System.out.println("EXAMPLE: l2_version_switcher.jar " + L2.NCWEST_HOST + " " + L2.NCWEST_GAME + " 1 \"system\\*\""); System.out.println(/*from www. j a v a 2 s .co m*/ " l2_version_switcher.jar " + L2.PLAYNC_TEST_HOST + " " + L2.PLAYNC_TEST_GAME + " 48"); System.exit(0); } List<String> argsList = new ArrayList<>(Arrays.asList(args)); String host = argsList.get(0); String game = argsList.get(1); int version = Integer.parseInt(argsList.get(2)); Helper helper = new Helper(host, game, version); boolean available = false; try { available = helper.isAvailable(); } catch (IOException e) { System.err.print(e.getClass().getSimpleName()); if (e.getMessage() != null) { System.err.print(": " + e.getMessage()); } System.err.println(); } System.out.println(String.format("Version %d available: %b", version, available)); if (!available) { System.exit(0); } List<FileInfo> fileInfoList = null; try { fileInfoList = helper.getFileInfoList(); } catch (IOException e) { System.err.println("Couldn\'t get file info map"); System.exit(1); } boolean splash = argsList.remove("--splash"); if (splash) { Optional<FileInfo> splashObj = fileInfoList.stream() .filter(fi -> fi.getPath().contains("sp_32b_01.bmp")).findAny(); if (splashObj.isPresent()) { try (InputStream is = new FilterInputStream( Util.getUnzipStream(helper.getDownloadStream(splashObj.get().getPath()))) { @Override public int read() throws IOException { int b = super.read(); if (b >= 0) b ^= 0x36; return b; } @Override public int read(byte[] b, int off, int len) throws IOException { int r = super.read(b, off, len); if (r >= 0) { for (int i = 0; i < r; i++) b[off + i] ^= 0x36; } return r; } }) { new DataInputStream(is).readFully(new byte[28]); BufferedImage bi = ImageIO.read(is); JFrame frame = new JFrame("Lineage 2 [" + version + "] " + splashObj.get().getPath()); frame.setContentPane(new JComponent() { { setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight())); } @Override protected void paintComponent(Graphics g) { g.drawImage(bi, 0, 0, null); } }); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Splash not found"); } return; } String filter = argsList.size() > 3 ? separatorsToSystem(argsList.get(3)) : null; File l2Folder = new File(System.getProperty("user.dir")); List<FileInfo> toUpdate = fileInfoList.parallelStream().filter(fi -> { String filePath = separatorsToSystem(fi.getPath()); if (filter != null && !wildcardMatch(filePath, filter, IOCase.INSENSITIVE)) return false; File file = new File(l2Folder, filePath); try { if (file.exists() && file.length() == fi.getSize() && Util.hashEquals(file, fi.getHash())) { System.out.println(filePath + ": OK"); return false; } } catch (IOException e) { System.out.println(filePath + ": couldn't check hash: " + e); return true; } System.out.println(filePath + ": need update"); return true; }).collect(Collectors.toList()); List<String> errors = Collections.synchronizedList(new ArrayList<>()); ExecutorService executor = Executors.newFixedThreadPool(16); CompletableFuture[] tasks = toUpdate.stream().map(fi -> CompletableFuture.runAsync(() -> { String filePath = separatorsToSystem(fi.getPath()); File file = new File(l2Folder, filePath); File folder = file.getParentFile(); if (!folder.exists()) { if (!folder.mkdirs()) { errors.add(filePath + ": couldn't create parent dir"); return; } } try (InputStream input = Util .getUnzipStream(new BufferedInputStream(helper.getDownloadStream(fi.getPath()))); OutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { byte[] buffer = new byte[Math.min(fi.getSize(), 1 << 24)]; int pos = 0; int r; while ((r = input.read(buffer, pos, buffer.length - pos)) >= 0) { pos += r; if (pos == buffer.length) { output.write(buffer, 0, pos); pos = 0; } } if (pos != 0) { output.write(buffer, 0, pos); } System.out.println(filePath + ": OK"); } catch (IOException e) { String msg = filePath + ": FAIL: " + e.getClass().getSimpleName(); if (e.getMessage() != null) { msg += ": " + e.getMessage(); } errors.add(msg); } }, executor)).toArray(CompletableFuture[]::new); CompletableFuture.allOf(tasks).thenRun(() -> { for (String err : errors) System.err.println(err); executor.shutdown(); }); }
From source file:edu.msu.cme.rdp.kmer.KmerFilter.java
public static void main(String[] args) throws Exception { final KmerTrie kmerTrie; final SeqReader queryReader; final SequenceType querySeqType; final File queryFile; final KmerStartsWriter out; final boolean translQuery; final int wordSize; final int translTable; final boolean alignedSeqs; final List<String> refLabels = new ArrayList(); final int maxThreads; try {// w ww .j av a 2 s .co m CommandLine cmdLine = new PosixParser().parse(options, args); args = cmdLine.getArgs(); if (args.length < 3) { throw new Exception("Unexpected number of arguments"); } if (cmdLine.hasOption("out")) { out = new KmerStartsWriter(cmdLine.getOptionValue("out")); } else { out = new KmerStartsWriter(System.out); } if (cmdLine.hasOption("aligned")) { alignedSeqs = true; } else { alignedSeqs = false; } if (cmdLine.hasOption("transl-table")) { translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table")); } else { translTable = 11; } if (cmdLine.hasOption("threads")) { maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads")); } else { maxThreads = Runtime.getRuntime().availableProcessors(); } queryFile = new File(args[1]); wordSize = Integer.valueOf(args[0]); SequenceType refSeqType = null; querySeqType = SeqUtils.guessSequenceType(queryFile); queryReader = new SequenceReader(queryFile); if (querySeqType == SequenceType.Protein) { throw new Exception("Expected nucl query sequences"); } refSeqType = SeqUtils .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2])); translQuery = refSeqType == SequenceType.Protein; if (translQuery && wordSize % 3 != 0) { throw new Exception("Word size must be a multiple of 3 for nucl ref seqs"); } int trieWordSize; if (translQuery) { trieWordSize = wordSize / 3; } else { trieWordSize = wordSize; } kmerTrie = new KmerTrie(trieWordSize, translQuery); for (int index = 2; index < args.length; index++) { String refName; String refFileName = args[index]; if (refFileName.contains("=")) { String[] lexemes = refFileName.split("="); refName = lexemes[0]; refFileName = lexemes[1]; } else { String tmpName = new File(refFileName).getName(); if (tmpName.contains(".")) { refName = tmpName.substring(0, tmpName.lastIndexOf(".")); } else { refName = tmpName; } } File refFile = new File(refFileName); if (refSeqType != SeqUtils.guessSequenceType(refFile)) { throw new Exception( "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile) + " sequences but expected " + refSeqType + " sequences"); } SequenceReader seqReader = new SequenceReader(refFile); Sequence seq; while ((seq = seqReader.readNextSequence()) != null) { if (seq.getSeqName().startsWith("#")) { continue; } if (alignedSeqs) { kmerTrie.addModelSequence(seq, refLabels.size()); } else { kmerTrie.addSequence(seq, refLabels.size()); } } seqReader.close(); refLabels.add(refName); } } catch (Exception e) { new HelpFormatter().printHelp("KmerSearch <word_size> <query_file> [name=]<ref_file> ...", options); System.err.println(e.getMessage()); e.printStackTrace(); System.exit(1); throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables } long startTime = System.currentTimeMillis(); long seqCount = 0; final int maxTasks = 25000; /* * if (args.length == 4) { maxThreads = Integer.valueOf(args[3]); } else { */ //} System.err.println("Starting kmer mapping at " + new Date()); System.err.println("* Number of threads: " + maxThreads); System.err.println("* References: " + refLabels); System.err.println("* Reads file: " + queryFile); System.err.println("* Kmer length: " + kmerTrie.getWordSize()); final AtomicInteger processed = new AtomicInteger(); final AtomicInteger outstandingTasks = new AtomicInteger(); ExecutorService service = Executors.newFixedThreadPool(maxThreads); Sequence querySeq; while ((querySeq = queryReader.readNextSequence()) != null) { seqCount++; String seqString = querySeq.getSeqString(); if (seqString.length() < 3) { System.err.println("Sequence " + querySeq.getSeqName() + "'s length is less than 3"); continue; } final Sequence threadSeq = querySeq; Runnable r = new Runnable() { public void run() { try { processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, false); processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, true); } catch (IOException e) { throw new RuntimeException(e); } processed.incrementAndGet(); outstandingTasks.decrementAndGet(); } }; outstandingTasks.incrementAndGet(); service.submit(r); while (outstandingTasks.get() >= maxTasks) ; if ((processed.get() + 1) % 1000000 == 0) { System.err.println("Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); } } service.shutdown(); service.awaitTermination(1, TimeUnit.DAYS); System.err.println("Finished Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); out.close(); }
From source file:com.github.s4ke.moar.cli.Main.java
public static void main(String[] args) throws ParseException, IOException { // create Options object Options options = new Options(); options.addOption("rf", true, "file containing the regexes to test against (multiple regexes are separated by one empty line)"); options.addOption("r", true, "regex to test against"); options.addOption("mf", true, "file/folder to read the MOA from"); options.addOption("mo", true, "folder to export the MOAs to (overwrites if existent)"); options.addOption("sf", true, "file to read the input string(s) from"); options.addOption("s", true, "string to test the MOA/Regex against"); options.addOption("m", false, "multiline matching mode (search in string for regex)"); options.addOption("ls", false, "treat every line of the input string file as one string"); options.addOption("t", false, "trim lines if -ls is set"); options.addOption("d", false, "only do determinism check"); options.addOption("help", false, "prints this dialog"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); if (args.length == 0 || cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("moar-cli", options); return;//w ww . jav a 2s .c om } List<String> patternNames = new ArrayList<>(); List<MoaPattern> patterns = new ArrayList<>(); List<String> stringsToCheck = new ArrayList<>(); if (cmd.hasOption("r")) { String regexStr = cmd.getOptionValue("r"); try { patterns.add(MoaPattern.compile(regexStr)); patternNames.add(regexStr); } catch (Exception e) { System.out.println(e.getMessage()); } } if (cmd.hasOption("rf")) { String fileName = cmd.getOptionValue("rf"); List<String> regexFileContents = readFileContents(new File(fileName)); int emptyLineCountAfterRegex = 0; StringBuilder regexStr = new StringBuilder(); for (String line : regexFileContents) { if (emptyLineCountAfterRegex >= 1) { if (regexStr.length() > 0) { patterns.add(MoaPattern.compile(regexStr.toString())); patternNames.add(regexStr.toString()); } regexStr.setLength(0); emptyLineCountAfterRegex = 0; } if (line.trim().equals("")) { if (regexStr.length() > 0) { ++emptyLineCountAfterRegex; } } else { regexStr.append(line); } } if (regexStr.length() > 0) { try { patterns.add(MoaPattern.compile(regexStr.toString())); patternNames.add(regexStr.toString()); } catch (Exception e) { System.out.println(e.getMessage()); return; } regexStr.setLength(0); } } if (cmd.hasOption("mf")) { String fileName = cmd.getOptionValue("mf"); File file = new File(fileName); if (file.isDirectory()) { System.out.println(fileName + " is a directory, using all *.moar files as patterns"); File[] moarFiles = file.listFiles(pathname -> pathname.getName().endsWith(".moar")); for (File moar : moarFiles) { String jsonString = readWholeFile(moar); patterns.add(MoarJSONSerializer.fromJSON(jsonString)); patternNames.add(moar.getAbsolutePath()); } } else { System.out.println(fileName + " is a single file. using it directly (no check for *.moar suffix)"); String jsonString = readWholeFile(file); patterns.add(MoarJSONSerializer.fromJSON(jsonString)); patternNames.add(fileName); } } if (cmd.hasOption("s")) { String str = cmd.getOptionValue("s"); stringsToCheck.add(str); } if (cmd.hasOption("sf")) { boolean treatLineAsString = cmd.hasOption("ls"); boolean trim = cmd.hasOption("t"); String fileName = cmd.getOptionValue("sf"); StringBuilder stringBuilder = new StringBuilder(); boolean firstLine = true; for (String str : readFileContents(new File(fileName))) { if (treatLineAsString) { if (trim) { str = str.trim(); if (str.length() == 0) { continue; } } stringsToCheck.add(str); } else { if (!firstLine) { stringBuilder.append("\n"); } if (firstLine) { firstLine = false; } stringBuilder.append(str); } } if (!treatLineAsString) { stringsToCheck.add(stringBuilder.toString()); } } if (cmd.hasOption("d")) { //at this point we have already built the Patterns //so just give the user a short note. System.out.println("All Regexes seem to be deterministic."); return; } if (patterns.size() == 0) { System.out.println("no patterns to check"); return; } if (cmd.hasOption("mo")) { String folder = cmd.getOptionValue("mo"); File folderFile = new File(folder); if (!folderFile.exists()) { System.out.println(folder + " does not exist. creating..."); if (!folderFile.mkdirs()) { System.out.println("folder " + folder + " could not be created"); } } int cnt = 0; for (MoaPattern pattern : patterns) { String patternAsJSON = MoarJSONSerializer.toJSON(pattern); try (BufferedWriter writer = new BufferedWriter( new FileWriter(new File(folderFile, "pattern" + ++cnt + ".moar")))) { writer.write(patternAsJSON); } } System.out.println("stored " + cnt + " patterns in " + folder); } if (stringsToCheck.size() == 0) { System.out.println("no strings to check"); return; } boolean multiline = cmd.hasOption("m"); for (String string : stringsToCheck) { int curPattern = 0; for (MoaPattern pattern : patterns) { MoaMatcher matcher = pattern.matcher(string); if (!multiline) { if (matcher.matches()) { System.out.println("\"" + patternNames.get(curPattern) + "\" matches \"" + string + "\""); } else { System.out.println( "\"" + patternNames.get(curPattern) + "\" does not match \"" + string + "\""); } } else { StringBuilder buffer = new StringBuilder(string); int additionalCharsPerMatch = ("<match>" + "</match>").length(); int matchCount = 0; while (matcher.nextMatch()) { buffer.replace(matcher.getStart() + matchCount * additionalCharsPerMatch, matcher.getEnd() + matchCount * additionalCharsPerMatch, "<match>" + string.substring(matcher.getStart(), matcher.getEnd()) + "</match>"); ++matchCount; } System.out.println(buffer.toString()); } } ++curPattern; } }
From source file:edu.msu.cme.rdp.kmer.cli.FastKmerFilter.java
public static void main(String[] args) throws Exception { final KmerSet<Set<RefKmer>> kmerSet; final SeqReader queryReader; final SequenceType querySeqType; final File queryFile; final KmerStartsWriter out; final boolean translQuery; final int wordSize; final int translTable; final boolean alignedSeqs; final List<String> refLabels = new ArrayList(); final int maxThreads; final int trieWordSize; try {/*from w w w. j av a 2 s . com*/ CommandLine cmdLine = new PosixParser().parse(options, args); args = cmdLine.getArgs(); if (args.length < 3) { throw new Exception("Unexpected number of arguments"); } if (cmdLine.hasOption("out")) { out = new KmerStartsWriter(cmdLine.getOptionValue("out")); } else { out = new KmerStartsWriter(System.out); } if (cmdLine.hasOption("aligned")) { alignedSeqs = true; } else { alignedSeqs = false; } if (cmdLine.hasOption("transl-table")) { translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table")); } else { translTable = 11; } if (cmdLine.hasOption("threads")) { maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads")); } else { maxThreads = Runtime.getRuntime().availableProcessors(); } queryFile = new File(args[1]); wordSize = Integer.valueOf(args[0]); SequenceType refSeqType = null; querySeqType = SeqUtils.guessSequenceType(queryFile); queryReader = new SequenceReader(queryFile); if (querySeqType == SequenceType.Protein) { throw new Exception("Expected nucl query sequences"); } refSeqType = SeqUtils .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2])); translQuery = refSeqType == SequenceType.Protein; if (translQuery && wordSize % 3 != 0) { throw new Exception("Word size must be a multiple of 3 for nucl ref seqs"); } if (translQuery) { trieWordSize = wordSize / 3; } else { trieWordSize = wordSize; } kmerSet = new KmerSet<Set<RefKmer>>();//new KmerTrie(trieWordSize, translQuery); for (int index = 2; index < args.length; index++) { String refName; String refFileName = args[index]; if (refFileName.contains("=")) { String[] lexemes = refFileName.split("="); refName = lexemes[0]; refFileName = lexemes[1]; } else { String tmpName = new File(refFileName).getName(); if (tmpName.contains(".")) { refName = tmpName.substring(0, tmpName.lastIndexOf(".")); } else { refName = tmpName; } } File refFile = new File(refFileName); if (refSeqType != SeqUtils.guessSequenceType(refFile)) { throw new Exception( "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile) + " sequences but expected " + refSeqType + " sequences"); } SequenceReader seqReader = new SequenceReader(refFile); Sequence seq; while ((seq = seqReader.readNextSequence()) != null) { if (seq.getSeqName().startsWith("#")) { continue; } KmerGenerator kmers; try { if (translQuery) { //protein ref kmers = new ProtKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs); } else { kmers = new NuclKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs); } while (kmers.hasNext()) { Kmer temp = kmers.next(); long[] next = temp.getLongKmers(); Set<RefKmer> refKmers = kmerSet.get(next); if (refKmers == null) { refKmers = new HashSet(); kmerSet.add(next, refKmers); } RefKmer kmerRef = new RefKmer(); kmerRef.modelPos = kmers.getPosition(); kmerRef.refFileIndex = refLabels.size(); kmerRef.refSeqid = seq.getSeqName(); refKmers.add(kmerRef); } } catch (IllegalArgumentException ex) { //System.err.println(seq.getSeqName()+ " " + ex.getMessage()); } } seqReader.close(); refLabels.add(refName); } } catch (Exception e) { new HelpFormatter().printHelp( "KmerSearch <kmerSize> <query_file> [name=]<ref_file> ...\nkmerSize should be multiple of 3, (recommend 45, minimum 30, maximum 63) ", options); e.printStackTrace(); System.exit(1); throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables } long startTime = System.currentTimeMillis(); long seqCount = 0; final int maxTasks = 25000; System.err.println("Starting kmer mapping at " + new Date()); System.err.println("* Number of threads: " + maxThreads); System.err.println("* References: " + refLabels); System.err.println("* Reads file: " + queryFile); System.err.println("* Kmer length: " + trieWordSize); System.err.println("* Kmer Refset Size: " + kmerSet.size()); final AtomicInteger processed = new AtomicInteger(); final AtomicInteger outstandingTasks = new AtomicInteger(); ExecutorService service = Executors.newFixedThreadPool(maxThreads); Sequence querySeq; while ((querySeq = queryReader.readNextSequence()) != null) { seqCount++; String seqString = querySeq.getSeqString(); if ((!translQuery && seqString.length() < wordSize) || (translQuery && seqString.length() < wordSize + 2)) { //System.err.println(querySeq.getSeqName() + "\t" + seqString.length()); continue; } final Sequence threadSeq = querySeq; Runnable r = new Runnable() { public void run() { try { processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, false); processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, true); processed.incrementAndGet(); outstandingTasks.decrementAndGet(); } catch (Exception e) { e.printStackTrace(); } } }; outstandingTasks.incrementAndGet(); service.submit(r); while (outstandingTasks.get() >= maxTasks) ; if ((processed.get() + 1) % 1000000 == 0) { System.err.println("Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); } } service.shutdown(); service.awaitTermination(1, TimeUnit.DAYS); System.err.println("Finished Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); out.close(); }
From source file:com.github.wshackle.java4cpp.J4CppMain.java
public static void main(String[] args) throws IOException, ClassNotFoundException { main_completed = false;/* w ww.j ava 2 s . co m*/ Options options = new Options(); options.addOption(Option.builder("?").desc("Print this message").longOpt("help").build()); options.addOption(Option.builder("n").hasArg().desc("C++ namespace for newly generated classes.") .longOpt("namespace").build()); options.addOption( Option.builder("c").hasArgs().desc("Single Java class to extract.").longOpt("classes").build()); options.addOption( Option.builder("p").hasArgs().desc("Java Package prefix to extract").longOpt("packages").build()); options.addOption(Option.builder("o").hasArg().desc("Output C++ source file.").longOpt("output").build()); options.addOption(Option.builder("j").hasArg().desc("Input jar file").longOpt("jar").build()); options.addOption(Option.builder("h").hasArg().desc("Output C++ header file.").longOpt("header").build()); options.addOption(Option.builder("l").hasArg() .desc("Maximum limit on classes to extract from jars.[default=200]").longOpt("limit").build()); options.addOption(Option.builder("v").desc("enable verbose output").longOpt("verbose").build()); options.addOption(Option.builder().hasArg().desc("Classes per output file.[default=10]") .longOpt(CLASSESPEROUTPUT).build()); options.addOption(Option.builder().hasArgs().desc( "Comma seperated list of nativeclass=javaclass native where nativeclass will be generated as an extension/implementation of the java class.") .longOpt("natives").build()); options.addOption(Option.builder().hasArg() .desc("library name for System.loadLibrary(...) for native extension classes") .longOpt("loadlibname").build()); String output = null; String header = null; String jar = null; Set<String> classnamesToFind = null; Set<String> packageprefixes = null; String loadlibname = null; Map<String, String> nativesNameMap = null; Map<String, Class> nativesClassMap = null; int limit = DEFAULT_LIMIT; int classes_per_file = 10; List<Class> classes = new ArrayList<>(); String limitstring = Integer.toString(limit); try { // parse the command line arguments System.out.println("args = " + Arrays.toString(args)); CommandLine line = new DefaultParser().parse(options, args); loadlibname = line.getOptionValue("loadlibname"); verbose = line.hasOption("verbose"); if (line.hasOption(CLASSESPEROUTPUT)) { String cpoStr = line.getOptionValue(CLASSESPEROUTPUT); try { int cpoI = Integer.valueOf(cpoStr); classes_per_file = cpoI; } catch (Exception e) { System.err.println("Option for " + CLASSESPEROUTPUT + "=\"" + cpoStr + "\""); printHelpAndExit(options, args); } } if (line.hasOption("natives")) { String natives[] = line.getOptionValues("natives"); if (verbose) { System.out.println("natives = " + Arrays.toString(natives)); } nativesNameMap = new HashMap<>(); nativesClassMap = new HashMap<>(); for (int i = 0; i < natives.length; i++) { int eq_index = natives[i].indexOf('='); String nativeClassName = natives[i].substring(0, eq_index).trim(); String javaClassName = natives[i].substring(eq_index + 1).trim(); Class javaClass = null; try { javaClass = Class.forName(javaClassName); } catch (ClassNotFoundException e) { //e.printStackTrace(); System.err.println("Class for " + javaClassName + " not found. (It may be found later in jar if specified.)"); } nativesNameMap.put(javaClassName, nativeClassName); if (javaClass != null) { nativesClassMap.put(nativeClassName, javaClass); if (!classes.contains(javaClass)) { classes.add(javaClass); } } } } // // validate that block-size has been set // if (line.hasOption("block-size")) { // // print the value of block-size // if(verbose) System.out.println(line.getOptionValue("block-size")); // } jar = line.getOptionValue("jar", jar); if (verbose) { System.out.println("jar = " + jar); } if (null != jar) { if (jar.startsWith("~/")) { jar = new File(new File(getHomeDir()), jar.substring(2)).getCanonicalPath(); } if (jar.startsWith("./")) { jar = new File(new File(getCurrentDir()), jar.substring(2)).getCanonicalPath(); } if (jar.startsWith("../")) { jar = new File(new File(getCurrentDir()).getParentFile(), jar.substring(3)).getCanonicalPath(); } } if (line.hasOption("classes")) { classnamesToFind = new HashSet<String>(); String classStrings[] = line.getOptionValues("classes"); if (verbose) { System.out.println("classStrings = " + Arrays.toString(classStrings)); } classnamesToFind.addAll(Arrays.asList(classStrings)); if (verbose) { System.out.println("classnamesToFind = " + classnamesToFind); } } // if (!line.hasOption("namespace")) { // if (classname != null && classname.length() > 0) { // namespace = classname.toLowerCase().replace(".", "_"); // } else if (jar != null && jar.length() > 0) { // int lastSep = jar.lastIndexOf(File.separator); // int start = Math.max(0, lastSep + 1); // int period = jar.indexOf('.', start + 1); // int end = Math.max(start + 1, period); // namespace = jar.substring(start, end).toLowerCase(); // namespace = namespace.replace(" ", "_"); // if (namespace.indexOf("-") > 0) { // namespace = namespace.substring(0, namespace.indexOf("-")); // } // } // } namespace = line.getOptionValue("namespace", namespace); if (verbose) { System.out.println("namespace = " + namespace); } if (null != namespace) { output = namespace + ".cpp"; } output = line.getOptionValue("output", output); if (verbose) { System.out.println("output = " + output); } if (null != output) { if (output.startsWith("~/")) { output = System.getProperty("user.home") + output.substring(1); } header = output.substring(0, output.lastIndexOf('.')) + ".h"; } else { output = "out.cpp"; } header = line.getOptionValue("header", header); if (verbose) { System.out.println("header = " + header); } if (null != header) { if (header.startsWith("~/")) { header = System.getProperty("user.home") + header.substring(1); } } else { header = "out.h"; } if (line.hasOption("packages")) { packageprefixes = new HashSet<String>(); packageprefixes.addAll(Arrays.asList(line.getOptionValues("packages"))); } if (line.hasOption("limit")) { limitstring = line.getOptionValue("limit", Integer.toString(DEFAULT_LIMIT)); limit = Integer.valueOf(limitstring); } if (line.hasOption("help")) { printHelpAndExit(options, args); } } catch (ParseException exp) { if (verbose) { System.out.println("Unexpected exception:" + exp.getMessage()); } printHelpAndExit(options, args); } Set<Class> excludedClasses = new HashSet<>(); Set<String> foundClassNames = new HashSet<>(); excludedClasses.add(Object.class); excludedClasses.add(String.class); excludedClasses.add(void.class); excludedClasses.add(Void.class); excludedClasses.add(Class.class); excludedClasses.add(Enum.class); Set<String> packagesSet = new TreeSet<>(); List<URL> urlsList = new ArrayList<>(); String cp = System.getProperty("java.class.path"); if (verbose) { System.out.println("System.getProperty(\"java.class.path\") = " + cp); } if (null != cp) { for (String cpe : cp.split(File.pathSeparator)) { if (verbose) { System.out.println("class path element = " + cpe); } File f = new File(cpe); if (f.isDirectory()) { urlsList.add(new URL("file:" + f.getCanonicalPath() + File.separator)); } else if (cpe.endsWith(".jar")) { urlsList.add(new URL("jar:file:" + f.getCanonicalPath() + "!/")); } } } cp = System.getenv("CLASSPATH"); if (verbose) { System.out.println("System.getenv(\"CLASSPATH\") = " + cp); } if (null != cp) { for (String cpe : cp.split(File.pathSeparator)) { if (verbose) { System.out.println("class path element = " + cpe); } File f = new File(cpe); if (f.isDirectory()) { urlsList.add(new URL("file:" + f.getCanonicalPath() + File.separator)); } else if (cpe.endsWith(".jar")) { urlsList.add(new URL("jar:file:" + f.getCanonicalPath() + "!/")); } } } if (verbose) { System.out.println("urlsList = " + urlsList); } if (null != jar && jar.length() > 0) { Path jarPath = FileSystems.getDefault().getPath(jar); ZipInputStream zip = new ZipInputStream(Files.newInputStream(jarPath, StandardOpenOption.READ)); URL jarUrl = new URL("jar:file:" + jarPath.toFile().getCanonicalPath() + "!/"); urlsList.add(jarUrl); URL[] urls = urlsList.toArray(new URL[urlsList.size()]); if (verbose) { System.out.println("urls = " + Arrays.toString(urls)); } URLClassLoader cl = URLClassLoader.newInstance(urls); for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { // This ZipEntry represents a class. Now, what class does it represent? String entryName = entry.getName(); if (verbose) { System.out.println("entryName = " + entryName); } if (!entry.isDirectory() && entryName.endsWith(".class")) { if (entryName.indexOf('$') >= 0) { continue; } String classFileName = entry.getName().replace('/', '.'); String className = classFileName.substring(0, classFileName.length() - ".class".length()); if (classnamesToFind != null && classnamesToFind.size() > 0 && !classnamesToFind.contains(className)) { if (verbose) { System.out.println("skipping className=" + className + " because it does not found in=" + classnamesToFind); } continue; } try { Class clss = cl.loadClass(className); if (null != nativesClassMap && null != nativesNameMap && nativesNameMap.containsKey(className)) { nativesClassMap.put(nativesNameMap.get(className), clss); if (!classes.contains(clss)) { classes.add(clss); } } if (packageprefixes != null && packageprefixes.size() > 0) { if (null == clss.getPackage()) { continue; } final String pkgName = clss.getPackage().getName(); boolean matchFound = false; for (String prefix : packageprefixes) { if (pkgName.startsWith(prefix)) { matchFound = true; break; } } if (!matchFound) { continue; } } Package p = clss.getPackage(); if (null != p) { packagesSet.add(clss.getPackage().getName()); } if (!classes.contains(clss) && isAddableClass(clss, excludedClasses)) { if (null != classnamesToFind && classnamesToFind.contains(className) && !foundClassNames.contains(className)) { foundClassNames.add(className); if (verbose) { System.out.println("foundClassNames = " + foundClassNames); } } // if(verbose) System.out.println("clss = " + clss); classes.add(clss); // Class superClass = clss.getSuperclass(); // while (null != superClass // && !classes.contains(superClass) // && isAddableClass(superClass, excludedClasses)) { // classes.add(superClass); // superClass = superClass.getSuperclass(); // } } } catch (ClassNotFoundException | NoClassDefFoundError ex) { System.err.println( "Caught " + ex.getClass().getName() + ":" + ex.getMessage() + " for className=" + className + ", entryName=" + entryName + ", jarPath=" + jarPath); } } } } if (null != classnamesToFind) { if (verbose) { System.out.println("Checking classnames arguments"); } for (String classname : classnamesToFind) { if (verbose) { System.out.println("classname = " + classname); } if (foundClassNames.contains(classname)) { if (verbose) { System.out.println("foundClassNames.contains(" + classname + ")"); } continue; } try { if (classes.contains(Class.forName(classname))) { if (verbose) { System.out.println("Classes list already contains: " + classname); } continue; } } catch (Exception e) { } if (null != classname && classname.length() > 0) { urlsList.add(new URL("file://" + System.getProperty("user.dir") + "/")); URL[] urls = urlsList.toArray(new URL[urlsList.size()]); if (verbose) { System.out.println("urls = " + Arrays.toString(urls)); } URLClassLoader cl = URLClassLoader.newInstance(urls); Class c = null; try { c = cl.loadClass(classname); } catch (ClassNotFoundException e) { System.err.println("Class " + classname + " not found "); } if (verbose) { System.out.println("c = " + c); } if (null == c) { try { c = ClassLoader.getSystemClassLoader().loadClass(classname); } catch (ClassNotFoundException e) { if (verbose) { System.out.println("System ClassLoader failed to find " + classname); } } } if (null != c) { classes.add(c); } else { System.err.println("Class " + classname + " not found"); } } } if (verbose) { System.out.println("Finished checking classnames arguments"); } } if (classes == null || classes.size() < 1) { if (null == nativesClassMap || nativesClassMap.keySet().size() < 1) { System.err.println("No Classes specified or found."); System.exit(1); } } if (verbose) { System.out.println("Classes found = " + classes.size()); } if (classes.size() > limit) { System.err.println("limit=" + limit); System.err.println( "Too many classes please use -c or -p options to limit classes or -l to increase limit."); if (verbose) { System.out.println("packagesSet = " + packagesSet); } System.exit(1); } List<Class> newClasses = new ArrayList<Class>(); for (Class clss : classes) { Class superClass = clss.getSuperclass(); while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass) && isAddableClass(superClass, excludedClasses)) { newClasses.add(superClass); superClass = superClass.getSuperclass(); } try { Field fa[] = clss.getDeclaredFields(); for (Field f : fa) { if (Modifier.isPublic(f.getModifiers())) { Class fClass = f.getType(); if (!classes.contains(fClass) && !newClasses.contains(fClass) && isAddableClass(fClass, excludedClasses)) { newClasses.add(fClass); } } } } catch (NoClassDefFoundError e) { e.printStackTrace(); } for (Method m : clss.getDeclaredMethods()) { if (m.isSynthetic()) { continue; } if (!Modifier.isPublic(m.getModifiers()) || Modifier.isAbstract(m.getModifiers())) { continue; } Class retType = m.getReturnType(); if (verbose) { System.out.println("Checking dependancies for Method = " + m); } if (!classes.contains(retType) && !newClasses.contains(retType) && isAddableClass(retType, excludedClasses)) { newClasses.add(retType); superClass = retType.getSuperclass(); while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass) && isAddableClass(superClass, excludedClasses)) { newClasses.add(superClass); superClass = superClass.getSuperclass(); } } for (Class paramType : m.getParameterTypes()) { if (!classes.contains(paramType) && !newClasses.contains(paramType) && isAddableClass(paramType, excludedClasses)) { newClasses.add(paramType); superClass = paramType.getSuperclass(); while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass) && !excludedClasses.contains(superClass)) { newClasses.add(superClass); superClass = superClass.getSuperclass(); } } } } } if (null != nativesClassMap) { for (Class clss : nativesClassMap.values()) { if (null != clss) { Class superClass = clss.getSuperclass(); while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass) && !excludedClasses.contains(superClass)) { newClasses.add(superClass); superClass = superClass.getSuperclass(); } } } } if (verbose) { System.out.println("Dependency classes needed = " + newClasses.size()); } classes.addAll(newClasses); List<Class> newOrderClasses = new ArrayList<>(); for (Class clss : classes) { if (newOrderClasses.contains(clss)) { continue; } Class superClass = clss.getSuperclass(); Stack<Class> stack = new Stack<>(); while (null != superClass && !newOrderClasses.contains(superClass) && !superClass.equals(java.lang.Object.class)) { stack.push(superClass); superClass = superClass.getSuperclass(); } while (!stack.empty()) { newOrderClasses.add(stack.pop()); } newOrderClasses.add(clss); } classes = newOrderClasses; if (verbose) { System.out.println("Total number of classes = " + classes.size()); System.out.println("classes = " + classes); } String forward_header = header.substring(0, header.lastIndexOf('.')) + "_fwd.h"; Map<String, String> map = new HashMap<>(); map.put(JAR, jar != null ? jar : ""); map.put("%CLASSPATH_PREFIX%", getCurrentDir() + ((jar != null) ? (File.pathSeparator + ((new File(jar).getCanonicalPath()).replace("\\", "\\\\"))) : "")); map.put("%FORWARD_HEADER%", forward_header); map.put("%HEADER%", header); map.put("%PATH_SEPERATOR%", File.pathSeparator); String tabs = ""; String headerDefine = forward_header.substring(Math.max(0, forward_header.indexOf(File.separator))) .replace(".", "_"); map.put(HEADER_DEFINE, headerDefine); map.put(NAMESPACE, namespace); if (null != nativesClassMap) { for (Entry<String, Class> e : nativesClassMap.entrySet()) { final Class javaClass = e.getValue(); final String nativeClassName = e.getKey(); try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassName + ".java"))) { if (javaClass.isInterface()) { pw.println("public class " + nativeClassName + " implements " + javaClass.getCanonicalName() + ", AutoCloseable{"); } else { pw.println("public class " + nativeClassName + " extends " + javaClass.getCanonicalName() + " implements AutoCloseable{"); } if (null != loadlibname && loadlibname.length() > 0) { pw.println(TAB_STRING + "static {"); pw.println(TAB_STRING + TAB_STRING + "System.loadLibrary(\"" + loadlibname + "\");"); pw.println(TAB_STRING + "}"); pw.println(); } pw.println(TAB_STRING + "public " + nativeClassName + "() {"); pw.println(TAB_STRING + "}"); pw.println(); pw.println(TAB_STRING + "private long nativeAddress=0;"); pw.println(TAB_STRING + "private boolean nativeDeleteOnClose=false;"); pw.println(); pw.println(TAB_STRING + "protected " + nativeClassName + "(final long nativeAddress) {"); pw.println(TAB_STRING + TAB_STRING + "this.nativeAddress = nativeAddress;"); pw.println(TAB_STRING + "}"); pw.println(TAB_STRING + "private native void nativeDelete();"); pw.println(); pw.println(TAB_STRING + "@Override"); pw.println(TAB_STRING + "public synchronized void close() {"); // pw.println(TAB_STRING + TAB_STRING + "if(nativeAddress != 0 && nativeDeleteOnClose) {"); pw.println(TAB_STRING + TAB_STRING + "nativeDelete();"); // pw.println(TAB_STRING + TAB_STRING + "}"); // pw.println(TAB_STRING + TAB_STRING + "nativeAddress=0;"); // pw.println(TAB_STRING + TAB_STRING + "nativeDeleteOnClose=false;"); pw.println(TAB_STRING + "}"); pw.println(); pw.println(TAB_STRING + "protected void finalizer() {"); pw.println(TAB_STRING + TAB_STRING + "close();"); pw.println(TAB_STRING + "}"); pw.println(); Method ma[] = javaClass.getDeclaredMethods(); for (Method m : ma) { int modifiers = m.getModifiers(); if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) // && !m.isDefault() && !m.isSynthetic()) { pw.println(); pw.println(TAB_STRING + "@Override"); String params = ""; for (int i = 0; i < m.getParameterTypes().length; i++) { params += m.getParameterTypes()[i].getCanonicalName() + " param" + i; if (i < m.getParameterTypes().length - 1) { params += ","; } } pw.println(TAB_STRING + "native public " + m.getReturnType().getCanonicalName() + " " + m.getName() + "(" + params + ");"); // + IntStream.range(0, m.getParameterTypes().length) // .mapToObj(i -> m.getParameterTypes()[i].getCanonicalName() + " param" + i) // .collect(Collectors.joining(",")) + ");"); } } pw.println("}"); } } } try (PrintWriter pw = new PrintWriter(new FileWriter(forward_header))) { tabs = ""; processTemplate(pw, map, "header_fwd_template_start.h", tabs); Class lastClass = null; for (int class_index = 0; class_index < classes.size(); class_index++) { Class clss = classes.get(class_index); String clssOnlyName = getCppClassName(clss); tabs = openClassNamespace(clss, pw, tabs, lastClass); tabs += TAB_STRING; pw.println(tabs + "class " + clssOnlyName + ";"); tabs = tabs.substring(0, tabs.length() - 1); Class nextClass = (class_index < (classes.size() - 1)) ? classes.get(class_index + 1) : null; tabs = closeClassNamespace(clss, pw, tabs, nextClass); lastClass = clss; } processTemplate(pw, map, "header_fwd_template_end.h", tabs); } headerDefine = header.substring(Math.max(0, header.indexOf(File.separator))).replace(".", "_"); map.put(HEADER_DEFINE, headerDefine); map.put(NAMESPACE, namespace); if (classes_per_file < 1) { classes_per_file = classes.size(); } final int num_class_segments = (classes.size() > 1) ? ((classes.size() + classes_per_file - 1) / classes_per_file) : 1; String fmt = "%d"; if (num_class_segments > 10) { fmt = "%02d"; } if (num_class_segments > 100) { fmt = "%03d"; } String header_file_base = header; if (header_file_base.endsWith(".h")) { header_file_base = header_file_base.substring(0, header_file_base.length() - 2); } else if (header_file_base.endsWith(".hh")) { header_file_base = header_file_base.substring(0, header_file_base.length() - 3); } else if (header_file_base.endsWith(".hpp")) { header_file_base = header_file_base.substring(0, header_file_base.length() - 4); } try (PrintWriter pw = new PrintWriter(new FileWriter(header))) { tabs = ""; processTemplate(pw, map, HEADER_TEMPLATE_STARTH, tabs); for (int segment_index = 0; segment_index < num_class_segments; segment_index++) { String header_segment_file = header_file_base + String.format(fmt, segment_index) + ".h"; pw.println("#include \"" + header_segment_file + "\""); } if (null != nativesClassMap) { tabs = TAB_STRING; for (Entry<String, Class> e : nativesClassMap.entrySet()) { final Class javaClass = e.getValue(); final String nativeClassName = e.getKey(); pw.println(); pw.println(tabs + "class " + nativeClassName + "Context;"); pw.println(); map.put(CLASS_NAME, nativeClassName); map.put("%BASE_CLASS_FULL_NAME%", "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::")); map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object"); processTemplate(pw, map, HEADER_CLASS_STARTH, tabs); tabs += TAB_STRING; pw.println(tabs + nativeClassName + "Context *context;"); pw.println(tabs + nativeClassName + "();"); pw.println(tabs + "~" + nativeClassName + "();"); Method methods[] = javaClass.getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { continue; } if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) // && !method.isDefault() && !method.isSynthetic()) { pw.println(tabs + getNativeMethodCppDeclaration(method, javaClass)); } } pw.println(tabs + "void initContext(" + nativeClassName + "Context *ctx,bool isref);"); pw.println(tabs + "void deleteContext();"); tabs = tabs.substring(TAB_STRING.length()); pw.println(tabs + "}; // end class " + nativeClassName); } } tabs = ""; processTemplate(pw, map, HEADER_TEMPLATE_ENDH, tabs); } for (int segment_index = 0; segment_index < num_class_segments; segment_index++) { String header_segment_file = header_file_base + String.format(fmt, segment_index) + ".h"; try (PrintWriter pw = new PrintWriter(new FileWriter(header_segment_file))) { tabs = ""; //processTemplate(pw, map, HEADER_TEMPLATE_STARTH, tabs); pw.println("// Never include this file (" + header_segment_file + ") directly. include " + header + " instead."); pw.println(); Class lastClass = null; final int start_segment_index = segment_index * classes_per_file; final int end_segment_index = Math.min(segment_index * classes_per_file + classes_per_file, classes.size()); List<Class> classesSegList = classes.subList(start_segment_index, end_segment_index); pw.println(); pw.println(tabs + "// start_segment_index = " + start_segment_index); pw.println(tabs + "// start_segment_index = " + end_segment_index); pw.println(tabs + "// segment_index = " + segment_index); pw.println(tabs + "// classesSegList=" + classesSegList); pw.println(); for (int class_index = 0; class_index < classesSegList.size(); class_index++) { Class clss = classesSegList.get(class_index); pw.println(); pw.println(tabs + "// class_index = " + class_index + " clss=" + clss); pw.println(); String clssName = clss.getCanonicalName(); tabs = openClassNamespace(clss, pw, tabs, lastClass); String clssOnlyName = getCppClassName(clss); map.put(CLASS_NAME, clssOnlyName); map.put("%BASE_CLASS_FULL_NAME%", classToCppBase(clss)); map.put(OBJECT_CLASS_FULL_NAME, getCppRelativeName(Object.class, clss)); tabs += TAB_STRING; processTemplate(pw, map, HEADER_CLASS_STARTH, tabs); tabs += TAB_STRING; Constructor constructors[] = clss.getDeclaredConstructors(); if (!hasNoArgConstructor(constructors)) { if (Modifier.isAbstract(clss.getModifiers()) || clss.isInterface()) { pw.println(tabs + "protected:"); pw.println(tabs + clssOnlyName + "() {};"); pw.println(tabs + "public:"); } else { if (constructors.length > 0) { pw.println(tabs + "protected:"); } pw.println(tabs + clssOnlyName + "();"); if (constructors.length > 0) { pw.println(tabs + "public:"); } } } for (Constructor c : constructors) { if (c.getParameterTypes().length == 0 && Modifier.isProtected(c.getModifiers())) { pw.println(tabs + "protected:"); pw.println(tabs + clssOnlyName + "();"); pw.println(tabs + "public:"); } if (checkConstructor(c, clss, classes)) { continue; } if (c.getParameterTypes().length == 1 && clss.isAssignableFrom(c.getParameterTypes()[0])) { continue; } if (!Modifier.isPublic(c.getModifiers())) { continue; } if (c.getParameterTypes().length == 1) { if (c.getParameterTypes()[0].getName().equals(clss.getName())) { // if(verbose) System.out.println("skipping constructor."); continue; } } if (!checkParameters(c.getParameterTypes(), classes)) { continue; } pw.println( tabs + clssOnlyName + getCppParamDeclarations(c.getParameterTypes(), clss) + ";"); if (isConstructorToMakeEasy(c, clss)) { pw.println(tabs + clssOnlyName + getEasyCallCppParamDeclarations(c.getParameterTypes(), clss) + ";"); } } pw.println(tabs + "~" + clssOnlyName + "();"); Field fa[] = clss.getDeclaredFields(); for (int findex = 0; findex < fa.length; findex++) { Field field = fa[findex]; if (addGetterMethod(field, clss, classes)) { pw.println(tabs + getCppFieldGetterDeclaration(field, clss)); } if (addSetterMethod(field, clss, classes)) { pw.println(tabs + getCppFieldSetterDeclaration(field, clss)); } } Method methods[] = clss.getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (!checkMethod(method, classes)) { continue; } if ((method.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC) { pw.println(tabs + getCppDeclaration(method, clss)); } if (isArrayStringMethod(method)) { pw.println(tabs + getCppModifiers(method.getModifiers()) + getCppType(method.getReturnType(), clss) + " " + fixMethodName(method) + "(int argc,const char **argv);"); } if (isMethodToMakeEasy(method)) { pw.println(tabs + getEasyCallCppDeclaration(method, clss)); } } tabs = tabs.substring(TAB_STRING.length()); pw.println(tabs + "}; // end class " + clssOnlyName); tabs = tabs.substring(0, tabs.length() - 1); Class nextClass = (class_index < (classesSegList.size() - 1)) ? classesSegList.get(class_index + 1) : null; tabs = closeClassNamespace(clss, pw, tabs, nextClass); pw.println(); lastClass = clss; } //processTemplate(pw, map, HEADER_TEMPLATE_ENDH, tabs); } } for (int segment_index = 0; segment_index < num_class_segments; segment_index++) { String output_segment_file = output; if (output_segment_file.endsWith(".cpp")) { output_segment_file = output_segment_file.substring(0, output_segment_file.length() - 4); } else if (output_segment_file.endsWith(".cc")) { output_segment_file = output_segment_file.substring(0, output_segment_file.length() - 3); } output_segment_file += "" + String.format(fmt, segment_index) + ".cpp"; try (PrintWriter pw = new PrintWriter(new FileWriter(output_segment_file))) { tabs = ""; if (segment_index < 1) { processTemplate(pw, map, "cpp_template_start_first.cpp", tabs); } else { processTemplate(pw, map, CPP_TEMPLATE_STARTCPP, tabs); } final int start_segment_index = segment_index * classes_per_file; final int end_segment_index = Math.min(segment_index * classes_per_file + classes_per_file, classes.size()); List<Class> classesSegList = classes.subList(start_segment_index, end_segment_index); pw.println(); pw.println(tabs + "// start_segment_index = " + start_segment_index); pw.println(tabs + "// start_segment_index = " + end_segment_index); pw.println(tabs + "// segment_index = " + segment_index); pw.println(tabs + "// classesSegList=" + classesSegList); pw.println(); Class lastClass = null; for (int class_index = 0; class_index < classesSegList.size(); class_index++) { Class clss = classesSegList.get(class_index); pw.println(); pw.println(tabs + "// class_index = " + class_index + " clss=" + clss); pw.println(); String clssName = clss.getCanonicalName(); tabs = openClassNamespace(clss, pw, tabs, lastClass); String clssOnlyName = getCppClassName(clss); map.put(CLASS_NAME, clssOnlyName); map.put("%BASE_CLASS_FULL_NAME%", classToCppBase(clss)); map.put(FULL_CLASS_NAME, clssName); String fcjs = classToJNISignature(clss); fcjs = fcjs.substring(1, fcjs.length() - 1); map.put(FULL_CLASS_JNI_SIGNATURE, fcjs); if (verbose) { System.out.println("fcjs = " + fcjs); } map.put(OBJECT_CLASS_FULL_NAME, getCppRelativeName(Object.class, clss)); map.put("%INITIALIZE_CONTEXT%", ""); map.put("%INITIALIZE_CONTEXT_REF%", ""); processTemplate(pw, map, CPP_START_CLASSCPP, tabs); Constructor constructors[] = clss.getDeclaredConstructors(); if (!hasNoArgConstructor(constructors)) { if (!Modifier.isAbstract(clss.getModifiers()) && !clss.isInterface()) { pw.println(tabs + clssOnlyName + "::" + clssOnlyName + "() : " + classToCppBase(clss) + "((jobject)NULL,false) {"); map.put(JNI_SIGNATURE, "()V"); map.put(CONSTRUCTOR_ARGS, ""); processTemplate(pw, map, CPP_NEWCPP, tabs); pw.println(tabs + "}"); pw.println(); } } for (Constructor c : constructors) { if (checkConstructor(c, clss, classes)) { continue; } Class[] paramClasses = c.getParameterTypes(); pw.println(tabs + clssOnlyName + "::" + clssOnlyName + getCppParamDeclarations(paramClasses, clss) + " : " + classToCppBase(clss) + "((jobject)NULL,false) {"); tabs = tabs + TAB_STRING; map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(paramClasses) + ")V"); map.put(CONSTRUCTOR_ARGS, (paramClasses.length > 0 ? "," : "") + getCppParamNames(paramClasses)); processTemplate(pw, map, CPP_NEWCPP, tabs); tabs = tabs.substring(0, tabs.length() - 1); pw.println(tabs + "}"); pw.println(); if (isConstructorToMakeEasy(c, clss)) { pw.println(tabs + clssOnlyName + "::" + clssOnlyName + getEasyCallCppParamDeclarations(c.getParameterTypes(), clss) + " : " + classToCppBase(clss) + "((jobject)NULL,false) {"); processTemplate(pw, map, "cpp_start_easy_constructor.cpp", tabs); for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) { Class paramClasse = paramClasses[paramIndex]; String parmName = getParamNameIn(paramClasse, paramIndex); if (isString(paramClasse)) { pw.println(tabs + "jstring " + parmName + " = env->NewStringUTF(easyArg_" + paramIndex + ");"); } else if (isPrimitiveArray(paramClasse)) { String callString = getMethodCallString(paramClasse.getComponentType()); pw.println(tabs + getCppArrayType(paramClasse.getComponentType()) + " " + classToParamNameDecl(paramClasse, paramIndex) + "= env->New" + callString + "Array(easyArg_" + paramIndex + "_length);"); pw.println(tabs + "env->Set" + callString + "ArrayRegion(" + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_" + paramIndex + "_length,easyArg_" + paramIndex + ");"); } else { pw.println(tabs + getCppType(paramClasse, clss) + " " + classToParamNameDecl(paramClasse, paramIndex) + "= easyArg_" + paramIndex + ";"); } } processTemplate(pw, map, "cpp_new_easy_internals.cpp", tabs); for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) { Class paramClasse = paramClasses[paramIndex]; String parmName = getParamNameIn(paramClasse, paramIndex); if (isString(paramClasse)) { pw.println(tabs + "jobjectRefType ref_" + paramIndex + " = env->GetObjectRefType(" + parmName + ");"); pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {"); pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");"); pw.println(tabs + "}"); } else if (isPrimitiveArray(paramClasse)) { String callString = getMethodCallString(paramClasse.getComponentType()); pw.println(tabs + "env->Get" + callString + "ArrayRegion(" + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_" + paramIndex + "_length,easyArg_" + paramIndex + ");"); pw.println(tabs + "jobjectRefType ref_" + paramIndex + " = env->GetObjectRefType(" + parmName + ");"); pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {"); pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");"); pw.println(tabs + "}"); } else { } } processTemplate(pw, map, "cpp_end_easy_constructor.cpp", tabs); pw.println(tabs + "}"); } } pw.println(); pw.println(tabs + "// Destructor for " + clssName); pw.println(tabs + clssOnlyName + "::~" + clssOnlyName + "() {"); pw.println(tabs + "\t// Place-holder for later extensibility."); pw.println(tabs + "}"); pw.println(); Field fa[] = clss.getDeclaredFields(); for (int findex = 0; findex < fa.length; findex++) { Field field = fa[findex]; if (addGetterMethod(field, clss, classes)) { pw.println(); pw.println(tabs + "// Field getter for " + field.getName()); pw.println(getCppFieldGetterDefinitionStart(tabs, clssOnlyName, field, clss)); map.put("%FIELD_NAME%", field.getName()); map.put(JNI_SIGNATURE, classToJNISignature(field.getType())); Class returnClass = field.getType(); map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss)); map.put(METHOD_ARGS, ""); map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return"); map.put("%METHOD_CALL_TYPE%", getMethodCallString(returnClass)); map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss)); map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass)); String retStore = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnVarType(returnClass) + ") "; map.put("%METHOD_RETURN_STORE%", retStore); map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss)); if (Modifier.isStatic(field.getModifiers())) { processTemplate(pw, map, "cpp_static_getfield.cpp", tabs); } else { processTemplate(pw, map, "cpp_getfield.cpp", tabs); } pw.println(tabs + "}"); } if (addSetterMethod(field, clss, classes)) { pw.println(); pw.println(tabs + "// Field setter for " + field.getName()); pw.println(getCppFieldSetterDefinitionStart(tabs, clssOnlyName, field, clss)); map.put("%FIELD_NAME%", field.getName()); map.put(JNI_SIGNATURE, classToJNISignature(field.getType())); Class returnClass = void.class; map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss)); Class[] paramClasses = new Class[] { field.getType() }; String methodArgs = getCppParamNames(paramClasses); map.put(METHOD_ARGS, (paramClasses.length > 0 ? "," : "") + methodArgs); map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return"); map.put("%METHOD_CALL_TYPE%", getMethodCallString(field.getType())); map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss)); map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass)); String retStore = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnVarType(returnClass) + ") "; map.put("%METHOD_RETURN_STORE%", retStore); map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss)); if (Modifier.isStatic(field.getModifiers())) { processTemplate(pw, map, "cpp_static_setfield.cpp", tabs); } else { processTemplate(pw, map, "cpp_setfield.cpp", tabs); } pw.println(tabs + "}"); } } Method methods[] = clss.getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (checkMethod(method, classes)) { pw.println(); pw.println(getCppMethodDefinitionStart(tabs, clssOnlyName, method, clss)); map.put(METHOD_NAME, method.getName()); if (fixMethodName(method).contains("equals2")) { if (verbose) { System.out.println("debug me"); } } map.put("%JAVA_METHOD_NAME%", method.getName()); Class[] paramClasses = method.getParameterTypes(); String methodArgs = getCppParamNames(paramClasses); map.put(METHOD_ARGS, (paramClasses.length > 0 ? "," : "") + methodArgs); Class returnClass = method.getReturnType(); map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(paramClasses) + ")" + classToJNISignature(returnClass)); map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss)); map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return"); map.put("%METHOD_CALL_TYPE%", getMethodCallString(returnClass)); map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss)); map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass)); String retStore = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnVarType(returnClass) + ") "; map.put("%METHOD_RETURN_STORE%", retStore); map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss)); tabs += TAB_STRING; if (!Modifier.isStatic(method.getModifiers())) { processTemplate(pw, map, CPP_METHODCPP, tabs); } else { processTemplate(pw, map, CPP_STATIC_METHODCPP, tabs); } tabs = tabs.substring(0, tabs.length() - TAB_STRING.length()); pw.println(tabs + "}"); if (isArrayStringMethod(method)) { map.put(METHOD_RETURN_STORE, isVoid(returnClass) ? "" : getCppType(returnClass, clss) + " returnVal="); map.put(METHOD_RETURN_GET, isVoid(returnClass) ? "return ;" : "return returnVal;"); processTemplate(pw, map, CPP_EASY_STRING_ARRAY_METHODCPP, tabs); } else if (isMethodToMakeEasy(method)) { pw.println(); pw.println(tabs + "// Easy call alternative for " + method.getName()); pw.println(getEasyCallCppMethodDefinitionStart(tabs, clssOnlyName, method, clss)); tabs += TAB_STRING; map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclareOut(returnClass, clss)); processTemplate(pw, map, "cpp_start_easy_method.cpp", tabs); for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) { Class paramClasse = paramClasses[paramIndex]; String parmName = getParamNameIn(paramClasse, paramIndex); if (isString(paramClasse)) { pw.println(tabs + "jstring " + parmName + " = env->NewStringUTF(easyArg_" + paramIndex + ");"); } else if (isPrimitiveArray(paramClasse)) { String callString = getMethodCallString(paramClasse.getComponentType()); pw.println(tabs + getCppArrayType(paramClasse.getComponentType()) + " " + classToParamNameDecl(paramClasse, paramIndex) + "= env->New" + callString + "Array(easyArg_" + paramIndex + "_length);"); pw.println(tabs + "env->Set" + callString + "ArrayRegion(" + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_" + paramIndex + "_length,easyArg_" + paramIndex + ");"); } else { pw.println(tabs + getCppType(paramClasse, clss) + " " + classToParamNameDecl(paramClasse, paramIndex) + "= easyArg_" + paramIndex + ";"); } } String methodArgsIn = getCppParamNamesIn(paramClasses); String retStoreOut = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnOutVarType(returnClass, clss) + ") "; pw.println(tabs + retStoreOut + fixMethodName(method) + "(" + methodArgsIn + ");"); for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) { Class paramClasse = paramClasses[paramIndex]; String parmName = getParamNameIn(paramClasse, paramIndex); if (isString(paramClasse)) { pw.println(tabs + "jobjectRefType ref_" + paramIndex + " = env->GetObjectRefType(" + parmName + ");"); pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {"); pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");"); pw.println(tabs + "}"); } else if (isPrimitiveArray(paramClasse)) { String callString = getMethodCallString(paramClasse.getComponentType()); pw.println(tabs + "env->Get" + callString + "ArrayRegion(" + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_" + paramIndex + "_length,easyArg_" + paramIndex + ");"); pw.println(tabs + "jobjectRefType ref_" + paramIndex + " = env->GetObjectRefType(" + parmName + ");"); pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {"); pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");"); pw.println(tabs + "}"); } else { } } processTemplate(pw, map, "cpp_end_easy_method.cpp", tabs); if (!isVoid(returnClass)) { pw.println(tabs + "return retVal;"); } tabs = tabs.substring(TAB_STRING.length()); pw.println(tabs + "}"); pw.println(); } } } processTemplate(pw, map, CPP_END_CLASSCPP, tabs); tabs = tabs.substring(0, tabs.length() - 1); Class nextClass = (class_index < (classesSegList.size() - 1)) ? classesSegList.get(class_index + 1) : null; tabs = closeClassNamespace(clss, pw, tabs, nextClass); lastClass = clss; } if (segment_index < 1) { if (null != nativesClassMap) { for (Entry<String, Class> e : nativesClassMap.entrySet()) { final Class javaClass = e.getValue(); final String nativeClassName = e.getKey(); map.put(CLASS_NAME, nativeClassName); map.put(FULL_CLASS_NAME, nativeClassName); map.put(FULL_CLASS_JNI_SIGNATURE, nativeClassName); map.put("%BASE_CLASS_FULL_NAME%", "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::")); map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object"); map.put("%INITIALIZE_CONTEXT%", "context=NULL; initContext(NULL,false);"); map.put("%INITIALIZE_CONTEXT_REF%", "context=NULL; initContext(objref.context,true);"); tabs += TAB_STRING; processTemplate(pw, map, CPP_START_CLASSCPP, tabs); pw.println(); pw.println(tabs + nativeClassName + "::" + nativeClassName + "() : " + getModifiedClassName(javaClass).replace(".", "::") + "((jobject)NULL,false) {"); tabs += TAB_STRING; pw.println(tabs + "context=NULL;"); pw.println(tabs + "initContext(NULL,false);"); map.put(JNI_SIGNATURE, "()V"); map.put(CONSTRUCTOR_ARGS, ""); processTemplate(pw, map, "cpp_new_native.cpp", tabs); tabs = tabs.substring(TAB_STRING.length()); pw.println(tabs + "}"); pw.println(); pw.println(tabs + "// Destructor for " + nativeClassName); pw.println(tabs + nativeClassName + "::~" + nativeClassName + "() {"); pw.println(tabs + TAB_STRING + "if(NULL != context) {"); pw.println(tabs + TAB_STRING + TAB_STRING + "deleteContext();"); pw.println(tabs + TAB_STRING + "}"); pw.println(tabs + TAB_STRING + "context=NULL;"); pw.println(tabs + "}"); pw.println(); // pw.println(tabs + "public:"); // pw.println(tabs + nativeClassName + "();"); // pw.println(tabs + "~" + nativeClassName + "();"); tabs = tabs.substring(TAB_STRING.length()); // Method methods[] = javaClass.getDeclaredMethods(); // for (int j = 0; j < methods.length; j++) { // Method method = methods[j]; // int modifiers = method.getModifiers(); // if (!Modifier.isPublic(modifiers)) { // continue; // } // pw.println(tabs + getCppDeclaration(method, javaClass)); // } // pw.println(tabs + "}; // end class " + nativeClassName); processTemplate(pw, map, CPP_END_CLASSCPP, tabs); } } processTemplate(pw, map, "cpp_template_end_first.cpp", tabs); tabs = ""; if (null != nativesClassMap) { pw.println("using namespace " + namespace + ";"); pw.println("#ifdef __cplusplus"); pw.println("extern \"C\" {"); pw.println("#endif"); int max_method_count = 0; tabs = ""; for (Entry<String, Class> e : nativesClassMap.entrySet()) { final Class javaClass = e.getValue(); final String nativeClassName = e.getKey(); map.put(CLASS_NAME, nativeClassName); map.put(FULL_CLASS_NAME, nativeClassName); map.put("%BASE_CLASS_FULL_NAME%", "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::")); map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object"); map.put(FULL_CLASS_JNI_SIGNATURE, nativeClassName); map.put(METHOD_ONFAIL, "return;"); Method methods[] = javaClass.getDeclaredMethods(); if (max_method_count < methods.length) { max_method_count = methods.length; } for (int j = 0; j < methods.length; j++) { Method method = methods[j]; int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { continue; } if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) // && !method.isDefault() && !method.isSynthetic()) { Class[] paramClasses = method.getParameterTypes(); String methodArgs = getCppParamNames(paramClasses); map.put(METHOD_ARGS, methodArgs); map.put(METHOD_NAME, method.getName()); Class returnClass = method.getReturnType(); String retStore = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnVarType(returnClass) + ") "; map.put(METHOD_ONFAIL, getOnFailString(returnClass, javaClass)); map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass)); map.put("%METHOD_RETURN_STORE%", retStore); map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, javaClass)); pw.println(); String paramDecls = getCppParamDeclarations(paramClasses, javaClass); String argsToAdd = method.getParameterTypes().length > 0 ? "," + paramDecls.substring(1, paramDecls.length() - 1) : ""; pw.println("JNIEXPORT " + getCppType(returnClass, javaClass) + " JNICALL Java_" + nativeClassName + "_" + method.getName() + "(JNIEnv *env, jobject jthis" + argsToAdd + ") {"); tabs = TAB_STRING; processTemplate(pw, map, "cpp_native_wrap.cpp", tabs); tabs = tabs.substring(TAB_STRING.length()); pw.println("}"); pw.println(); } } pw.println("JNIEXPORT void JNICALL Java_" + nativeClassName + "_nativeDelete(JNIEnv *env, jobject jthis) {"); tabs += TAB_STRING; map.put(METHOD_ONFAIL, getOnFailString(void.class, javaClass)); processTemplate(pw, map, "cpp_native_delete.cpp", tabs); tabs = tabs.substring(TAB_STRING.length()); pw.println(tabs + "}"); pw.println(); } pw.println("#ifdef __cplusplus"); pw.println("} // end extern \"C\""); pw.println("#endif"); map.put("%MAX_METHOD_COUNT%", Integer.toString(max_method_count + 1)); processTemplate(pw, map, "cpp_start_register_native.cpp", tabs); for (Entry<String, Class> e : nativesClassMap.entrySet()) { final Class javaClass = e.getValue(); final String nativeClassName = e.getKey(); map.put(CLASS_NAME, nativeClassName); map.put(FULL_CLASS_NAME, nativeClassName); map.put("%BASE_CLASS_FULL_NAME%", "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::")); map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object"); processTemplate(pw, map, "cpp_start_register_native_class.cpp", tabs); tabs += TAB_STRING; Method methods[] = javaClass.getDeclaredMethods(); int method_number = 0; for (int j = 0; j < methods.length; j++) { Method method = methods[j]; int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { continue; } if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) // && !method.isDefault() && !method.isSynthetic()) { map.put("%METHOD_NUMBER%", Integer.toString(method_number)); map.put(METHOD_NAME, method.getName()); map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(method.getParameterTypes()) + ")" + classToJNISignature(method.getReturnType())); processTemplate(pw, map, "cpp_register_native_item.cpp", tabs); method_number++; } } map.put("%METHOD_NUMBER%", Integer.toString(method_number)); map.put(METHOD_NAME, "nativeDelete"); map.put(JNI_SIGNATURE, "()V"); processTemplate(pw, map, "cpp_register_native_item.cpp", tabs); map.put("%NUM_NATIVE_METHODS%", Integer.toString(method_number)); processTemplate(pw, map, "cpp_end_register_native_class.cpp", tabs); tabs = tabs.substring(TAB_STRING.length()); } processTemplate(pw, map, "cpp_end_register_native.cpp", tabs); } else { pw.println("// No Native classes : registerNativMethods not needed."); pw.println("static void registerNativeMethods(JNIEnv *env) {}"); } } else { processTemplate(pw, map, CPP_TEMPLATE_ENDCPP, tabs); } } if (null != nativesClassMap) { tabs = ""; for (Entry<String, Class> e : nativesClassMap.entrySet()) { String nativeClassName = e.getKey(); File nativeClassHeaderFile = new File( namespace.toLowerCase() + "_" + nativeClassName.toLowerCase() + ".h"); map.put("%NATIVE_CLASS_HEADER%", nativeClassHeaderFile.getName()); map.put(CLASS_NAME, nativeClassName); if (nativeClassHeaderFile.exists()) { if (verbose) { System.out.println("skipping " + nativeClassHeaderFile.getCanonicalPath() + " since it already exists."); } } else { try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassHeaderFile))) { processTemplate(pw, map, "header_native_imp.h", tabs); } } File nativeClassCppFile = new File( namespace.toLowerCase() + "_" + nativeClassName.toLowerCase() + ".cpp"); if (nativeClassCppFile.exists()) { if (verbose) { System.out.println("skipping " + nativeClassCppFile.getCanonicalPath() + " since it already exists."); } } else { try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassCppFile))) { processTemplate(pw, map, "cpp_native_imp_start.cpp", tabs); Class javaClass = e.getValue(); Method methods[] = javaClass.getDeclaredMethods(); int method_number = 0; for (int j = 0; j < methods.length; j++) { Method method = methods[j]; int modifiers = method.getModifiers(); if (!Modifier.isPublic(modifiers)) { continue; } if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) // && !method.isDefault() && !method.isSynthetic()) { Class[] paramClasses = method.getParameterTypes(); // String methodArgs = getCppParamNames(paramClasses); String paramDecls = getCppParamDeclarations(paramClasses, javaClass); String methodArgs = method.getParameterTypes().length > 0 ? paramDecls.substring(1, paramDecls.length() - 1) : ""; map.put(METHOD_ARGS, methodArgs); map.put(METHOD_NAME, method.getName()); Class returnClass = method.getReturnType(); String retStore = isVoid(returnClass) ? "" : "retVal= (" + getMethodReturnVarType(returnClass) + ") "; map.put(METHOD_ONFAIL, getOnFailString(returnClass, javaClass)); map.put("%RETURN_TYPE%", getCppType(returnClass, javaClass)); map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass)); map.put("%METHOD_RETURN_STORE%", retStore); map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, javaClass)); processTemplate(pw, map, "cpp_native_imp_stub.cpp", tabs); } } processTemplate(pw, map, "cpp_native_imp_end.cpp", tabs); } } } } } main_completed = true; }
From source file:edu.upenn.egricelab.AlignerBoost.FilterSAMAlignPE.java
public static void main(String[] args) { if (args.length == 0) { printUsage();/* w ww . jav a2 s.co m*/ return; } try { parseOptions(args); } catch (IllegalArgumentException e) { System.err.println("Error: " + e.getMessage()); printUsage(); return; } // Read in chrList, if specified if (chrFile != null) { chrFilter = new HashSet<String>(); try { BufferedReader chrFilterIn = new BufferedReader(new FileReader(chrFile)); String chr = null; while ((chr = chrFilterIn.readLine()) != null) chrFilter.add(chr); chrFilterIn.close(); if (verbose > 0) System.err.println( "Only looking at alignments on " + chrFilter.size() + " specified chromosomes"); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); return; } } if (verbose > 0) { // Start the processMonitor processMonitor = new Timer(); // Start the ProcessStatusTask statusTask = new ProcessStatusTask(); // Schedule to show the status every 1 second processMonitor.scheduleAtFixedRate(statusTask, 0, statusFreq); } // Read in known SNP file, if specified if (knownSnpFile != null) { if (verbose > 0) System.err.println("Checking known SNPs from user specified VCF file"); knownVCF = new VCFFileReader(new File(knownSnpFile)); } SamReaderFactory readerFac = SamReaderFactory.makeDefault(); SAMFileWriterFactory writerFac = new SAMFileWriterFactory(); if (!isSilent) readerFac.validationStringency(ValidationStringency.LENIENT); // use LENIENT stringency else readerFac.validationStringency(ValidationStringency.SILENT); // use SILENT stringency SamReader in = readerFac.open(new File(inFile)); SAMFileHeader inHeader = in.getFileHeader(); if (inHeader.getGroupOrder() == GroupOrder.reference && inHeader.getSortOrder() == SortOrder.coordinate) System.err.println("Warning: Input file '" + inFile + "' might be sorted by coordinate and cannot be correctly processed!"); SAMFileHeader header = inHeader.clone(); // copy the inFile header as outFile header // Add new programHeader SAMProgramRecord progRec = new SAMProgramRecord(progName); progRec.setProgramName(progName); progRec.setProgramVersion(progVer); progRec.setCommandLine(StringUtils.join(" ", args)); header.addProgramRecord(progRec); //System.err.println(inFile + " groupOrder: " + in.getFileHeader().getGroupOrder() + " sortOrder: " + in.getFileHeader().getSortOrder()); // reset the orders header.setGroupOrder(groupOrder); header.setSortOrder(sortOrder); // write SAMHeader String prevID = null; SAMRecord prevRecord = null; List<SAMRecord> alnList = new ArrayList<SAMRecord>(); List<SAMRecordPair> alnPEList = null; // Estimate fragment length distribution by scan one-pass through the alignments SAMRecordIterator results = in.iterator(); if (!NO_ESTIMATE) { if (verbose > 0) { System.err.println("Estimating insert fragment size distribution ..."); statusTask.reset(); statusTask.setInfo("alignments scanned"); } long N = 0; double fragL_S = 0; // fragLen sum double fragL_SS = 0; // fragLen^2 sum while (results.hasNext()) { SAMRecord record = results.next(); if (verbose > 0) statusTask.updateStatus(); if (record.getFirstOfPairFlag() && !record.isSecondaryOrSupplementary()) { double fragLen = Math.abs(record.getInferredInsertSize()); if (fragLen != 0 && fragLen >= MIN_FRAG_LEN && fragLen <= MAX_FRAG_LEN) { // only consider certain alignments N++; fragL_S += fragLen; fragL_SS += fragLen * fragLen; } // stop estimate if already enough if (MAX_ESTIMATE_SCAN > 0 && N >= MAX_ESTIMATE_SCAN) break; } } if (verbose > 0) statusTask.finish(); // estimate fragment size if (N >= MIN_ESTIMATE_BASE) { // override command line values MEAN_FRAG_LEN = fragL_S / N; SD_FRAG_LEN = Math.sqrt((N * fragL_SS - fragL_S * fragL_S) / (N * (N - 1))); String estStr = String.format("Estimated fragment size distribution: N(%.1f, %.1f)", MEAN_FRAG_LEN, SD_FRAG_LEN); if (verbose > 0) System.err.println(estStr); // also add the estimation to comment header.addComment(estStr); } else { System.err.println( "Unable to estimate the fragment size distribution due to too few observed alignments"); System.err.println( "You have to specify the '--mean-frag-len' and '--sd-frag-len' on the command line and re-run this step"); statusTask.cancel(); processMonitor.cancel(); return; } // Initiate the normal model normModel = new NormalDistribution(MEAN_FRAG_LEN, SD_FRAG_LEN); // reset the iterator, if necessary if (in.type() == SamReader.Type.SAM_TYPE) { try { in.close(); } catch (IOException e) { System.err.println(e.getMessage()); } in = readerFac.open(new File(inFile)); } results.close(); results = in.iterator(); } // end of NO_ESTIMATE SAMFileWriter out = OUT_IS_SAM ? writerFac.makeSAMWriter(header, false, new File(outFile)) : writerFac.makeBAMWriter(header, false, new File(outFile)); // check each alignment again if (verbose > 0) { System.err.println("Filtering alignments ..."); statusTask.reset(); statusTask.setInfo("alignments processed"); } while (results.hasNext()) { SAMRecord record = results.next(); if (verbose > 0) statusTask.updateStatus(); String ID = record.getReadName(); // fix read and quality string for this read, if is a secondary hit from multiple hits, used for BWA alignment if (ID.equals(prevID) && record.getReadLength() == 0) SAMAlignFixer.fixSAMRecordRead(record, prevRecord); if (chrFilter != null && !chrFilter.contains(record.getReferenceName())) { prevID = ID; prevRecord = record; continue; } // fix MD:Z string for certain aligners with invalid format (i.e. seqAlto) if (fixMD) SAMAlignFixer.fixMisStr(record); // fix alignment, ignore if failed (unmapped or empty) if (!SAMAlignFixer.fixSAMRecord(record, knownVCF, DO_1DP)) { prevID = ID; prevRecord = record; continue; } if (!record.getReadPairedFlag()) { System.err.println("Error: alignment is not from a paired-end read at\n" + record.getSAMString()); out.close(); statusTask.cancel(); processMonitor.cancel(); return; } if (!ID.equals(prevID) && prevID != null || !results.hasNext()) { // a non-first new ID meet, or end of alignments // create alnPEList from filtered alnList alnPEList = createAlnPEListFromAlnList(alnList); //System.err.printf("%d alignments for %s transformed to %d alnPairs%n", alnList.size(), prevID, alnPEList.size()); int totalPair = alnPEList.size(); // filter highly unlikely PEhits filterPEHits(alnPEList, MIN_ALIGN_RATE, MIN_IDENTITY); // calculate posterior mapQ for each pair calcPEHitPostP(alnPEList, totalPair, MAX_HIT); // filter hits by mapQ if (MIN_MAPQ > 0) filterPEHits(alnPEList, MIN_MAPQ); // sort the list first with an anonymous class of comparator, with DESCREASING order Collections.sort(alnPEList, Collections.reverseOrder()); // control max-best if (MAX_BEST != 0 && alnPEList.size() > MAX_BEST) { // potential too much best hits int nBestStratum = 0; int bestMapQ = alnPEList.get(0).getPEMapQ(); // best mapQ from first PE for (SAMRecordPair pr : alnPEList) if (pr.getPEMapQ() == bestMapQ) nBestStratum++; else break; // stop searching for sorted list if (nBestStratum > MAX_BEST) alnPEList.clear(); } // filter alignments with auxiliary filters if (!MAX_SENSITIVITY) filterPEHits(alnPEList, MAX_SEED_MIS, MAX_SEED_INDEL, MAX_ALL_MIS, MAX_ALL_INDEL); // report remaining secondary alignments, up-to MAX_REPORT for (int i = 0; i < alnPEList.size() && (MAX_REPORT == 0 || i < MAX_REPORT); i++) { SAMRecordPair repPair = alnPEList.get(i); if (doUpdateBit) repPair.setNotPrimaryAlignmentFlags(i != 0); int nReport = MAX_REPORT == 0 ? Math.min(alnPEList.size(), MAX_REPORT) : alnPEList.size(); int nFiltered = alnPEList.size(); if (repPair.fwdRecord != null) { repPair.fwdRecord.setAttribute("NH", nReport); repPair.fwdRecord.setAttribute("XN", nFiltered); out.addAlignment(repPair.fwdRecord); } if (repPair.revRecord != null) { repPair.revRecord.setAttribute("NH", nReport); repPair.revRecord.setAttribute("XN", nFiltered); out.addAlignment(repPair.revRecord); } } // reset list alnList.clear(); alnPEList.clear(); } // update if (!ID.equals(prevID)) { prevID = ID; prevRecord = record; } alnList.add(record); } // end while try { in.close(); out.close(); } catch (IOException e) { System.err.println(e.getMessage()); } // Terminate the monitor task and monitor if (verbose > 0) { statusTask.cancel(); statusTask.finish(); processMonitor.cancel(); } }
From source file:boa.compiler.BoaCompiler.java
public static void main(final String[] args) throws IOException { CommandLine cl = processCommandLineOptions(args); if (cl == null) return;//from ww w. ja va 2s .c o m final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // get the name of the generated class final String className = getGeneratedClass(cl); // get the filename of the jar we will be writing final String jarName; if (cl.hasOption('o')) jarName = cl.getOptionValue('o'); else jarName = className + ".jar"; // make the output directory File outputRoot = null; if (cl.hasOption("cd")) { outputRoot = new File(cl.getOptionValue("cd")); } else { outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString()); } final File outputSrcDir = new File(outputRoot, "boa"); if (!outputSrcDir.mkdirs()) throw new IOException("unable to mkdir " + outputSrcDir); // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final File outputFile = new File(outputSrcDir, className + ".java"); final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile)); try { final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); final int maxVisitors; if (cl.hasOption('v')) maxVisitors = Integer.parseInt(cl.getOptionValue('v')); else maxVisitors = Integer.MAX_VALUE; for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new ShadowTypeEraser().start(p); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || maxVisitors < 2 || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": compilation failed: "); e.printStackTrace(); } } if (!visitorPrograms.isEmpty()) try { for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } catch (final Exception e) { System.err.println("error fusing visitors - falling back: " + e); e.printStackTrace(); for (final Program p : visitorPrograms) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } if (jobs.size() == 0) throw new RuntimeException("no files compiled without error"); final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program"); st.add("name", className); st.add("numreducers", inputFiles.size()); st.add("jobs", jobs); st.add("jobnames", jobnames); st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings); st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings); st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024); if (DefaultProperties.localDataPath != null) { st.add("isLocal", true); } o.write(st.render().getBytes()); } finally { o.close(); } compileGeneratedSrc(cl, jarName, outputRoot, outputFile); }
From source file:DcmSnd.java
@SuppressWarnings("unchecked") public static void main(String[] args) { CommandLine cl = parse(args);/*from w w w . ja v a 2s . co m*/ DcmSnd dcmsnd = new DcmSnd(); final List<String> argList = cl.getArgList(); String remoteAE = argList.get(0); String[] calledAETAddress = split(remoteAE, '@'); dcmsnd.setCalledAET(calledAETAddress[0]); if (calledAETAddress[1] == null) { dcmsnd.setRemoteHost("127.0.0.1"); dcmsnd.setRemotePort(104); } else { String[] hostPort = split(calledAETAddress[1], ':'); dcmsnd.setRemoteHost(hostPort[0]); dcmsnd.setRemotePort(toPort(hostPort[1])); } if (cl.hasOption("L")) { String localAE = cl.getOptionValue("L"); String[] localPort = split(localAE, ':'); if (localPort[1] != null) { dcmsnd.setLocalPort(toPort(localPort[1])); } String[] callingAETHost = split(localPort[0], '@'); dcmsnd.setCalling(callingAETHost[0]); if (callingAETHost[1] != null) { dcmsnd.setLocalHost(callingAETHost[1]); } } dcmsnd.setOfferDefaultTransferSyntaxInSeparatePresentationContext(cl.hasOption("ts1")); dcmsnd.setSendFileRef(cl.hasOption("fileref")); if (cl.hasOption("username")) { String username = cl.getOptionValue("username"); UserIdentity userId; if (cl.hasOption("passcode")) { String passcode = cl.getOptionValue("passcode"); userId = new UserIdentity.UsernamePasscode(username, passcode.toCharArray()); } else { userId = new UserIdentity.Username(username); } userId.setPositiveResponseRequested(cl.hasOption("uidnegrsp")); dcmsnd.setUserIdentity(userId); } dcmsnd.setStorageCommitment(cl.hasOption("stgcmt")); String remoteStgCmtAE = null; if (cl.hasOption("stgcmtae")) { try { remoteStgCmtAE = cl.getOptionValue("stgcmtae"); String[] aet_hostport = split(remoteStgCmtAE, '@'); String[] host_port = split(aet_hostport[1], ':'); dcmsnd.setStgcmtCalledAET(aet_hostport[0]); dcmsnd.setRemoteStgcmtHost(host_port[0]); dcmsnd.setRemoteStgcmtPort(toPort(host_port[1])); } catch (Exception e) { exit("illegal argument of option -stgcmtae"); } } if (cl.hasOption("connectTO")) dcmsnd.setConnectTimeout(parseInt(cl.getOptionValue("connectTO"), "illegal argument of option -connectTO", 1, Integer.MAX_VALUE)); if (cl.hasOption("reaper")) dcmsnd.setAssociationReaperPeriod(parseInt(cl.getOptionValue("reaper"), "illegal argument of option -reaper", 1, Integer.MAX_VALUE)); if (cl.hasOption("rspTO")) dcmsnd.setDimseRspTimeout(parseInt(cl.getOptionValue("rspTO"), "illegal argument of option -rspTO", 1, Integer.MAX_VALUE)); if (cl.hasOption("acceptTO")) dcmsnd.setAcceptTimeout(parseInt(cl.getOptionValue("acceptTO"), "illegal argument of option -acceptTO", 1, Integer.MAX_VALUE)); if (cl.hasOption("releaseTO")) dcmsnd.setReleaseTimeout(parseInt(cl.getOptionValue("releaseTO"), "illegal argument of option -releaseTO", 1, Integer.MAX_VALUE)); if (cl.hasOption("soclosedelay")) dcmsnd.setSocketCloseDelay(parseInt(cl.getOptionValue("soclosedelay"), "illegal argument of option -soclosedelay", 1, 10000)); if (cl.hasOption("shutdowndelay")) dcmsnd.setShutdownDelay(parseInt(cl.getOptionValue("shutdowndelay"), "illegal argument of option -shutdowndelay", 1, 10000)); if (cl.hasOption("rcvpdulen")) dcmsnd.setMaxPDULengthReceive( parseInt(cl.getOptionValue("rcvpdulen"), "illegal argument of option -rcvpdulen", 1, 10000) * KB); if (cl.hasOption("sndpdulen")) dcmsnd.setMaxPDULengthSend( parseInt(cl.getOptionValue("sndpdulen"), "illegal argument of option -sndpdulen", 1, 10000) * KB); if (cl.hasOption("sosndbuf")) dcmsnd.setSendBufferSize( parseInt(cl.getOptionValue("sosndbuf"), "illegal argument of option -sosndbuf", 1, 10000) * KB); if (cl.hasOption("sorcvbuf")) dcmsnd.setReceiveBufferSize( parseInt(cl.getOptionValue("sorcvbuf"), "illegal argument of option -sorcvbuf", 1, 10000) * KB); if (cl.hasOption("bufsize")) dcmsnd.setTranscoderBufferSize( parseInt(cl.getOptionValue("bufsize"), "illegal argument of option -bufsize", 1, 10000) * KB); dcmsnd.setPackPDV(!cl.hasOption("pdv1")); dcmsnd.setTcpNoDelay(!cl.hasOption("tcpdelay")); if (cl.hasOption("async")) dcmsnd.setMaxOpsInvoked( parseInt(cl.getOptionValue("async"), "illegal argument of option -async", 0, 0xffff)); if (cl.hasOption("lowprior")) dcmsnd.setPriority(CommandUtils.LOW); if (cl.hasOption("highprior")) dcmsnd.setPriority(CommandUtils.HIGH); System.out.println("Scanning files to send"); long t1 = System.currentTimeMillis(); for (int i = 1, n = argList.size(); i < n; ++i) dcmsnd.addFile(new File(argList.get(i))); long t2 = System.currentTimeMillis(); if (dcmsnd.getNumberOfFilesToSend() == 0) { System.exit(2); } System.out.println("\nScanned " + dcmsnd.getNumberOfFilesToSend() + " files in " + ((t2 - t1) / 1000F) + "s (=" + ((t2 - t1) / dcmsnd.getNumberOfFilesToSend()) + "ms/file)"); dcmsnd.configureTransferCapability(); if (cl.hasOption("tls")) { String cipher = cl.getOptionValue("tls"); if ("NULL".equalsIgnoreCase(cipher)) { dcmsnd.setTlsWithoutEncyrption(); } else if ("3DES".equalsIgnoreCase(cipher)) { dcmsnd.setTls3DES_EDE_CBC(); } else if ("AES".equalsIgnoreCase(cipher)) { dcmsnd.setTlsAES_128_CBC(); } else { exit("Invalid parameter for option -tls: " + cipher); } if (cl.hasOption("nossl2")) { dcmsnd.disableSSLv2Hello(); } dcmsnd.setTlsNeedClientAuth(!cl.hasOption("noclientauth")); if (cl.hasOption("keystore")) { dcmsnd.setKeyStoreURL(cl.getOptionValue("keystore")); } if (cl.hasOption("keystorepw")) { dcmsnd.setKeyStorePassword(cl.getOptionValue("keystorepw")); } if (cl.hasOption("keypw")) { dcmsnd.setKeyPassword(cl.getOptionValue("keypw")); } if (cl.hasOption("truststore")) { dcmsnd.setTrustStoreURL(cl.getOptionValue("truststore")); } if (cl.hasOption("truststorepw")) { dcmsnd.setTrustStorePassword(cl.getOptionValue("truststorepw")); } try { dcmsnd.initTLS(); } catch (Exception e) { System.err.println("ERROR: Failed to initialize TLS context:" + e.getMessage()); System.exit(2); } } try { dcmsnd.start(); } catch (Exception e) { System.err.println("ERROR: Failed to start server for receiving " + "Storage Commitment results:" + e.getMessage()); System.exit(2); } try { t1 = System.currentTimeMillis(); try { dcmsnd.open(); } catch (Exception e) { System.err.println("ERROR: Failed to establish association:" + e.getMessage()); System.exit(2); } t2 = System.currentTimeMillis(); System.out.println("Connected to " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s"); t1 = System.currentTimeMillis(); dcmsnd.send(); t2 = System.currentTimeMillis(); prompt(dcmsnd, (t2 - t1) / 1000F); if (dcmsnd.isStorageCommitment()) { t1 = System.currentTimeMillis(); if (dcmsnd.commit()) { t2 = System.currentTimeMillis(); System.out.println( "Request Storage Commitment from " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s"); System.out.println("Waiting for Storage Commitment Result.."); try { DicomObject cmtrslt = dcmsnd.waitForStgCmtResult(); t1 = System.currentTimeMillis(); promptStgCmt(cmtrslt, ((t1 - t2) / 1000F)); } catch (InterruptedException e) { System.err.println("ERROR:" + e.getMessage()); } } } dcmsnd.close(); System.out.println("Released connection to " + remoteAE); if (remoteStgCmtAE != null) { t1 = System.currentTimeMillis(); try { dcmsnd.openToStgcmtAE(); } catch (Exception e) { System.err.println("ERROR: Failed to establish association:" + e.getMessage()); System.exit(2); } t2 = System.currentTimeMillis(); System.out.println("Connected to " + remoteStgCmtAE + " in " + ((t2 - t1) / 1000F) + "s"); t1 = System.currentTimeMillis(); if (dcmsnd.commit()) { t2 = System.currentTimeMillis(); System.out.println("Request Storage Commitment from " + remoteStgCmtAE + " in " + ((t2 - t1) / 1000F) + "s"); System.out.println("Waiting for Storage Commitment Result.."); try { DicomObject cmtrslt = dcmsnd.waitForStgCmtResult(); t1 = System.currentTimeMillis(); promptStgCmt(cmtrslt, ((t1 - t2) / 1000F)); } catch (InterruptedException e) { System.err.println("ERROR:" + e.getMessage()); } } dcmsnd.close(); System.out.println("Released connection to " + remoteStgCmtAE); } } finally { dcmsnd.stop(); } }
From source file:edu.msu.cme.rdp.multicompare.Main.java
public static void main(String[] args) throws Exception { PrintStream hier_out = null;/*from w w w . j a va 2 s . c o m*/ PrintWriter assign_out = new PrintWriter(new NullWriter()); PrintStream bootstrap_out = null; File hier_out_filename = null; String propFile = null; File biomFile = null; File metadataFile = null; PrintWriter shortseq_out = null; List<MCSample> samples = new ArrayList(); ClassificationResultFormatter.FORMAT format = ClassificationResultFormatter.FORMAT.allRank; float conf = CmdOptions.DEFAULT_CONF; String gene = null; int min_bootstrap_words = Classifier.MIN_BOOTSTRSP_WORDS; try { CommandLine line = new PosixParser().parse(options, args); if (line.hasOption(CmdOptions.OUTFILE_SHORT_OPT)) { assign_out = new PrintWriter(line.getOptionValue(CmdOptions.OUTFILE_SHORT_OPT)); } else { throw new IllegalArgumentException("Require the output file for classification assignment"); } if (line.hasOption(CmdOptions.HIER_OUTFILE_SHORT_OPT)) { hier_out_filename = new File(line.getOptionValue(CmdOptions.HIER_OUTFILE_SHORT_OPT)); hier_out = new PrintStream(hier_out_filename); } if (line.hasOption(CmdOptions.BIOMFILE_SHORT_OPT)) { biomFile = new File(line.getOptionValue(CmdOptions.BIOMFILE_SHORT_OPT)); } if (line.hasOption(CmdOptions.METADATA_SHORT_OPT)) { metadataFile = new File(line.getOptionValue(CmdOptions.METADATA_SHORT_OPT)); } if (line.hasOption(CmdOptions.TRAINPROPFILE_SHORT_OPT)) { if (gene != null) { throw new IllegalArgumentException( "Already specified the gene from the default location. Can not specify train_propfile"); } else { propFile = line.getOptionValue(CmdOptions.TRAINPROPFILE_SHORT_OPT); } } if (line.hasOption(CmdOptions.FORMAT_SHORT_OPT)) { String f = line.getOptionValue(CmdOptions.FORMAT_SHORT_OPT); if (f.equalsIgnoreCase("allrank")) { format = ClassificationResultFormatter.FORMAT.allRank; } else if (f.equalsIgnoreCase("fixrank")) { format = ClassificationResultFormatter.FORMAT.fixRank; } else if (f.equalsIgnoreCase("filterbyconf")) { format = ClassificationResultFormatter.FORMAT.filterbyconf; } else if (f.equalsIgnoreCase("db")) { format = ClassificationResultFormatter.FORMAT.dbformat; } else if (f.equalsIgnoreCase("biom")) { format = ClassificationResultFormatter.FORMAT.biom; } else { throw new IllegalArgumentException( "Not an valid output format, only allrank, fixrank, biom, filterbyconf and db allowed"); } } if (line.hasOption(CmdOptions.GENE_SHORT_OPT)) { if (propFile != null) { throw new IllegalArgumentException( "Already specified train_propfile. Can not specify gene any more"); } gene = line.getOptionValue(CmdOptions.GENE_SHORT_OPT).toLowerCase(); if (!gene.equals(ClassifierFactory.RRNA_16S_GENE) && !gene.equals(ClassifierFactory.FUNGALLSU_GENE) && !gene.equals(ClassifierFactory.FUNGALITS_warcup_GENE) && !gene.equals(ClassifierFactory.FUNGALITS_unite_GENE)) { throw new IllegalArgumentException(gene + " not found, choose from" + ClassifierFactory.RRNA_16S_GENE + ", " + ClassifierFactory.FUNGALLSU_GENE + ", " + ClassifierFactory.FUNGALITS_warcup_GENE + ", " + ClassifierFactory.FUNGALITS_unite_GENE); } } if (line.hasOption(CmdOptions.MIN_BOOTSTRAP_WORDS_SHORT_OPT)) { min_bootstrap_words = Integer .parseInt(line.getOptionValue(CmdOptions.MIN_BOOTSTRAP_WORDS_SHORT_OPT)); if (min_bootstrap_words < Classifier.MIN_BOOTSTRSP_WORDS) { throw new IllegalArgumentException(CmdOptions.MIN_BOOTSTRAP_WORDS_LONG_OPT + " must be at least " + Classifier.MIN_BOOTSTRSP_WORDS); } } if (line.hasOption(CmdOptions.BOOTSTRAP_SHORT_OPT)) { String confString = line.getOptionValue(CmdOptions.BOOTSTRAP_SHORT_OPT); try { conf = Float.valueOf(confString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Confidence must be a decimal number"); } if (conf < 0 || conf > 1) { throw new IllegalArgumentException("Confidence must be in the range [0,1]"); } } if (line.hasOption(CmdOptions.SHORTSEQ_OUTFILE_SHORT_OPT)) { shortseq_out = new PrintWriter(line.getOptionValue(CmdOptions.SHORTSEQ_OUTFILE_SHORT_OPT)); } if (line.hasOption(CmdOptions.BOOTSTRAP_OUTFILE_SHORT_OPT)) { bootstrap_out = new PrintStream(line.getOptionValue(CmdOptions.BOOTSTRAP_OUTFILE_SHORT_OPT)); } if (format.equals(ClassificationResultFormatter.FORMAT.biom) && biomFile == null) { throw new IllegalArgumentException("biom format requires an input biom file"); } if (biomFile != null) { // if input biom file provided, use biom format format = ClassificationResultFormatter.FORMAT.biom; } args = line.getArgs(); for (String arg : args) { String[] inFileNames = arg.split(","); File inputFile = new File(inFileNames[0]); File idmappingFile = null; if (!inputFile.exists()) { throw new IllegalArgumentException("Failed to find input file \"" + inFileNames[0] + "\""); } if (inFileNames.length == 2) { idmappingFile = new File(inFileNames[1]); if (!idmappingFile.exists()) { throw new IllegalArgumentException("Failed to find input file \"" + inFileNames[1] + "\""); } } MCSample nextSample = new MCSample(inputFile, idmappingFile); samples.add(nextSample); } if (propFile == null && gene == null) { gene = CmdOptions.DEFAULT_GENE; } if (samples.size() < 1) { throw new IllegalArgumentException("Require at least one sample files"); } } catch (Exception e) { System.out.println("Command Error: " + e.getMessage()); new HelpFormatter().printHelp(80, " [options] <samplefile>[,idmappingfile] ...", "", options, ""); return; } MultiClassifier multiClassifier = new MultiClassifier(propFile, gene, biomFile, metadataFile); MultiClassifierResult result = multiClassifier.multiCompare(samples, conf, assign_out, format, min_bootstrap_words); assign_out.close(); if (hier_out != null) { DefaultPrintVisitor printVisitor = new DefaultPrintVisitor(hier_out, samples); result.getRoot().topDownVisit(printVisitor); hier_out.close(); if (multiClassifier.hasCopyNumber()) { // print copy number corrected counts File cn_corrected_s = new File(hier_out_filename.getParentFile(), "cnadjusted_" + hier_out_filename.getName()); PrintStream cn_corrected_hier_out = new PrintStream(cn_corrected_s); printVisitor = new DefaultPrintVisitor(cn_corrected_hier_out, samples, true); result.getRoot().topDownVisit(printVisitor); cn_corrected_hier_out.close(); } } if (bootstrap_out != null) { for (MCSample sample : samples) { MCSamplePrintUtil.printBootstrapCountTable(bootstrap_out, sample); } bootstrap_out.close(); } if (shortseq_out != null) { for (String id : result.getBadSequences()) { shortseq_out.write(id + "\n"); } shortseq_out.close(); } }