// TODO: HANDLE THE CASE WHERE ANY OF THE START/ENDS ARE 0 private String[] parseBarcode(FastqSequence r) { if (this.IN_READ_NAME) { return parseBarcodeFromName(r.getName()); } else { return parseBarcodeFromSequence(r.getSequence()); } }
private void setBarcode(FastqSequence r, String[] bcInfo) { String compoundBC = bcInfo[0] + (bcInfo[1] != null && bcInfo[1].length() > 0 ? "_" + bcInfo[1] : ""); String newName = buildBarcodeFromReadName(r.getName(), compoundBC); r.setName(newName); /*String comment = r.getDescription(); if (comment == null || comment.trim().isEmpty()) { comment = compoundBC; } else { comment = comment + ":" + compoundBC; } r.setDescription(comment);*/ }
@Override protected int doWork() { sampleBarcodeCounts = new HashMap<String, Integer>(); randomBarcodeCounts = new HashMap<String, Integer>(); otherBarcodeCounts = new HashMap<String, Integer>(); sampleNonBarcodeCounts = new HashMap<String, Integer>(); randomNonBarcodeCounts = new HashMap<String, Integer>(); otherNonBarcodeCounts = new HashMap<String, Integer>(); HashMap<String, String> bcSampleMap = null; try { bcSampleMap = BCEvaluator.readBCMap(BC_SAMPLE_MAP); } catch (FileNotFoundException e) { System.out.println("Could not access the barcode sample mapping file"); e.printStackTrace(); return 1; } catch (IOException e) { System.out.println("Error accessing barcode sample mapping file"); e.printStackTrace(); return 1; } if (!OUTDIR.exists()) { try { System.out.println("Output directory does not exists " + OUTDIR + ", going to create it"); OUTDIR.mkdir(); } catch (SecurityException e) { System.out.println("Could not create output directory"); e.printStackTrace(); return 1; } } init(); FastqParser parser1 = new FastqParser(); FastqParser parser2 = new FastqParser(); HashMap<String, BufferedWriter[]> writerMap = null; BufferedWriter unassignedP1 = null; BufferedWriter unassignedP2 = null; try { try { parser1.start(FASTQ1); } catch (IOException e) { System.out.println("Error reading fastq file " + FASTQ1); e.printStackTrace(); return 1; } if (isPaired) { try { parser2.start(FASTQ2); } catch (IOException e) { System.out.println("Error reading fastq file " + FASTQ2); e.printStackTrace(); return 1; } } try { writerMap = buildWriterMap(bcSampleMap); unassignedP1 = new BufferedWriter(new FileWriter(OUTDIR + "/unassigned.P1.fq")); if (isPaired) { unassignedP2 = new BufferedWriter(new FileWriter(OUTDIR + "/unassigned.P2.fq")); } } catch (IOException e) { System.out.println("Could not create output files for barcode splitting"); e.printStackTrace(); return 1; } while (parser1.hasNext()) { FastqSequence r1 = parser1.next(); String[] bcInfo = parseBarcode(r1); // First position sample, second UMI, third OTHER if (bcInfo[0] == null) { logger.error( "Obtained a null barcode for read " + r1.getName() + " sequence " + r1.getSequence()); } BufferedWriter out1 = unassignedP1; BufferedWriter out2 = unassignedP2; String bc = getClosestBc(bcSampleMap, bcInfo[0]); if (bc != null) { // Need to update the BCInfo with the closest barcode once it is found bcInfo[0] = bc; BufferedWriter[] out = writerMap.get(bc); out1 = out[0]; if (isPaired) { out2 = out[1]; } setBarcode(r1, bcInfo); if (!IN_READ_NAME) { r1.excise(this.BC_START_POS, this.bcLength); } updateCounts(bcInfo); } else { updateNonCounts(bcInfo); } r1.write(out1); if (isPaired) { FastqSequence r2 = parser2.next(); if (bc != null) { setBarcode(r2, bcInfo); } // TODO: Handle when/if there is another barcode on R2 r2.write(out2); } if (bc != null) { updateCounts(bcInfo); } } writeReports(); } catch (IOException e) { System.out.println("Error writing records to file"); e.printStackTrace(); return 1; } finally { try { if (parser1 != null) parser1.close(); } catch (IOException e) { System.out.println("Could not close fastq1 file"); } if (isPaired) { try { parser2.close(); } catch (IOException e) { System.out.println("Could not close fastq2 file"); } } try { if (unassignedP1 != null) { unassignedP1.close(); } if (unassignedP2 != null) { unassignedP2.close(); } if (writerMap != null) { closeWriters(writerMap); } } catch (IOException e) { System.err.println("Error closing fastq writers"); e.printStackTrace(); } } return 0; }