/** * Public constructor. * * @param file the workflow file * @throws IOException if an error occurs while opening the file */ public CommandWorkflowParser(final DataFile file) throws IOException { this(file.open()); }
@Override public void merge(final Iterator<DataFile> inFileIterator, DataFile outFile) throws IOException { final Multiset<String> counts = HashMultiset.create(); final Set<String> emptyCounts = new HashSet<>(); while (inFileIterator.hasNext()) { // Get input file final DataFile inFile = inFileIterator.next(); EoulsanLogger.getLogger().info("Merge " + inFile.getName() + " to " + outFile.getName()); boolean first = true; try (BufferedReader reader = new BufferedReader(new InputStreamReader(inFile.open()))) { String line = null; while ((line = reader.readLine()) != null) { // Do no handle header if (first) { first = false; continue; } final int tabPos = line.indexOf('\t'); // Do not handle empty or invalid lines if (tabPos == -1) { continue; } try { final String id = line.substring(0, tabPos).trim(); final int count = Integer.parseInt(line.substring(tabPos).trim()); if (count == 0) { emptyCounts.add(id); } counts.add(id, count); } catch (NumberFormatException e) { // Do not handle parsing errors } } } } // Write the result file try (Writer writer = new OutputStreamWriter(outFile.create())) { writer.write(ExpressionSplitter.EXPRESSION_FILE_HEADER); // Write the non empty counts for (Multiset.Entry<String> e : counts.entrySet()) { final String id = e.getElement(); // Remove the id from empty counts emptyCounts.remove(id); // Write the entry writer.write(id + '\t' + e.getCount() + '\n'); } // Write the empty counts for (String id : emptyCounts) { writer.write(id + "\t0\n"); } } }