List of usage examples for java.io PrintStream checkError
public boolean checkError()
From source file:idgs.IdgsCliDriver.java
/** * override super method process to handle idgs command */// w w w . j a va 2 s.co m @Override public int processCmd(String cmd) { CliSessionState ss = (CliSessionState) SessionState.get(); String cmd_trimmed = cmd.trim(); String[] tokens = cmd_trimmed.split("\\s+"); String cmd_1 = cmd_trimmed.substring(tokens[0].length()).trim(); int ret = 0; if (cmd_trimmed.toLowerCase().equals("quit") || cmd_trimmed.toLowerCase().equals("exit") || tokens[0].equalsIgnoreCase("source") || cmd_trimmed.startsWith("!") || tokens[0].toLowerCase().equals("list")) { super.processCmd(cmd); } else { HiveConf hconf = (HiveConf) conf; try { CommandProcessor proc = CommandProcessorFactory.get(tokens, hconf); if (proc != null) { // Spark expects the ClassLoader to be an URLClassLoader. // In case we're using something else here, wrap it into an // URLCLassLaoder. if (System.getenv("TEST_WITH_ANT") == "1") { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[1], cl)); } if (proc instanceof Driver) { // There is a small overhead here to create a new instance of // SharkDriver for every command. But it saves us the hassle of // hacking CommandProcessorFactory. Driver qp = null; try { // ##### using hive_idgs driver qp = (IdgsConfVars.getVar(conf, IdgsConfVars.EXEC_MODE) == "idgs") ? new IdgsDriver(hconf) : Driver.class.newInstance(); } catch (Exception e) { e.printStackTrace(); } LOG.info("Execution Mode: " + IdgsConfVars.getVar(conf, IdgsConfVars.EXEC_MODE)); qp.init(); PrintStream out = ss.out; long start = System.currentTimeMillis(); if (ss.getIsVerbose()) { out.println(cmd); } ret = qp.run(cmd).getResponseCode(); if (ret != 0) { qp.close(); return ret; } boolean isPrint = IdgsConfVars.getBoolVar(conf, IdgsConfVars.PRINT_RESULT); List<Object[]> res = new ArrayList<Object[]>(); if (isPrint) { if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CLI_PRINT_HEADER)) { // Print the column names. List<FieldSchema> fieldSchemas = qp.getSchema().getFieldSchemas(); if (fieldSchemas != null) { for (FieldSchema fieldSchema : fieldSchemas) { out.print("header" + fieldSchema.getName() + "\t"); } out.println(); } } } long printTime = 0; int counter = 0; SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { long s = System.currentTimeMillis(); while (qp.getResults(res)) { for (Object[] row : res) { if (isPrint) { for (Object v : row) { if (v != null) { if (v instanceof byte[]) { out.print(new String((byte[]) v)); } else if (v instanceof Timestamp) { out.print(timestampFormat.format((Timestamp) v)); } else if (v instanceof Date) { out.print(dateFormat.format((Date) v)); } else { out.print(v); } } else { out.print(v); } out.print("\t"); } out.println(); } } counter += res.size(); res.clear(); if (out.checkError()) { break; } } printTime = System.currentTimeMillis() - s; } catch (IOException e) { console.printError( "Failed with exception " + e.getClass().getName() + ":" + e.getMessage(), "\n" + org.apache.hadoop.util.StringUtils.stringifyException(e)); ret = 1; } int cret = qp.close(); if (ret == 0) { ret = cret; } long end = System.currentTimeMillis(); double timeTaken = (end - start) / 1000.0; console.printInfo("Time taken: " + timeTaken + " seconds, Fetched: " + counter + " row(s)"); // Destroy the driver to release all the locks. if (qp instanceof IdgsDriver) { LOG.info("Time taken: " + timeTaken + " seconds, Fetched: " + counter + " row(s)"); LOG.info("Compile time taken: " + (((IdgsDriver) qp).getCompileTime() / 1000.0) + " seconds"); LOG.info("Task run time taken: " + (((IdgsDriver) qp).getTaskRunTime() / 1000.0) + " seconds"); LOG.info("Print time taken: " + (printTime / 1000.0) + " seconds"); qp.destroy(); } } else { if (ss.getIsVerbose()) { ss.out.println(tokens[0] + " " + cmd_1); } ret = proc.run(cmd_1).getResponseCode(); } } } catch (CommandNeedRetryException ex) { LOG.error("Execute command " + cmd + " error.", ex); console.printInfo("Retry query with a different approach..."); } catch (Exception ex) { LOG.error("Execute command " + cmd + " error.", ex); console.printInfo("Execute command error, caused " + ex.getMessage() + "."); ret = 1; } } return ret; }
From source file:org.thdl.tib.input.TibetanConverter.java
/** Reads from in, closes in, converts (or finds some/all non-TM/TMW), writes the result to out, does not close out. The action taken depends on ct, which must be one of a set number of strings -- see the code. Uses short error and warning messages if shortMessages is true; gives no warnings or many warnings depending on warningLevel. Returns an appropriate return code so that TibetanConverter's usage message is honored. *//*from www . j a v a 2s . co m*/ static int reallyConvert(InputStream in, PrintStream out, String ct, String warningLevel, boolean shortMessages, boolean colors) { if (UNI_TO_WYLIE_TEXT == ct || WYLIE_TO_ACIP_TEXT == ct || ACIP_TO_WYLIE_TEXT == ct) { try { /*String uniText; { // TODO(dchandler): use, here and elsewhere in the // codebase, // org.apache.commons.io.IOUtils.toString(InputStream, // encoding) StringBuffer s = new StringBuffer(); char ch[] = new char[8192]; BufferedReader bin = new BufferedReader(new InputStreamReader(in, "UTF-8")); int amt; while (-1 != (amt = bin.read(ch))) { s.append(ch, 0, amt); } bin.close(); uniText = s.toString(); } StringBuffer errors = new StringBuffer(); // TODO(dchandler): DLC: use human-friendly EWTS, not // computer-friendly! String ewtsText = Converter.convertToEwtsForComputers(uniText, errors); // TODO(dchandler): is 51 the right choice? return (errors.length() > 0) ? 51 : 0;*/ BasicTibetanTranscriptionConverter bc = null; if (UNI_TO_WYLIE_TEXT == ct) bc = new BasicTibetanTranscriptionConverter( new BufferedReader(new InputStreamReader(in, "UTF16")), new PrintWriter(out)); else bc = new BasicTibetanTranscriptionConverter(new BufferedReader(new InputStreamReader(in)), new PrintWriter(out)); bc.run(ct); return 0; } catch (IOException e) { // TODO(dchandler): print it? where to? return 48; } } else if (ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct || WYLIE_TO_UNI_TEXT == ct || WYLIE_TO_TMW == ct) { try { ArrayList al = ((ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct) ? (TTraits) ACIPTraits.instance() : (TTraits) EWTSTraits.instance()).scanner().scanStream( in, null, ThdlOptions.getIntegerOption((ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct) ? "thdl.most.errors.a.tibetan.acip.document.can.have" : "thdl.most.errors.a.tibetan.ewts.document.can.have", 1000 - 1), shortMessages, warningLevel); if (null == al) return 47; boolean embeddedWarnings = (warningLevel != "None"); boolean hasWarnings[] = new boolean[] { false }; if (ACIP_TO_UNI_TEXT == ct || WYLIE_TO_UNI_TEXT == ct) { if (!TConverter.convertToUnicodeText( (WYLIE_TO_UNI_TEXT == ct) ? (TTraits) EWTSTraits.instance() : (TTraits) ACIPTraits.instance(), al, out, null, null, hasWarnings, embeddedWarnings, warningLevel, shortMessages)) return 46; } else { if (!TConverter.convertToTMW( (WYLIE_TO_TMW == ct) ? (TTraits) EWTSTraits.instance() : (TTraits) ACIPTraits.instance(), al, out, null, null, hasWarnings, embeddedWarnings, warningLevel, shortMessages, colors)) return 46; } if (embeddedWarnings && hasWarnings[0]) return 45; else return 0; } catch (IOException e) { // TODO(dchandler): print it? where to? return 48; } } else { TibetanDocument tdoc = new TibetanDocument(); { SimpleAttributeSet ras = new SimpleAttributeSet(); StyleConstants.setFontFamily(ras, ThdlOptions.getStringOption("thdl.default.roman.font.face", "Serif")); StyleConstants.setFontSize(ras, ThdlOptions.getIntegerOption("thdl.default.roman.font.size", 14)); tdoc.setRomanAttributeSet(ras); } try { // Read in the rtf file. if (debug) System.err.println("Start: reading in old RTF file"); if (!ThdlOptions.getBooleanOption("thdl.do.not.fix.rtf.hex.escapes")) in = new RTFFixerInputStream(in); (new RTFEditorKit()).read(in, tdoc, 0); if (debug) System.err.println("End : reading in old RTF file"); } catch (Exception e) { out.println("TibetanConverter:\n" + rtfErrorMessage); return 3; } try { in.close(); } catch (IOException e) { // silently ignore; we don't care about the input so much... ThdlDebug.noteIffyCode(); } if (FIND_ALL_NON_TMW == ct) { // 0, -1 is the entire document. int exitCode = tdoc.findAllNonTMWCharacters(0, -1, out); if (out.checkError()) exitCode = 41; return exitCode; } else if (FIND_SOME_NON_TMW == ct) { // 0, -1 is the entire document. int exitCode = tdoc.findSomeNonTMWCharacters(0, -1, out); if (out.checkError()) exitCode = 41; return exitCode; } else if (FIND_SOME_NON_TM == ct) { // 0, -1 is the entire document. int exitCode = tdoc.findSomeNonTMCharacters(0, -1, out); if (out.checkError()) exitCode = 41; return exitCode; } else if (FIND_ALL_NON_TM == ct) { // 0, -1 is the entire document. int exitCode = tdoc.findAllNonTMCharacters(0, -1, out); if (out.checkError()) exitCode = 41; return exitCode; } else { // conversion {to Wylie or TM} mode // Fix curly braces in the entire document if the input is TMW: if (TM_TO_TMW != ct) { // DLC make me optional if (debug) System.err.println("Start: solving curly brace problem"); tdoc.replaceTahomaCurlyBracesAndBackslashes(0, -1); if (debug) System.err.println("End : solving curly brace problem"); } int exitCode = 0; ThdlDebug.verify(((TMW_TO_TM == ct) ? 1 : 0) + ((TMW_TO_SAME_TMW == ct) ? 1 : 0) + ((TMW_TO_UNI == ct) ? 1 : 0) + ((TM_TO_TMW == ct) ? 1 : 0) + ((TMW_TO_ACIP == ct) ? 1 : 0) + ((TMW_TO_ACIP_TEXT == ct) ? 1 : 0) + ((TMW_TO_WYLIE == ct) ? 1 : 0) + ((TMW_TO_WYLIE_TEXT == ct) ? 1 : 0) == 1); long numAttemptedReplacements[] = new long[] { 0 }; if (TMW_TO_SAME_TMW == ct) { // Identity conversion for testing if (tdoc.identityTmwToTmwConversion(0, tdoc.getLength(), numAttemptedReplacements)) { exitCode = 50; } } else if (TMW_TO_WYLIE == ct || TMW_TO_WYLIE_TEXT == ct) { // Convert to THDL Wylie: if (!tdoc.toWylie(0, tdoc.getLength(), numAttemptedReplacements)) { exitCode = 44; } } else if (TMW_TO_ACIP == ct || TMW_TO_ACIP_TEXT == ct) { // Convert to ACIP: if (!tdoc.toACIP(0, tdoc.getLength(), numAttemptedReplacements)) { exitCode = 49; } } else if (TMW_TO_UNI == ct) { StringBuffer errors = new StringBuffer(); // Convert to Unicode: if (tdoc.convertToUnicode(0, tdoc.getLength(), errors, ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern(), numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } } else if (TM_TO_TMW == ct) { StringBuffer errors = new StringBuffer(); // Convert to TibetanMachineWeb: if (tdoc.convertToTMW(0, tdoc.getLength(), errors, numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } } else { ThdlDebug.verify(TMW_TO_TM == ct); StringBuffer errors = new StringBuffer(); // Convert to TibetanMachine: if (tdoc.convertToTM(0, tdoc.getLength(), errors, numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } } // Write to standard output the result: if (TMW_TO_WYLIE_TEXT == ct || TMW_TO_ACIP_TEXT == ct) { try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); tdoc.writeTextOutput(bw); bw.flush(); } catch (IOException e) { exitCode = 40; } } else { try { tdoc.writeRTFOutputStream(out); } catch (IOException e) { exitCode = 40; } } if (out.checkError()) exitCode = 41; if (numAttemptedReplacements[0] < 1) exitCode = 43; return exitCode; } } }
From source file:com.github.lindenb.jvarkit.tools.vcfcmp.VcfCompareCallers.java
@Override public Collection<Throwable> call() throws Exception { htsjdk.samtools.util.IntervalTreeMap<Boolean> capture = null; PrintWriter exampleWriter = null; XMLStreamWriter exampleOut = null; PrintStream pw = null; VcfIterator vcfInputs[] = new VcfIterator[] { null, null }; VCFHeader headers[] = new VCFHeader[] { null, null }; final List<String> args = getInputFiles(); try {// w ww .j a v a2s . co m if (args.size() == 1) { LOG.info("Reading from stdin and " + args.get(0)); vcfInputs[0] = VCFUtils.createVcfIteratorStdin(); vcfInputs[1] = VCFUtils.createVcfIterator(args.get(0)); } else if (args.size() == 2) { LOG.info("Reading from stdin and " + args.get(0) + " and " + args.get(1)); vcfInputs[0] = VCFUtils.createVcfIterator(args.get(0)); vcfInputs[1] = VCFUtils.createVcfIterator(args.get(1)); } else { return wrapException(getMessageBundle("illegal.number.of.arguments")); } if (super.captureFile != null) { LOG.info("Reading " + super.captureFile); capture = super.readBedFileAsBooleanIntervalTreeMap(super.captureFile); } for (int i = 0; i < vcfInputs.length; ++i) { headers[i] = vcfInputs[i].getHeader(); } /* dicts */ final SAMSequenceDictionary dict0 = headers[0].getSequenceDictionary(); final SAMSequenceDictionary dict1 = headers[1].getSequenceDictionary(); final Comparator<VariantContext> ctxComparator; if (dict0 == null && dict1 == null) { ctxComparator = VCFUtils.createChromPosRefComparator(); } else if (dict0 != null && dict1 != null) { if (!SequenceUtil.areSequenceDictionariesEqual(dict0, dict1)) { return wrapException(getMessageBundle("not.the.same.sequence.dictionaries")); } ctxComparator = VCFUtils.createTidPosRefComparator(dict0); } else { return wrapException(getMessageBundle("not.the.same.sequence.dictionaries")); } /* samples */ Set<String> samples0 = new HashSet<>(headers[0].getSampleNamesInOrder()); Set<String> samples1 = new HashSet<>(headers[1].getSampleNamesInOrder()); Set<String> samples = new TreeSet<>(samples0); samples.retainAll(samples1); if (samples.size() != samples0.size() || samples.size() != samples1.size()) { LOG.warn("Warning: Not the same samples set. Using intersection of both lists."); } if (samples.isEmpty()) { return wrapException("No common samples"); } Map<String, Counter<Category>> sample2info = new HashMap<String, Counter<Category>>(samples.size()); for (String sampleName : samples) { sample2info.put(sampleName, new Counter<Category>()); } if (super.exampleFile != null) { exampleWriter = new PrintWriter(exampleFile, "UTF-8"); XMLOutputFactory xof = XMLOutputFactory.newFactory(); exampleOut = xof.createXMLStreamWriter(exampleWriter); exampleOut.writeStartDocument("UTF-8", "1.0"); exampleOut.writeStartElement("compare-callers"); } SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(dict0); VariantContext buffer[] = new VariantContext[vcfInputs.length]; VariantContext prev[] = new VariantContext[vcfInputs.length]; for (;;) { VariantContext smallest = null; //refill buffer for (int i = 0; i < vcfInputs.length; ++i) { if (buffer[i] == null && vcfInputs[i] != null) { if (vcfInputs[i].hasNext()) { buffer[i] = vcfInputs[i].peek(); /* check data are sorted */ if (prev[i] != null && ctxComparator.compare(prev[i], buffer[i]) > 0) { return wrapException("Input " + (i + 1) + "/2 is not sorted" + (((i == 0 && dict0 == null) || (i == 1 && dict1 == null)) ? "on chrom/pos/ref" : "on sequence dictionary") + ". got\n" + buffer[i] + "\nafter\n" + prev[i]); } } else { vcfInputs[i].close(); vcfInputs[i] = null; } } if (buffer[i] != null) { if (smallest == null || ctxComparator.compare(buffer[i], smallest) < 0) { smallest = buffer[i]; } } } if (smallest == null) break; VariantContext ctx0 = null; VariantContext ctx1 = null; Interval interval = null; if (buffer[0] != null && ctxComparator.compare(buffer[0], smallest) == 0) { prev[0] = progress.watch(vcfInputs[0].next()); ctx0 = prev[0]; buffer[0] = null; interval = new Interval(ctx0.getContig(), ctx0.getStart(), ctx0.getEnd()); } if (buffer[1] != null && ctxComparator.compare(buffer[1], smallest) == 0) { prev[1] = progress.watch(vcfInputs[1].next()); ctx1 = prev[1]; buffer[1] = null; interval = new Interval(ctx1.getContig(), ctx1.getStart(), ctx1.getEnd()); } boolean in_capture = true; if (capture != null && interval != null) { in_capture = capture.containsOverlapping(interval); } for (final String sampleName : sample2info.keySet()) { final Counter<Category> sampleInfo = sample2info.get(sampleName); Genotype g0 = (ctx0 == null ? null : ctx0.getGenotype(sampleName)); Genotype g1 = (ctx1 == null ? null : ctx1.getGenotype(sampleName)); if (g0 != null && (g0.isNoCall() || !g0.isAvailable())) g0 = null; if (g1 != null && (g1.isNoCall() || !g1.isAvailable())) g1 = null; if (g0 == null && g1 == null) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.both_missing); continue; } else if (g0 != null && g1 == null) { if (!in_capture) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.off_target_only_1); continue; } watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_1); if (ctx0.isIndel()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_1_indel); } else if (ctx0.isSNP()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_1_snp); } continue; } else if (g0 == null && g1 != null) { if (!in_capture) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.off_target_only_2); continue; } watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_2); if (ctx1.isIndel()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_2_indel); } else if (ctx1.isSNP()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_2_snp); } continue; } else { if (!in_capture) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.off_target_both); continue; } watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.common_context); if (ctx0.isIndel() && ctx1.isIndel()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.common_context_indel); } else if (ctx0.isSNP() && ctx1.isSNP()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.common_context_snp); } if ((ctx0.hasID() && !ctx1.hasID()) || (!ctx0.hasID() && ctx1.hasID()) || (ctx0.hasID() && ctx1.hasID() && !ctx0.getID().equals(ctx1.getID()))) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.common_context_discordant_id); } if (g0.sameGenotype(g1)) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_and_same); if (g0.isHomRef()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_and_same_hom_ref); } if (g0.isHomVar()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_and_same_hom_var); } else if (g0.isHet()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_and_same_het); } } else { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant); if (g0.isHom() && g1.isHet()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant_hom1_het2); } else if (g0.isHet() && g1.isHom()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant_het1_hom2); } else if (g0.isHom() && g1.isHom()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant_hom1_hom2); } else if (g0.isHet() && g1.isHet()) { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant_het1_het2); } else { watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_but_discordant_others); } } } } } progress.finish(); pw = openFileOrStdoutAsPrintStream(); pw.print("#Sample"); for (Category c : Category.values()) { pw.print('\t'); pw.print(c.name()); } pw.println(); for (String sample : sample2info.keySet()) { Counter<Category> count = sample2info.get(sample); pw.print(sample); for (Category c : Category.values()) { pw.print('\t'); pw.print(count.count(c)); } pw.println(); if (pw.checkError()) break; } pw.flush(); if (exampleOut != null) { exampleOut.writeEndElement(); exampleOut.writeEndDocument(); exampleOut.flush(); exampleOut.close(); } return RETURN_OK; } catch (Exception err) { return wrapException(err); } finally { if (getOutputFile() != null) CloserUtil.close(pw); CloserUtil.close(exampleWriter); } }