/**
* For each line in the LIBRARY_PARAMS file create a SamFileWriter and put it in the barcodeSamWriterMap map, where
* the key to the map is the concatenation of all barcodes in order for the given line
*/
private void populateWritersFromLibraryParams() {
final TabbedTextFileWithHeaderParser libraryParamsParser = new TabbedTextFileWithHeaderParser(LIBRARY_PARAMS);
final Set<String> expectedColumnLabels = CollectionUtil.makeSet("OUTPUT", "SAMPLE_ALIAS", "LIBRARY_NAME");
final List<String> barcodeColumnLabels = new ArrayList<String>();
if (readStructure.barcodes.length() == 1) {
//For the single barcode read case, the barcode label name can either by BARCODE or BARCODE_1
if (libraryParamsParser.hasColumn("BARCODE")) {
barcodeColumnLabels.add("BARCODE");
} else if (libraryParamsParser.hasColumn("BARCODE_1")) {
barcodeColumnLabels.add("BARCODE_1");
} else {
throw new PicardException("LIBRARY_PARAMS(BARCODE_PARAMS) file " + LIBRARY_PARAMS + " does not have column BARCODE or BARCODE_1.");
}
} else {
for (int i = 1; i <= readStructure.barcodes.length(); i++) {
barcodeColumnLabels.add("BARCODE_" + i);
}
}
expectedColumnLabels.addAll(barcodeColumnLabels);
final Set<String> rgTagColumns = findAndFilterExpectedColumns(libraryParamsParser.columnLabels(), expectedColumnLabels);
checkRgTagColumns(rgTagColumns);
for (final TabbedTextFileWithHeaderParser.Row row : libraryParamsParser) {
List<String> barcodeValues = null;
if (barcodeColumnLabels.size() > 0) {
barcodeValues = new ArrayList<String>();
for (final String barcodeLabel : barcodeColumnLabels) {
barcodeValues.add(row.getField(barcodeLabel));
}
}
final String key = (barcodeValues == null || barcodeValues.contains("N")) ? null : StringUtil.join("", barcodeValues);
if (barcodeSamWriterMap.containsKey(key)) { //This will catch the case of having more than 1 line in a non-barcoded LIBRARY_PARAMS file
throw new PicardException("Row for barcode " + key + " appears more than once in LIBRARY_PARAMS or BARCODE_PARAMS file " +
LIBRARY_PARAMS);
}
final Map<String, String> samHeaderParams = buildSamHeaderParameters(barcodeValues);
for (final String tagName : rgTagColumns) {
samHeaderParams.put(tagName, row.getField(tagName));
}
final SAMFileWriterWrapper writer = buildSamFileWriter(new File(row.getField("OUTPUT")),
row.getField("SAMPLE_ALIAS"), row.getField("LIBRARY_NAME"), samHeaderParams);
barcodeSamWriterMap.put(key, writer);
}
if (barcodeSamWriterMap.isEmpty()) {
throw new PicardException("LIBRARY_PARAMS(BARCODE_PARAMS) file " + LIBRARY_PARAMS + " does have any data rows.");
}
libraryParamsParser.close();
}