List of usage examples for java.io PrintStream close
public void close()
From source file:edu.msu.cme.rdp.probematch.cli.PrimerMatch.java
public static void main(String[] args) throws Exception { PrintStream out = new PrintStream(System.out); int maxDist = Integer.MAX_VALUE; try {//from www.j a v a 2 s. c o m CommandLine line = new PosixParser().parse(options, args); if (line.hasOption("outFile")) { out = new PrintStream(new File(line.getOptionValue("outFile"))); } if (line.hasOption("maxDist")) { maxDist = Integer.valueOf(line.getOptionValue("maxDist")); } args = line.getArgs(); if (args.length != 2) { throw new Exception("Unexpected number of command line arguments"); } } catch (Exception e) { System.err.println("Error: " + e.getMessage()); new HelpFormatter().printHelp("PrimerMatch <primer_list | primer_file> <seq_file>", options); return; } List<PatternBitMask64> primers = new ArrayList(); if (new File(args[0]).exists()) { File primerFile = new File(args[0]); SequenceFormat seqformat = SeqUtils.guessFileFormat(primerFile); if (seqformat.equals(SequenceFormat.FASTA)) { SequenceReader reader = new SequenceReader(primerFile); Sequence seq; while ((seq = reader.readNextSequence()) != null) { primers.add(new PatternBitMask64(seq.getSeqString(), true, seq.getSeqName())); } reader.close(); } else { BufferedReader reader = new BufferedReader(new FileReader(args[0])); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.equals("")) { primers.add(new PatternBitMask64(line, true)); } } reader.close(); } } else { for (String primer : args[0].split(",")) { primers.add(new PatternBitMask64(primer, true)); } } SeqReader seqReader = new SequenceReader(new File(args[1])); Sequence seq; String primerRegion; out.println("#seqname\tdesc\tprimer_index\tprimer_name\tposition\tmismatches\tseq_primer_region"); while ((seq = seqReader.readNextSequence()) != null) { for (int index = 0; index < primers.size(); index++) { PatternBitMask64 primer = primers.get(index); BitVector64Result results = BitVector64.process(seq.getSeqString().toCharArray(), primer, maxDist); for (BitVector64Match result : results.getResults()) { primerRegion = seq.getSeqString().substring( Math.max(0, result.getPosition() - primer.getPatternLength()), result.getPosition()); if (result.getPosition() < primer.getPatternLength()) { for (int pad = result.getPosition(); pad < primer.getPatternLength(); pad++) { primerRegion = "x" + primerRegion; } } out.println(seq.getSeqName() + "\t" + seq.getDesc() + "\t" + (index + 1) + "\t" + primer.getPrimerName() + "\t" + result.getPosition() + "\t" + result.getScore() + "\t" + primerRegion); } } } out.close(); seqReader.close(); }
From source file:com.jbrisbin.groovy.mqdsl.RabbitMQDsl.java
public static void main(String[] argv) { // Parse command line arguments CommandLine args = null;//from w ww.j ava 2 s.c o m try { Parser p = new BasicParser(); args = p.parse(cliOpts, argv); } catch (ParseException e) { log.error(e.getMessage(), e); } // Check for help if (args.hasOption('?')) { printUsage(); return; } // Runtime properties Properties props = System.getProperties(); // Check for ~/.rabbitmqrc File userSettings = new File(System.getProperty("user.home"), ".rabbitmqrc"); if (userSettings.exists()) { try { props.load(new FileInputStream(userSettings)); } catch (IOException e) { log.error(e.getMessage(), e); } } // Load Groovy builder file StringBuffer script = new StringBuffer(); BufferedInputStream in = null; String filename = "<STDIN>"; if (args.hasOption("f")) { filename = args.getOptionValue("f"); try { in = new BufferedInputStream(new FileInputStream(filename)); } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } } else { in = new BufferedInputStream(System.in); } // Read script if (null != in) { byte[] buff = new byte[4096]; try { for (int read = in.read(buff); read > -1;) { script.append(new String(buff, 0, read)); read = in.read(buff); } } catch (IOException e) { log.error(e.getMessage(), e); } } else { System.err.println("No script file to evaluate..."); } PrintStream stdout = System.out; PrintStream out = null; if (args.hasOption("o")) { try { out = new PrintStream(new FileOutputStream(args.getOptionValue("o")), true); System.setOut(out); } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } } String[] includes = (System.getenv().containsKey("MQDSL_INCLUDE") ? System.getenv("MQDSL_INCLUDE").split(String.valueOf(File.pathSeparatorChar)) : new String[] { System.getenv("HOME") + File.separator + ".mqdsl.d" }); try { // Setup RabbitMQ String username = (args.hasOption("U") ? args.getOptionValue("U") : props.getProperty("mq.user", "guest")); String password = (args.hasOption("P") ? args.getOptionValue("P") : props.getProperty("mq.password", "guest")); String virtualHost = (args.hasOption("v") ? args.getOptionValue("v") : props.getProperty("mq.virtualhost", "/")); String host = (args.hasOption("h") ? args.getOptionValue("h") : props.getProperty("mq.host", "localhost")); int port = Integer.parseInt( args.hasOption("p") ? args.getOptionValue("p") : props.getProperty("mq.port", "5672")); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host); connectionFactory.setPort(port); connectionFactory.setUsername(username); connectionFactory.setPassword(password); if (null != virtualHost) { connectionFactory.setVirtualHost(virtualHost); } // The DSL builder RabbitMQBuilder builder = new RabbitMQBuilder(); builder.setConnectionFactory(connectionFactory); // Our execution environment Binding binding = new Binding(args.getArgs()); binding.setVariable("mq", builder); String fileBaseName = filename.replaceAll("\\.groovy$", ""); binding.setVariable("log", LoggerFactory.getLogger(fileBaseName.substring(fileBaseName.lastIndexOf("/") + 1))); if (null != out) { binding.setVariable("out", out); } // Include helper files GroovyShell shell = new GroovyShell(binding); for (String inc : includes) { File f = new File(inc); if (f.isDirectory()) { File[] files = f.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String s) { return s.endsWith(".groovy"); } }); for (File incFile : files) { run(incFile, shell, binding); } } else { run(f, shell, binding); } } run(script.toString(), shell, binding); while (builder.isActive()) { try { Thread.sleep(500); } catch (InterruptedException e) { log.error(e.getMessage(), e); } } if (null != out) { out.close(); System.setOut(stdout); } } finally { System.exit(0); } }
From source file:org.apache.jackrabbit.oak.plugins.document.rdb.RDBExport.java
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException { String url = null, user = null, pw = null, table = "nodes", query = null, dumpfile = null, lobdir = null; List<String> fieldList = Collections.emptyList(); Format format = Format.JSON; PrintStream out = System.out; Set<String> excl = new HashSet<String>(); excl.add(Document.ID);//from ww w . ja va 2s . c om RDBDocumentSerializer ser = new RDBDocumentSerializer(new MemoryDocumentStore(), excl); String columns = null; String param = null; try { for (int i = 0; i < args.length; i++) { param = args[i]; if ("-u".equals(param) || "--username".equals(param)) { user = args[++i]; } else if ("-p".equals(param) || "--password".equals(param)) { pw = args[++i]; } else if ("-c".equals(param) || "--collection".equals(param)) { table = args[++i]; } else if ("-j".equals(param) || "--jdbc-url".equals(param)) { url = args[++i]; } else if ("-q".equals(param) || "--query".equals(param)) { query = args[++i]; } else if ("-o".equals(param) || "--out".equals(param)) { OutputStream os = new FileOutputStream(args[++i]); out = new PrintStream(os, true, "UTF-8"); } else if ("--from-db2-dump".equals(param)) { dumpfile = args[++i]; } else if ("--lobdir".equals(param)) { lobdir = args[++i]; } else if ("--jsonArray".equals(param)) { format = Format.JSONARRAY; } else if ("--csv".equals(param)) { format = Format.CSV; } else if ("--columns".equals(param)) { columns = args[++i]; } else if ("--fields".equals(param)) { String fields = args[++i]; fieldList = Arrays.asList(fields.split(",")); } else if ("--version".equals(param)) { System.out.println(RDBExport.class.getName() + " version " + OakVersion.getVersion()); System.exit(0); } else if ("--help".equals(param)) { printHelp(); System.exit(0); } else { System.err.println(RDBExport.class.getName() + ": invalid parameter " + args[i]); printUsage(); System.exit(2); } } } catch (IndexOutOfBoundsException ex) { System.err.println(RDBExport.class.getName() + ": value missing for parameter " + param); printUsage(); System.exit(2); } if (format == Format.CSV && fieldList.isEmpty()) { System.err.println(RDBExport.class.getName() + ": csv output requires specification of field list"); System.exit(2); } // JSON output with fieldList missing "_id" if ((format == Format.JSON || format == Format.JSONARRAY) && !fieldList.isEmpty() && !fieldList.contains("_id")) { fieldList = new ArrayList<String>(fieldList); fieldList.add(0, "_id"); } if (dumpfile == null && url == null) { System.err.println(RDBExport.class.getName() + ": must use either dump file or JDBC URL"); printUsage(); System.exit(2); } else if (dumpfile != null) { columns = (columns == null) ? "id, modified, hasbinary, deletedonce, cmodcount, modcount, dsize, data, bdata" : columns; List<String> columnList = Arrays .asList(columns.toLowerCase(Locale.ENGLISH).replace(" ", "").split(",")); dumpFile(dumpfile, lobdir, format, out, fieldList, columnList, ser); } else { if (columns != null) { System.err.println(RDBExport.class.getName() + ": column names ignored when using JDBC"); } dumpJDBC(url, user, pw, table, query, format, out, fieldList, ser); } out.flush(); out.close(); }
From source file:edu.msu.cme.rdp.multicompare.Reprocess.java
/** * This class reprocesses the classification results (allrank output) and print out hierarchy output file, based on the confidence cutoff; * and print out only the detail classification results with assignment at certain rank with confidence above the cutoff or/and matching a given taxon. * @param args/*from w ww.j av a 2 s . c om*/ * @throws Exception */ public static void main(String[] args) throws Exception { PrintWriter assign_out = new PrintWriter(new NullWriter()); float conf = 0.8f; PrintStream heir_out = null; String hier_out_filename = null; ClassificationResultFormatter.FORMAT format = ClassificationResultFormatter.FORMAT.allRank; String rank = null; String taxonFilterFile = null; String train_propfile = null; String gene = null; List<MCSample> samples = new ArrayList(); try { CommandLine line = new PosixParser().parse(options, args); if (line.hasOption(CmdOptions.HIER_OUTFILE_SHORT_OPT)) { hier_out_filename = line.getOptionValue(CmdOptions.HIER_OUTFILE_SHORT_OPT); heir_out = new PrintStream(hier_out_filename); } else { throw new Exception( "It make sense to provide output filename for " + CmdOptions.HIER_OUTFILE_LONG_OPT); } if (line.hasOption(CmdOptions.OUTFILE_SHORT_OPT)) { assign_out = new PrintWriter(line.getOptionValue(CmdOptions.OUTFILE_SHORT_OPT)); } if (line.hasOption(CmdOptions.RANK_SHORT_OPT)) { rank = line.getOptionValue(CmdOptions.RANK_SHORT_OPT); } if (line.hasOption(CmdOptions.TAXON_SHORT_OPT)) { taxonFilterFile = line.getOptionValue(CmdOptions.TAXON_SHORT_OPT); } if (line.hasOption(CmdOptions.BOOTSTRAP_SHORT_OPT)) { conf = Float.parseFloat(line.getOptionValue(CmdOptions.BOOTSTRAP_SHORT_OPT)); if (conf < 0 || conf > 1) { throw new IllegalArgumentException("Confidence must be in the range [0,1]"); } } 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("db")) { format = ClassificationResultFormatter.FORMAT.dbformat; } else if (f.equalsIgnoreCase("filterbyconf")) { format = ClassificationResultFormatter.FORMAT.filterbyconf; } else { throw new IllegalArgumentException( "Not valid output format, only allrank, fixrank, filterbyconf and db allowed"); } } 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 { train_propfile = line.getOptionValue(CmdOptions.TRAINPROPFILE_SHORT_OPT); } } if (line.hasOption(CmdOptions.GENE_SHORT_OPT)) { if (train_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 + " is NOT valid, only allows " + ClassifierFactory.RRNA_16S_GENE + ", " + ClassifierFactory.FUNGALLSU_GENE + ", " + ClassifierFactory.FUNGALITS_warcup_GENE + " and " + ClassifierFactory.FUNGALITS_unite_GENE); } } args = line.getArgs(); if (args.length < 1) { throw new Exception("Incorrect number of command line arguments"); } for (String arg : args) { String[] inFileNames = arg.split(","); String inputFile = inFileNames[0]; File idmappingFile = null; if (inFileNames.length == 2) { idmappingFile = new File(inFileNames[1]); if (!idmappingFile.exists()) { System.err.println("Failed to find input file \"" + inFileNames[1] + "\""); return; } } MCSample nextSample = new MCSampleResult(inputFile, idmappingFile); samples.add(nextSample); } } catch (Exception e) { System.out.println("Command Error: " + e.getMessage()); new HelpFormatter().printHelp(120, "Reprocess [options] <Classification_allrank_result>[,idmappingfile] ...", "", options, ""); return; } if (train_propfile == null && gene == null) { gene = ClassifierFactory.RRNA_16S_GENE; } HashSet<String> taxonFilter = null; if (taxonFilterFile != null) { taxonFilter = readTaxonFilterFile(taxonFilterFile); } MultiClassifier multiClassifier = new MultiClassifier(train_propfile, gene); DefaultPrintVisitor printVisitor = new DefaultPrintVisitor(heir_out, samples); MultiClassifierResult result = multiClassifier.multiClassificationParser(samples, conf, assign_out, format, rank, taxonFilter); result.getRoot().topDownVisit(printVisitor); assign_out.close(); heir_out.close(); if (multiClassifier.hasCopyNumber()) { // print copy number corrected counts File cn_corrected_s = new File(new File(hier_out_filename).getParentFile(), "cncorrected_" + hier_out_filename); 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(); } }
From source file:edu.msu.cme.rdp.alignment.errorcheck.CompareErrorType.java
public static void main(String[] args) throws IOException { Options options = new Options(); options.addOption("s", "stem", true, "Output stem (default <query_nucl.fasta>)"); final SeqReader queryReader; final List<Sequence> refSeqList; final PrintStream alignOutStream; final CompareErrorType errorProcessor; Sequence seq;//from w w w . ja va2s .co m Map<String, PAObject> matchMap = new HashMap(); try { CommandLine line = new PosixParser().parse(options, args); String stem; args = line.getArgs(); if (args.length != 2 && args.length != 3) { throw new Exception("Unexpected number of arguments"); } File refFile = new File(args[0]); File queryFile = new File(args[1]); if (line.hasOption("stem")) { stem = line.getOptionValue("stem"); } else { stem = queryFile.getName(); } File alignOutFile = new File(stem + "_alignments.txt"); File mismatchOutFile = new File(stem + "_mismatches.txt"); File indelOutFile = new File(stem + "_indels.txt"); File qualOutFile = null; refSeqList = SequenceReader.readFully(refFile); if (args.length == 3) { queryReader = new QSeqReader(queryFile, new File(args[2])); } else { queryReader = new SequenceReader(queryFile); } seq = queryReader.readNextSequence(); if (seq instanceof QSequence) { qualOutFile = new File(stem + "_qual.txt"); } errorProcessor = new CompareErrorType(mismatchOutFile, indelOutFile, qualOutFile); alignOutStream = new PrintStream(alignOutFile); System.err.println("Starting CompareErrorType"); System.err.println("* Time: " + new Date()); System.err.println("* Reference File: " + refFile); System.err.println("* Query File: " + queryFile); if (args.length == 3) { System.err.println("* Qual File: " + args[2]); } System.err.println("* Query format: " + queryReader.getFormat()); System.err.println("* Alignment Output: " + alignOutFile); System.err.println("* Mismatches Output: " + mismatchOutFile); System.err.println("* Alignment Output: " + indelOutFile); if (qualOutFile != null) { System.err.println("* Quality Output: " + qualOutFile); } } catch (Exception e) { new HelpFormatter().printHelp( "CompareErrorType [options] <ref_nucl> (<query_nucl> | <query_nucl.fasta> <query_nucl.qual>)", options); System.err.println("ERROR: " + e.getMessage()); throw new RuntimeException(e); //System.exit(1); //return; } //ScoringMatrix scoringMatrix = ScoringMatrix.getDefaultNuclMatrix(); // use a simple scoring function, match score 0, mismatch -1, gap opening -1, gap extension -1. ScoringMatrix scoringMatrix = new ScoringMatrix( ScoringMatrix.class.getResourceAsStream("/data/simple_scoringmatrix.txt"), -1, -1); do { try { PairwiseAlignment bestResult = null; Sequence bestSeq = null; boolean bestReversed = false; String querySeqStr = seq.getSeqString().toLowerCase(); String reversedQuery = IUBUtilities.reverseComplement(querySeqStr); PAObject bestMatch = null; //checking if sequence has been seen before if (matchMap.containsKey(seq.getSeqString())) { bestMatch = matchMap.get(seq.getSeqString()); } else { for (Sequence refSeq : refSeqList) { String refSeqStr = refSeq.getSeqString().toLowerCase(); PairwiseAlignment result = PairwiseAligner.align(refSeqStr, querySeqStr, scoringMatrix, AlignmentMode.global); PairwiseAlignment reversedResult = PairwiseAligner.align(refSeqStr, IUBUtilities.reverseComplement(querySeqStr), scoringMatrix, AlignmentMode.global); PairwiseAlignment currBest = (result.getScore() > reversedResult.getScore()) ? result : reversedResult; if (bestResult == null || currBest.getScore() > bestResult.getScore()) { bestResult = currBest; bestSeq = refSeq; if (currBest == reversedResult) { bestReversed = true; } else { bestReversed = false; } } //Since this is a new sequence, make a new PAObject to put into the map to compare against later bestMatch = new PAObject(bestResult, bestReversed, bestSeq); matchMap.put(seq.getSeqString(), bestMatch); } } int refStart = bestMatch.getPA().getStarti(); int refEnd = bestMatch.getPA().getEndi(); bestSeq = bestMatch.getRefSeq(); bestReversed = bestMatch.getReversed(); bestResult = bestMatch.getPA(); //output information alignOutStream.println(">\t" + seq.getSeqName() + "\t" + bestSeq.getSeqName() + "\t" + seq.getSeqString().length() + "\t" + refStart + "\t" + refEnd + "\t" + bestResult.getScore() + "\t" + ((bestReversed) ? "\treversed" : "")); alignOutStream.println(bestResult.getAlignedSeqj() + "\n"); alignOutStream.println(bestResult.getAlignedSeqi() + "\n"); //seqi is reference seq, seqj is the refseq errorProcessor.processSequence(seq, bestResult.getAlignedSeqj(), bestSeq.getSeqName(), bestResult.getAlignedSeqi(), refStart, bestReversed); } catch (Exception e) { throw new RuntimeException("Failed while processing seq " + seq.getSeqName(), e); } } while ((seq = queryReader.readNextSequence()) != null); queryReader.close(); alignOutStream.close(); errorProcessor.close(); }
From source file:edu.msu.cme.rdp.graph.utils.ContigMerger.java
public static void main(String[] args) throws IOException { final BufferedReader hmmgsResultReader; final IndexedSeqReader nuclContigReader; final double minBits; final int minProtLength; final Options options = new Options(); final PrintStream out; final ProfileHMM hmm; final FastaWriter protSeqOut; final FastaWriter nuclSeqOut; final boolean prot; final boolean all; final String shortSampleName; options.addOption("a", "all", false, "Generate all combinations for multiple paths, instead of just the best"); options.addOption("b", "min-bits", true, "Minimum bits score"); options.addOption("l", "min-length", true, "Minimum length"); options.addOption("s", "short_samplename", true, "short sample name, to be used as part of contig identifiers. This allow analyzing contigs together from different samples in downstream analysis "); options.addOption("o", "out", true, "Write output to file instead of stdout"); try {// w ww .j a v a 2 s .c o m CommandLine line = new PosixParser().parse(options, args); if (line.hasOption("min-bits")) { minBits = Double.valueOf(line.getOptionValue("min-bits")); } else { minBits = Double.NEGATIVE_INFINITY; } if (line.hasOption("min-length")) { minProtLength = Integer.valueOf(line.getOptionValue("min-length")); } else { minProtLength = 0; } if (line.hasOption("short_samplename")) { shortSampleName = line.getOptionValue("short_samplename") + "_"; } else { shortSampleName = ""; } if (line.hasOption("out")) { out = new PrintStream(line.getOptionValue("out")); } else { out = System.err; } all = line.hasOption("all"); args = line.getArgs(); if (args.length != 3) { throw new Exception("Unexpected number of arguments"); } hmmgsResultReader = new BufferedReader(new FileReader(new File(args[1]))); nuclContigReader = new IndexedSeqReader(new File(args[2])); hmm = HMMER3bParser.readModel(new File(args[0])); prot = (hmm.getAlphabet() == SequenceType.Protein); if (prot) { protSeqOut = new FastaWriter(new File("prot_merged.fasta")); } else { protSeqOut = null; } nuclSeqOut = new FastaWriter(new File("nucl_merged.fasta")); } catch (Exception e) { new HelpFormatter().printHelp("USAGE: ContigMerger [options] <hmm> <hmmgs_file> <nucl_contig>", options); System.err.println("Error: " + e.getMessage()); System.exit(1); throw new RuntimeException("I hate you javac"); } String line; SearchDirection lastDir = SearchDirection.left; //So this has an assumption built in //It depends on hmmgs always outputting left fragments, then right //We can't just use the kmer to figure out if we've switched to another starting point //because we allow multiple starting model pos, so two different starting //positions can have the same starting kmer Map<String, Sequence> leftContigs = new HashMap(); Map<String, Sequence> rightContigs = new HashMap(); int contigsMerged = 0; int writtenMerges = 0; long startTime = System.currentTimeMillis(); String kmer = null; String geneName = null; while ((line = hmmgsResultReader.readLine()) != null) { if (line.startsWith("#")) { continue; } String[] lexemes = line.trim().split("\t"); if (lexemes.length != 12 || lexemes[0].equals("-")) { System.err.println("Skipping line: " + line); continue; } //contig_53493 nirk 1500:6:35:16409:3561/1 ADV15048 tcggcgctctacacgttcctgcagcccggg 40 210 70 left -44.692 184 0 int index = 0; String seqid = lexemes[0]; geneName = lexemes[1]; String readid = lexemes[2]; String refid = lexemes[3]; kmer = lexemes[4]; int modelStart = Integer.valueOf(lexemes[5]); int nuclLength = Integer.valueOf(lexemes[6]); int protLength = Integer.valueOf(lexemes[7]); SearchDirection dir = SearchDirection.valueOf(lexemes[8]); if (dir != lastDir) { if (dir == SearchDirection.left) { List<MergedContig> mergedContigs = all ? mergeAllContigs(leftContigs, rightContigs, kmer, geneName, hmm) : mergeContigs(leftContigs, rightContigs, kmer, geneName, hmm); contigsMerged++; for (MergedContig mc : mergedContigs) { String mergedId = shortSampleName + geneName + "_" + mc.leftContig + "_" + mc.rightContig; out.println(mergedId + "\t" + mc.length + "\t" + mc.score); if (mc.score > minBits && mc.length > minProtLength) { if (prot) { protSeqOut.writeSeq(mergedId, mc.protSeq); } nuclSeqOut.writeSeq(mergedId, mc.nuclSeq); writtenMerges++; } } leftContigs.clear(); rightContigs.clear(); } lastDir = dir; } Sequence seq = nuclContigReader.readSeq(seqid); if (dir == SearchDirection.left) { leftContigs.put(seqid, seq); } else if (dir == SearchDirection.right) { rightContigs.put(seqid, seq); } else { throw new IOException("Cannot handle search direction " + dir); } } if (!leftContigs.isEmpty() || !rightContigs.isEmpty()) { List<MergedContig> mergedContigs = all ? mergeAllContigs(leftContigs, rightContigs, kmer, geneName, hmm) : mergeContigs(leftContigs, rightContigs, kmer, geneName, hmm); for (MergedContig mc : mergedContigs) { String mergedId = shortSampleName + mc.gene + "_" + mc.leftContig + "_" + mc.rightContig; out.println(mergedId + "\t" + mc.length + "\t" + mc.score); contigsMerged++; if (mc.score > minBits && mc.length > minProtLength) { if (prot) { protSeqOut.writeSeq(mergedId, mc.protSeq); } nuclSeqOut.writeSeq(mergedId, mc.nuclSeq); writtenMerges++; } } } out.close(); if (prot) { protSeqOut.close(); } nuclSeqOut.close(); System.err.println("Read in " + contigsMerged + " contigs, wrote out " + writtenMerges + " merged contigs in " + ((double) (System.currentTimeMillis() - startTime) / 1000) + "s"); }
From source file:edu.msu.cme.rdp.probematch.cli.SliceToPrimer.java
public static void main(String[] args) throws Exception { //args = "--fedit-dist 4 --redit-dist=4 -k --max-length=400 --min-length=280 -o java_sliced_edit4.fasta TGCGAYCCSAARGCBGACTC ATSGCCATCATYTCRCCGGA /scratch/fishjord/tae_kwon_primer_match/all_genomes.fasta".split(" "); PatternBitMask64[] fprimers;// ww w. jav a 2 s . c o m String[] fprimerStrs, rprimerStrs; PatternBitMask64[] rprimers; FastaWriter seqOut; PrintStream statsOut; int fEdit = 3; int rEdit = 3; int minLength = Integer.MIN_VALUE; int maxLength = Integer.MAX_VALUE; boolean allowAmbiguities = true; boolean keepPrimers = false; SequenceReader inSeqs; try { CommandLine line = new PosixParser().parse(options, args); if (line.hasOption("edit-dist")) { fEdit = rEdit = Integer.parseInt(line.getOptionValue("edit-dist")); if (line.hasOption("redit-dist") || line.hasOption("fedit-dist")) { throw new Exception("edit-dist, [fedit-dist, redit-dist] are mutually exclusive"); } } if (line.hasOption("fedit-dist")) { fEdit = Integer.parseInt(line.getOptionValue("fedit-dist")); } if (line.hasOption("no-ambiguities")) { allowAmbiguities = false; } if (line.hasOption("keep-primers")) { keepPrimers = true; } if (line.hasOption("redit-dist")) { rEdit = Integer.parseInt(line.getOptionValue("redit-dist")); } if (line.hasOption("seq-out")) { seqOut = new FastaWriter(new File(line.getOptionValue("seq-out"))); } else { throw new Exception("Must specify seq-out"); } if (line.hasOption("stats-out")) { statsOut = new PrintStream(new File(line.getOptionValue("stats-out"))); } else { statsOut = System.out; } if (line.hasOption("min-length")) { minLength = Integer.parseInt(line.getOptionValue("min-length")); } if (line.hasOption("max-length")) { maxLength = Integer.parseInt(line.getOptionValue("max-length")); } args = line.getArgs(); if (args.length != 3) { throw new Exception("Unexpected number of command line arguments"); } fprimers = translateStringPrimers(args[0].split(","), allowAmbiguities, false); fprimerStrs = args[0].split(","); rprimers = translateStringPrimers(args[1].split(","), allowAmbiguities, true); rprimerStrs = args[1].split(","); inSeqs = new SequenceReader(new File(args[2])); } catch (Exception e) { new HelpFormatter().printHelp("SliceToPrimer [options] <f,p,r,i,m,e,r> <r,p,r,i,m,e,r> <in_seq_file>", options); System.err.println("ERROR: " + e.getMessage()); return; } Sequence seq; statsOut.println( "orig_seqid\tsliced_seqid\tfprimer\tstart\tend\tscore\trprimer\tstart\tend\tscore\tlength"); ScoringMatrix sccoringMatrix = ScoringMatrix.getDefaultNuclMatrix(); DPMAligner[] faligners = new DPMAligner[fprimers.length]; for (int index = 0; index < faligners.length; index++) { faligners[index] = new DPMAligner(fprimerStrs[index], Integer.MAX_VALUE); } try { while ((seq = inSeqs.readNextSequence()) != null) { Set<PrimerMatch> fprimerMatches = new HashSet(); Set<PrimerMatch> rprimerMatches = new HashSet(); for (int index = 0; index < fprimers.length; index++) { PatternBitMask64 primer = fprimers[index]; for (BitVector64Match r : BitVector64.process(seq.getSeqString().toCharArray(), primer, fEdit) .getResults()) { PrimerMatch match = new PrimerMatch(); match.start = r.getPosition() - (primer.getPatternLength() + r.getScore()); match.end = r.getPosition(); match.score = r.getScore(); match.primerIndex = index; fprimerMatches.add(match); } } for (int index = 0; index < rprimers.length; index++) { PatternBitMask64 primer = rprimers[index]; for (BitVector64Match r : BitVector64.process(seq.getSeqString().toCharArray(), primer, rEdit) .getResults()) { PrimerMatch match = new PrimerMatch(); match.start = r.getPosition() - (primer.getPatternLength() + r.getScore()); match.end = r.getPosition(); match.score = r.getScore(); match.primerIndex = index; rprimerMatches.add(match); } } if (fprimerMatches.isEmpty() || rprimerMatches.isEmpty()) { statsOut.println(seq.getSeqName() + "\tEither/or no forward/reverse primer hits"); continue; } for (PrimerMatch fmatch : fprimerMatches) { PrimerMatch bestReverse = null; int bestScore = Integer.MAX_VALUE; for (PrimerMatch rmatch : rprimerMatches) { if (rmatch.start > fmatch.end && rmatch.start - fmatch.end < bestScore) { bestReverse = rmatch; bestScore = rmatch.start - fmatch.end; } } if (bestReverse == null) { statsOut.println(seq.getSeqName() + "\tNo reverse primer before " + fmatch.end); continue; } String slicedSeq = null; if (keepPrimers) { slicedSeq = seq.getSeqString().substring(fmatch.start, bestReverse.end); } else { slicedSeq = seq.getSeqString().substring(fmatch.end, bestReverse.start); } String seqid = seq.getSeqName() + "_" + fmatch.primerIndex + "_" + fmatch.start; if (slicedSeq.length() > minLength && slicedSeq.length() < maxLength) { seqOut.writeSeq(seqid, "", slicedSeq); } DPMAlignment seqs = faligners[fmatch.primerIndex] .align(seq.getSeqString().substring(fmatch.start, fmatch.end)); System.err.println(">" + seqid); System.err.println(fprimerStrs[fmatch.primerIndex]); System.err.println(seq.getSeqString().substring(fmatch.start, fmatch.end)); System.err.println(); System.err.println(seqs.getAlignedMatchFragment()); System.err.println(seqs.getAlignedProbe()); System.err.println(); statsOut.println(seq.getSeqName() + "\t" + seqid + "\t" + fmatch.primerIndex + "\t" + fmatch.start + "\t" + fmatch.end + "\t" + fmatch.score + "\t" + bestReverse.primerIndex + "\t" + bestReverse.start + "\t" + bestReverse.end + "\t" + bestReverse.score + "\t" + slicedSeq.length()); } } } catch (Exception e) { e.printStackTrace(); } finally { statsOut.close(); seqOut.close(); } }
From source file:edu.msu.cme.rdp.multicompare.Main.java
public static void main(String[] args) throws Exception { PrintStream hier_out = null; PrintWriter assign_out = new PrintWriter(new NullWriter()); PrintStream bootstrap_out = null; File hier_out_filename = null; String propFile = null;//from ww w . j a v a 2 s . c o m 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(); } }
From source file:io.compgen.cgpipe.CGPipe.java
public static void main(String[] args) { String fname = null;/* ww w .ja va 2 s.c om*/ String logFilename = null; String outputFilename = null; PrintStream outputStream = null; int verbosity = 0; boolean silent = false; boolean dryrun = false; boolean silenceStdErr = false; boolean showHelp = false; List<String> targets = new ArrayList<String>(); Map<String, VarValue> confVals = new HashMap<String, VarValue>(); String k = null; for (int i = 0; i < args.length; i++) { String arg = args[i]; if (i == 0) { if (new File(arg).exists()) { fname = arg; silenceStdErr = true; continue; } } else if (args[i - 1].equals("-f")) { fname = arg; continue; } else if (args[i - 1].equals("-l")) { logFilename = arg; continue; } else if (args[i - 1].equals("-o")) { outputFilename = arg; continue; } if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } showHelp = true; } else if (arg.equals("-license")) { license(); System.exit(1); } else if (arg.equals("-s")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } silent = true; } else if (arg.equals("-nolog")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } silenceStdErr = true; } else if (arg.equals("-v")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } verbosity++; } else if (arg.equals("-vv")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } verbosity += 2; } else if (arg.equals("-vvv")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } verbosity += 3; } else if (arg.equals("-dr")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } dryrun = true; } else if (arg.startsWith("--")) { if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } k = arg.substring(2); } else if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } if (confVals.containsKey(k)) { try { VarValue val = confVals.get(k); if (val.getClass().equals(VarList.class)) { ((VarList) val).add(VarValue.parseStringRaw(arg)); } else { VarList list = new VarList(); list.add(val); list.add(VarValue.parseStringRaw(arg)); confVals.put(k, list); } } catch (VarTypeException e) { System.err.println("Error setting variable: " + k + " => " + arg); System.exit(1); ; } } else { confVals.put(k, VarValue.parseStringRaw(arg)); } k = null; } else if (arg.charAt(0) != '-') { targets.add(arg); } } if (k != null) { if (k.contains("-")) { k = k.replaceAll("-", "_"); } confVals.put(k, VarBool.TRUE); } confVals.put("cgpipe.loglevel", new VarInt(verbosity)); if (fname == null) { usage(); System.exit(1); } if (!showHelp) { switch (verbosity) { case 0: SimpleFileLoggerImpl.setLevel(Level.INFO); break; case 1: SimpleFileLoggerImpl.setLevel(Level.DEBUG); break; case 2: SimpleFileLoggerImpl.setLevel(Level.TRACE); break; case 3: default: SimpleFileLoggerImpl.setLevel(Level.ALL); break; } } else { SimpleFileLoggerImpl.setLevel(Level.FATAL); } SimpleFileLoggerImpl.setSilent(silenceStdErr || showHelp); Log log = LogFactory.getLog(CGPipe.class); log.info("Starting new run: " + fname); if (logFilename != null) { confVals.put("cgpipe.log", new VarString(logFilename)); } if (System.getenv("CGPIPE_DRYRUN") != null && !System.getenv("CGPIPE_DRYRUN").equals("")) { dryrun = true; } JobRunner runner = null; try { // Load config values from global config. RootContext root = new RootContext(); loadInitFiles(root); // Load settings from environment variables. root.loadEnvironment(); // Set cmd-line arguments if (silent) { root.setOutputStream(null); } if (outputFilename != null) { outputStream = new PrintStream(new FileOutputStream(outputFilename)); root.setOutputStream(outputStream); } for (String k1 : confVals.keySet()) { log.info("config: " + k1 + " => " + confVals.get(k1).toString()); } root.update(confVals); root.set("cgpipe.procs", new VarInt(Runtime.getRuntime().availableProcessors())); // update the URL Source loader configs SourceLoader.updateRemoteHandlers(root.cloneString("cgpipe.remote")); // Now check for help, only after we've setup the remote handlers... if (showHelp) { try { Parser.showHelp(fname); System.exit(0); } catch (IOException e) { System.err.println("Unable to find pipeline: " + fname); System.exit(1); } } // Set the global config values // globalConfig.putAll(root.cloneValues()); // Parse the AST and run it Parser.exec(fname, root); // Load the job runner *after* we execute the script to capture any config changes runner = JobRunner.load(root, dryrun); // find a build-target, and submit the job(s) to a runner if (targets.size() > 0) { for (String target : targets) { log.debug("building: " + target); BuildTarget initTarget = root.build(target); if (initTarget != null) { runner.submitAll(initTarget, root); } else { System.out.println("CGPIPE ERROR: Unable to find target: " + target); } } } else { BuildTarget initTarget = root.build(); if (initTarget != null) { runner.submitAll(initTarget, root); // Leave this commented out - it should be allowed to run cgpipe scripts w/o a target defined (testing) // } else { // System.out.println("CGPIPE ERROR: Unable to find default target"); } } runner.done(); if (outputStream != null) { outputStream.close(); } } catch (ASTParseException | ASTExecException | RunnerException | FileNotFoundException e) { if (outputStream != null) { outputStream.close(); } if (runner != null) { runner.abort(); } if (e.getClass().equals(ExitException.class)) { System.exit(((ExitException) e).getReturnCode()); } System.out.println("CGPIPE ERROR " + e.getMessage()); if (verbosity > 0) { e.printStackTrace(); } System.exit(1); } }
From source file:Main.java
public static void saveFile(final String fileName, final String str) { try {//w w w .j av a 2 s . co m File f = new File(fileName); if (new File(f.getParent()).exists() == false) { f.getParentFile().mkdirs(); } f.createNewFile(); PrintStream p = new PrintStream(new FileOutputStream(f, false)); p.println(str); p.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(fileName); } }