Skip to content

Commit

Permalink
- make interval operations scale better (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yossi Farjoun authored and cmnbroad committed May 13, 2019
1 parent 7ce3636 commit ae49710
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 147 deletions.
13 changes: 6 additions & 7 deletions src/main/java/htsjdk/samtools/SAMSequenceDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public SAMSequenceRecord getSequence(final int sequenceIndex) {
}

/**
* @return The index for the given sequence name, or -1 if the name is not found.
* @return The index for the given sequence name, or {@value SAMSequenceRecord#UNAVAILABLE_SEQUENCE_INDEX} if the name is not found.
*/
public int getSequenceIndex(final String sequenceName) {
final SAMSequenceRecord record = mSequenceMap.get(sequenceName);
if (record == null) {
return -1;
return UNAVAILABLE_SEQUENCE_INDEX;
}
return record.getSequenceIndex();
}
Expand All @@ -124,11 +124,10 @@ public int size() {
* @return The sum of the lengths of the sequences in this dictionary
*/
public long getReferenceLength() {
long len = 0L;
for (final SAMSequenceRecord seq : getSequences()) {
len += seq.getSequenceLength();
}
return len;
return getSequences()
.stream()
.mapToLong(SAMSequenceRecord::getSequenceLength)
.sum();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/htsjdk/samtools/SAMSequenceRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
public class SAMSequenceRecord extends AbstractSAMHeaderRecord implements Cloneable
{
public static final long serialVersionUID = 1L; // AbstractSAMHeaderRecord implements Serializable
public final static int UNAVAILABLE_SEQUENCE_INDEX = -1;
private final String mSequenceName; // Value must be interned() if it's ever set/modified
private int mSequenceIndex = -1;
private int mSequenceIndex = UNAVAILABLE_SEQUENCE_INDEX;
private int mSequenceLength = 0;
public static final String SEQUENCE_NAME_TAG = "SN";
public static final String SEQUENCE_LENGTH_TAG = "LN";
Expand Down Expand Up @@ -196,7 +197,7 @@ public static String truncateSequenceName(final String sequenceName) {
int truncateAt = sequenceName.length();
for (final char c : WHITESPACE_CHARS) {
int index = sequenceName.indexOf(c);
if (index != -1 && index < truncateAt) {
if (index != UNAVAILABLE_SEQUENCE_INDEX && index < truncateAt) {
truncateAt = index;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/htsjdk/samtools/util/Interval.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,9 @@ public Interval pad(final int left, final int right) {
* Counts the total number of bases a collection of intervals.
*/
public static long countBases(final Collection<Interval> intervals) {
long total = 0;
for (final Interval i : intervals) {
total += i.length();
}

return total;
return intervals.stream()
.mapToInt(Interval::length)
.sum();
}

/**
Expand Down
Loading

0 comments on commit ae49710

Please sign in to comment.