Skip to content

Commit

Permalink
Add support for Sam Header Readgroup Barcode field
Browse files Browse the repository at this point in the history
* adding support in SAMReadGroupRecord for the BC attribute
* this was added to the 1.6 spec
  • Loading branch information
lbergelson committed Nov 19, 2018
1 parent 698a4c3 commit 013be18
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/main/java/htsjdk/samtools/SAMReadGroupRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@

import htsjdk.samtools.util.Iso8601Date;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Header information about a read group.
Expand All @@ -51,6 +47,13 @@ public class SAMReadGroupRecord extends AbstractSAMHeaderRecord
public static final String PLATFORM_MODEL_TAG = "PM";
public static final String PLATFORM_UNIT_TAG = "PU";
public static final String READ_GROUP_SAMPLE_TAG = "SM";
public static final String BARCODE_TAG = "BC";

/**
* The recommended separator for the {@link #BARCODE_TAG} when there are multiple bar codes associated with this read group.
*/
public static final String BARCODE_SEPARATOR = "-";


/* Platform values for the @RG-PL tag */
public enum PlatformValue {
Expand All @@ -63,7 +66,7 @@ public enum PlatformValue {
new HashSet<String>(Arrays.asList(READ_GROUP_ID_TAG, SEQUENCING_CENTER_TAG, DESCRIPTION_TAG,
DATE_RUN_PRODUCED_TAG, FLOW_ORDER_TAG, KEY_SEQUENCE_TAG, LIBRARY_TAG,
PROGRAM_GROUP_TAG, PREDICTED_MEDIAN_INSERT_SIZE_TAG, PLATFORM_TAG, PLATFORM_MODEL_TAG,
PLATFORM_UNIT_TAG, READ_GROUP_SAMPLE_TAG));
PLATFORM_UNIT_TAG, READ_GROUP_SAMPLE_TAG, BARCODE_TAG));

public SAMReadGroupRecord(final String id) { mReadGroupId = id; }

Expand All @@ -90,6 +93,36 @@ public SAMReadGroupRecord(final String id, final SAMReadGroupRecord srcProgramRe
public String getPlatform() { return getAttribute(PLATFORM_TAG); }
public void setPlatform(final String platform) { setAttribute(PLATFORM_TAG, platform); }

/**
* @return the List of barcodes associated with this read group or null
*/
public List<String> getBarcodes() {
final String barcodeString = getAttribute(BARCODE_TAG);
if (barcodeString == null) {
return null;
} else if (barcodeString.isEmpty()) {
return Collections.emptyList();
} else {
return Arrays.asList(barcodeString.split(BARCODE_SEPARATOR));
}
}

/**
* Set the barcodes associated with this ReadGroup.
* Note that an input of null results in unsetting the attribute while an empty list is set as a tag with an empty value.
* @param barcodes a list of barcodes to associate with this read group
*/
public void setBarcodes(List<String> barcodes) {
if (barcodes == null) {
setAttribute(BARCODE_TAG, null);
} else {
if (barcodes.stream().anyMatch(String::isEmpty)) {
throw new IllegalArgumentException("A barcode must not be an empty String");
}
setAttribute(BARCODE_TAG, String.join(BARCODE_SEPARATOR, barcodes));
}
}

public Date getRunDate() {
final String dt = getAttribute(DATE_RUN_PRODUCED_TAG);
if (dt == null) return null;
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/htsjdk/samtools/SAMReadGroupRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

Expand Down Expand Up @@ -145,4 +148,24 @@ public void testEqualsAndHashcode(final SAMReadGroupRecord rg, final Object othe
}
}

@DataProvider
public Object[][] getBarcodes() {
return new Object[][] {
{null, null},
{Collections.emptyList(), ""},
{Collections.singletonList("aa"), "aa"},
{Arrays.asList("aa", "ac"), "aa-ac"},
{Arrays.asList("aa", "ca", "gg"), "aa-ca-gg"}
};
}

@Test(dataProvider = "getBarcodes")
public void testGetAndSetBarcodes(List<String> barcodes, String encoded){
final SAMReadGroupRecord readGroup = new SAMReadGroupRecord("ReadGroup");
Assert.assertNull(readGroup.getBarcodes());
Assert.assertNull(readGroup.getAttribute(SAMReadGroupRecord.BARCODE_TAG));
readGroup.setBarcodes(barcodes);
Assert.assertEquals(readGroup.getBarcodes(), barcodes);
Assert.assertEquals(readGroup.getAttribute(SAMReadGroupRecord.BARCODE_TAG), encoded);
}
}

0 comments on commit 013be18

Please sign in to comment.