Skip to content

Commit

Permalink
Extend file format determination code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Oct 9, 2024
1 parent 15e8c2a commit 4778137
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/broad/igv/track/FileFormatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import htsjdk.samtools.util.BlockCompressedInputStream;
import org.broad.igv.ucsc.twobit.UnsignedByteBuffer;
import org.broad.igv.ucsc.twobit.UnsignedByteBufferImpl;
import org.broad.igv.util.ParsingUtils;
import org.broad.igv.util.stream.IGVSeekableStreamFactory;

import java.io.*;
Expand Down Expand Up @@ -89,6 +90,27 @@ public static String determineFormat(String path) throws IOException {
if (firstLine.startsWith("##gff-version")) {
return "gff";
}
if(firstLine.startsWith("##fileformat=")) {
return firstLine.substring(13); // Non standard extension of VCF convention
}

// Read maximum of first 100 lines searching for format indication.
int n = 0;
String nextLine;
while((nextLine = reader.readLine()) != null && n++ < 100) {
if(nextLine.startsWith("#")) continue;
if(nextLine.startsWith("track")) {
TrackProperties properties = new TrackProperties();
ParsingUtils.parseTrackLine(nextLine, properties);
if(properties.getFormat() != null) {
return properties.getFormat();
}
}
if(nextLine.startsWith("fixedStep") || nextLine.startsWith("variableStep")) {
return "wig";
}
}

if (maybeSampleInfo(bytes)) {
return "sampleinfo";
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/broad/igv/track/TrackProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public enum BaseCoord {

private String coverageURL;

/**
* Non-standard track field to indicate file format
*/
private String format;

/**
* Track attributes (meta data)
*/
Expand All @@ -153,6 +158,13 @@ public TrackProperties() {

}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public void setTrackLine(String trackLine) {
this.trackLine = trackLine;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/broad/igv/util/ParsingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,18 @@ public static boolean parseTrackLine(String nextLine, TrackProperties trackPrope
String key = kv.get(0).toLowerCase().trim();
String value = kv.get(1).replaceAll("\"", "");

if (key.equals("coords")) {
if(key.equals("format")) {
trackProperties.setFormat(value);
}
else if (key.equals("coords")) {
if (value.equals("0")) {
trackProperties.setBaseCoord(TrackProperties.BaseCoord.ZERO);
} else if (value.equals("1")) {
trackProperties.setBaseCoord(TrackProperties.BaseCoord.ONE);
}

}
if (key.equals("name")) {
else if (key.equals("name")) {
trackProperties.setName(value);
//dhmay adding name check for TopHat junctions files. graphType is also checked.
if (value.equals("junctions")) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/broad/igv/track/FileFormatUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ public void testDetermineFormat() throws Exception {
String sampleInfoFile = "http://igvdata.broadinstitute.org/data/hg18/tcga/gbm/gbmsubtypes/sampleTable.txt.gz";
format = FileFormatUtils.determineFormat(sampleInfoFile);
assertEquals("sampleinfo", format);

String wigFile = TestUtils.DATA_DIR + "wig/dm3_var_sample.wig";
format = FileFormatUtils.determineFormat(wigFile);
assertEquals("wig", format);
}
}

0 comments on commit 4778137

Please sign in to comment.