Skip to content

Commit

Permalink
- responding to review comments.
Browse files Browse the repository at this point in the history
- using Strand class to parse the strand
  • Loading branch information
Yossi Farjoun committed Mar 19, 2019
1 parent 0caa68b commit d71944e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/util/BufferedLineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String readLine() {
/**
* Non-destructive one-character look-ahead.
*
* @return If not eof, the next character that would be read. If eof, -1.
* @return If not eof, the next character that would be read. If eof, {@value EOF_VALUE}.
*/
@Override
public int peek() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/htsjdk/samtools/util/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public interface LineReader extends Closeable {

// value to return in call to peek, if eof has been reached.
int EOF_VALUE = -1;

/**
Expand All @@ -45,7 +46,7 @@ public interface LineReader extends Closeable {

/**
* Non-destructive one-character look-ahead.
* @return If not eof, the next character that would be read. If eof, -1.
* @return If not eof, the next character that would be read. If eof, {@value EOF_VALUE}.
*/
int peek();

Expand Down
98 changes: 0 additions & 98 deletions src/main/java/htsjdk/tribble/AsciiSamFeatureCodec.java

This file was deleted.

62 changes: 41 additions & 21 deletions src/main/java/htsjdk/tribble/IntervalList/IntervalListCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@
import htsjdk.samtools.util.Interval;
import htsjdk.samtools.util.LineReader;
import htsjdk.samtools.util.Log;
import htsjdk.tribble.AsciiSamFeatureCodec;
import htsjdk.tribble.AsciiFeatureCodec;
import htsjdk.tribble.TribbleException;
import htsjdk.tribble.annotation.Strand;
import htsjdk.tribble.readers.LineIterator;

/**
* A tribble codec for IntervalLists.
*
* Also contains the parseing code for the non-tribble parsing of IntervalLists
* Also contains the parsing code for the non-tribble parsing of IntervalLists
*/

public class IntervalListCodec extends AsciiSamFeatureCodec<Interval> {
public class IntervalListCodec extends AsciiFeatureCodec<Interval> {

static final Log log = Log.getInstance(IntervalListCodec.class);

final char[] shortArray;
private SAMSequenceDictionary dictionary = null;

public IntervalListCodec() {
super(Interval.class);
shortArray = new char[1];
}

public IntervalListCodec(final SAMSequenceDictionary dict) {
Expand All @@ -69,7 +69,7 @@ private Interval parseIntervalString(final String line, final SAMSequenceDiction
// Make sure we have the right number of fields
final String[] fields = line.split("\t");
if (fields.length != 5) {
throw new SAMException("Invalid interval record contains " +
throw new TribbleException("Invalid interval record contains " +
fields.length + " fields: " + line);
}

Expand All @@ -93,21 +93,12 @@ private Interval parseIntervalString(final String line, final SAMSequenceDiction
". I'm afraid I cannot let you do that.");
}

final boolean negative;
switch (fields[STRAND_POS]) {
case "-":
negative = true;
break;
case "+":
negative = false;
break;
default:
throw new IllegalArgumentException("Invalid strand field: " + fields[STRAND_POS]);
}
Strand strand = Strand.decode(fields[STRAND_POS]);
if (strand==Strand.NONE) throw new IllegalArgumentException("Invalid strand field: " + fields[STRAND_POS]);

final String name = fields[NAME_POS];

final Interval interval = new Interval(seq, start, end, negative, name);
final Interval interval = new Interval(seq, start, end, strand==Strand.NEGATIVE, name);
final SAMSequenceRecord sequence = dict.getSequence(seq);
if (sequence == null) {
log.warn("Ignoring interval for unknown reference: " + interval);
Expand Down Expand Up @@ -138,11 +129,39 @@ public Interval decode(final String line) {
return parseIntervalString(line, dictionary);
}


// @Override
// public Object readActualHeader(LineReader lineReader) {
// final SAMTextHeaderCodec headerCodec = new SAMTextHeaderCodec();
//
// final SAMFileHeader header = headerCodec.decode(lineReader, "");
// dictionary = header.getSequenceDictionary();
// return header;
// }

@Override
public Object readActualHeader(LineReader lineReader) {
public Object readActualHeader(LineIterator lineIterator) {
final SAMTextHeaderCodec headerCodec = new SAMTextHeaderCodec();

final SAMFileHeader header = headerCodec.decode(lineReader, "");
final SAMFileHeader header = headerCodec.decode(new LineReader() {
int lineNo = 0;
@Override
public String readLine() {
lineNo++;
return lineIterator.next();
}
@Override
public int getLineNumber() {
return lineNo;
}
@Override
public int peek() {
return lineIterator.hasNext() ?
lineIterator.peek().charAt(0) :
LineReader.EOF_VALUE;
}
@Override
public void close() { }
}, "IntervalListCodec");
dictionary = header.getSequenceDictionary();
return header;
}
Expand All @@ -151,5 +170,6 @@ public Object readActualHeader(LineReader lineReader) {
public boolean canDecode(String s) {
return s.endsWith(".interval_list");
}

}

0 comments on commit d71944e

Please sign in to comment.