|
|
@@ -24,6 +24,8 @@ |
|
|
package htsjdk.samtools;
|
|
|
|
|
|
|
|
|
+import htsjdk.samtools.util.CollectionUtil;
|
|
|
+import htsjdk.samtools.util.Log;
|
|
|
import htsjdk.samtools.util.StringLineReader;
|
|
|
|
|
|
import java.io.StringWriter;
|
|
|
@@ -47,14 +49,17 @@ |
|
|
public static final String SORT_ORDER_TAG = "SO";
|
|
|
public static final String GROUP_ORDER_TAG = "GO";
|
|
|
public static final String CURRENT_VERSION = "1.5";
|
|
|
- public static final Set<String> ACCEPTABLE_VERSIONS =
|
|
|
- new HashSet<String>(Arrays.asList("1.0", "1.3", "1.4", "1.5"));
|
|
|
+ public static final Set<String> ACCEPTABLE_VERSIONS = CollectionUtil.makeSet("1.0", "1.3", "1.4", "1.5");
|
|
|
|
|
|
+ private SortOrder sortOrder = null;
|
|
|
+ private GroupOrder groupOrder = null;
|
|
|
+
|
|
|
+ private static final Log log = Log.getInstance(SAMFileHeader.class);
|
|
|
/**
|
|
|
* These tags are of known type, so don't need a type field in the text representation.
|
|
|
*/
|
|
|
public static final Set<String> STANDARD_TAGS =
|
|
|
- new HashSet<String>(Arrays.asList(VERSION_TAG, SORT_ORDER_TAG, GROUP_ORDER_TAG));
|
|
|
+ new HashSet<>(Arrays.asList(VERSION_TAG, SORT_ORDER_TAG, GROUP_ORDER_TAG));
|
|
|
|
|
|
@Override
|
|
|
Set<String> getStandardTags() {
|
|
|
@@ -65,11 +70,11 @@ |
|
|
* Ways in which a SAM or BAM may be sorted.
|
|
|
*/
|
|
|
public enum SortOrder {
|
|
|
-
|
|
|
unsorted(null),
|
|
|
queryname(SAMRecordQueryNameComparator.class),
|
|
|
coordinate(SAMRecordCoordinateComparator.class),
|
|
|
- duplicate(SAMRecordDuplicateComparator.class); // NB: this is not in the SAM spec!
|
|
|
+ duplicate(SAMRecordDuplicateComparator.class), // NB: this is not in the SAM spec!
|
|
|
+ unknown(null);
|
|
|
|
|
|
private final Class<? extends SAMRecordComparator> comparator;
|
|
|
|
|
|
@@ -106,16 +111,14 @@ public SAMRecordComparator getComparatorInstance() { |
|
|
none, query, reference
|
|
|
}
|
|
|
|
|
|
- private List<SAMReadGroupRecord> mReadGroups =
|
|
|
- new ArrayList<SAMReadGroupRecord>();
|
|
|
- private List<SAMProgramRecord> mProgramRecords = new ArrayList<SAMProgramRecord>();
|
|
|
- private final Map<String, SAMReadGroupRecord> mReadGroupMap =
|
|
|
- new HashMap<String, SAMReadGroupRecord>();
|
|
|
- private final Map<String, SAMProgramRecord> mProgramRecordMap = new HashMap<String, SAMProgramRecord>();
|
|
|
+ private List<SAMReadGroupRecord> mReadGroups = new ArrayList<>();
|
|
|
+ private List<SAMProgramRecord> mProgramRecords = new ArrayList<>();
|
|
|
+ private final Map<String, SAMReadGroupRecord> mReadGroupMap = new HashMap<>();
|
|
|
+ private final Map<String, SAMProgramRecord> mProgramRecordMap = new HashMap<>();
|
|
|
private SAMSequenceDictionary mSequenceDictionary = new SAMSequenceDictionary();
|
|
|
- final private List<String> mComments = new ArrayList<String>();
|
|
|
+ final private List<String> mComments = new ArrayList<>();
|
|
|
private String textHeader;
|
|
|
- private final List<SAMValidationError> mValidationErrors = new ArrayList<SAMValidationError>();
|
|
|
+ private final List<SAMValidationError> mValidationErrors = new ArrayList<>();
|
|
|
|
|
|
public SAMFileHeader() {
|
|
|
setAttribute(VERSION_TAG, CURRENT_VERSION);
|
|
|
@@ -128,11 +131,11 @@ public SAMFileHeader(final SAMSequenceDictionary dict) { |
|
|
}
|
|
|
|
|
|
public String getVersion() {
|
|
|
- return (String) getAttribute("VN");
|
|
|
+ return getAttribute(VERSION_TAG);
|
|
|
}
|
|
|
|
|
|
public String getCreator() {
|
|
|
- return (String) getAttribute("CR");
|
|
|
+ return getAttribute("CR");
|
|
|
}
|
|
|
|
|
|
public SAMSequenceDictionary getSequenceDictionary() {
|
|
|
@@ -249,26 +252,47 @@ public SAMProgramRecord createProgramRecord() { |
|
|
}
|
|
|
|
|
|
public SortOrder getSortOrder() {
|
|
|
- final String so = getAttribute("SO");
|
|
|
- if (so == null || so.equals("unknown")) {
|
|
|
- return SortOrder.unsorted;
|
|
|
+ if (sortOrder == null) {
|
|
|
+ final String so = getAttribute(SORT_ORDER_TAG);
|
|
|
+ if (so == null) {
|
|
|
+ sortOrder = SortOrder.unsorted;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ return SortOrder.valueOf(so);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ log.warn("Found non conforming header SO tag: " + so + ". Treating as 'unknown'.");
|
|
|
+ sortOrder = SortOrder.unknown;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return SortOrder.valueOf((String) so);
|
|
|
+ return sortOrder;
|
|
|
}
|
|
|
|
|
|
public void setSortOrder(final SortOrder so) {
|
|
|
- setAttribute("SO", so.name());
|
|
|
+ sortOrder = so;
|
|
|
+ setAttribute(SORT_ORDER_TAG, so.name());
|
|
|
}
|
|
|
|
|
|
public GroupOrder getGroupOrder() {
|
|
|
- if (getAttribute("GO") == null) {
|
|
|
- return GroupOrder.none;
|
|
|
+ if (groupOrder == null) {
|
|
|
+ final String go = getAttribute(GROUP_ORDER_TAG);
|
|
|
+ if (go == null) {
|
|
|
+ groupOrder = GroupOrder.none;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ return GroupOrder.valueOf(go);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ log.warn("Found non conforming header GO tag: " + go + ". Treating as 'none'.");
|
|
|
+ groupOrder = GroupOrder.none;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return GroupOrder.valueOf((String)getAttribute("GO"));
|
|
|
+ return groupOrder;
|
|
|
}
|
|
|
|
|
|
public void setGroupOrder(final GroupOrder go) {
|
|
|
- setAttribute("GO", go.name());
|
|
|
+ groupOrder = go;
|
|
|
+ setAttribute(GROUP_ORDER_TAG, go.name());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -372,7 +396,7 @@ public String getSAMString() { |
|
|
public static class PgIdGenerator {
|
|
|
private int recordCounter;
|
|
|
|
|
|
- private final Set<String> idsThatAreAlreadyTaken = new HashSet<String>();
|
|
|
+ private final Set<String> idsThatAreAlreadyTaken = new HashSet<>();
|
|
|
|
|
|
public PgIdGenerator(final SAMFileHeader header) {
|
|
|
for (final SAMProgramRecord pgRecord : header.getProgramRecords()) {
|
|
|
@@ -400,7 +424,6 @@ public String getNonCollidingId(final String recordId) { |
|
|
idsThatAreAlreadyTaken.add(newId);
|
|
|
return newId;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|