Skip to content

Commit

Permalink
Add support for searching by bb extra indexes, including trix
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Nov 28, 2023
1 parent 564b683 commit 7df59de
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 94 deletions.
10 changes: 4 additions & 6 deletions src/main/java/org/broad/igv/feature/genome/Genome.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void addTracks(GenomeConfig config) {
res.setVisibilityWindow(vw);
}

if(trackConfig.searchTrix != null) {
if (trackConfig.searchTrix != null) {
res.setTrixURL(trackConfig.searchTrix);
}

Expand Down Expand Up @@ -355,7 +355,7 @@ public Genome(String id, List<Chromosome> chromosomes) {
* Return the canonical chromosome name for the (possibly) alias
*
* @param str chromosome or alias name
* @return the canonical chromsoome name -- i.e. chromosome name as defined by the reference sequence
* @return the canonical chromsoome name -if the chromosome exists.
*/
public String getCanonicalChrName(String str) {
if (str == null) {
Expand All @@ -365,9 +365,7 @@ public String getCanonicalChrName(String str) {
} else if (chromAliasSource != null) {
try {
ChromAlias aliasRecord = chromAliasSource.search(str);
if (aliasRecord == null) {
return str;
} else {
if (aliasRecord != null) {
String chr = aliasRecord.getChr();
chrAliasCache.put(str, chr);
return chr;
Expand Down Expand Up @@ -491,9 +489,9 @@ public Chromosome getChromosome(String name) {
if (chromosomeMap.containsKey(chrName)) {
return chromosomeMap.get(chrName);
} else {
int idx = this.chromosomeMap.size();
int length = this.sequence.getChromosomeLength(chrName);
if (length > 0) {
int idx = this.chromosomeMap.size();
Chromosome chromosome = new Chromosome(idx, chrName, length);
chromosomeMap.put(chrName, chromosome);
return chromosome;
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/broad/igv/feature/genome/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,26 @@
*/
public interface Sequence {

byte[] getSequence(String chr, int start, int end);
/**
* Return the sequence for the given range. If sequence named "seq" does not exist returns null.
* @param chr
* @param start
* @param end
* @return The sequence in bytes, or null if no sequence exists
*/
byte[] getSequence(String seq, int start, int end) ;

byte getBase(String chr, int position);
byte getBase(String seq, int position);

List<String> getChromosomeNames();

int getChromosomeLength(String chrname);
/**
* Return the given sequence length. If no sequence exists with name "seq" return -1.
*
* @param seq
* @return
*/
int getChromosomeLength(String seq);

List<Chromosome> getChromosomes();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.broad.igv.feature.genome;

public class SequenceNotFoundException extends RuntimeException {
public SequenceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public List<String> getChromosomeNames() {

@Override
public int getChromosomeLength(String chrname) {
return index.getSequenceSize(chrname);
return index.getSequenceSize(chrname);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/broad/igv/session/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.action.SearchCommand;
import org.broad.igv.ui.panel.FrameManager;
import org.broad.igv.util.LongRunningTask;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void processItem(Entry entry) {
if (FrameManager.isGeneListMode()) {
IGV.getInstance().setGeneList(null, false);
}
(new SearchCommand(FrameManager.getDefaultFrame(), locus, false)).execute();
LongRunningTask.submit(new SearchCommand(FrameManager.getDefaultFrame(), locus, false));
//Zoom should be implicit in the locus
//FrameManager.getDefaultFrame().setZoom(entry.getZoom());
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/broad/igv/track/FeatureSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.broad.igv.track;

import htsjdk.tribble.Feature;
import htsjdk.tribble.NamedFeature;
import org.broad.igv.feature.LocusScore;
import org.broad.igv.ui.panel.ReferenceFrame;

Expand Down Expand Up @@ -92,4 +93,17 @@ default void close() {
default Object getHeader() {
return null;
}

/**
* Return true if the source can be searched for a feature by name
*
* @return
*/
default boolean isSearchable() {
return false;
}

default NamedFeature search(String name) {
return null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/broad/igv/track/FeatureTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.broad.igv.track;

import htsjdk.tribble.Feature;
import htsjdk.tribble.NamedFeature;
import htsjdk.tribble.TribbleException;
import org.broad.igv.event.IGVEvent;
import org.broad.igv.logging.*;
Expand Down Expand Up @@ -1056,6 +1057,16 @@ public void marshalXML(Document document, Element element) {

}

@Override
public boolean isSearchable() {
return source.isSearchable();
}

@Override
public NamedFeature search(String token) {
return source.search(token);
}

@Override
public void unmarshalXML(Element element, Integer version) {

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/org/broad/igv/track/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


import htsjdk.tribble.Feature;
import htsjdk.tribble.NamedFeature;
import org.broad.igv.renderer.ContinuousColorScale;
import org.broad.igv.renderer.DataRange;
import org.broad.igv.renderer.Renderer;
Expand Down Expand Up @@ -272,9 +273,12 @@ default Color getExplicitAltColor() {

void setAutoScale(boolean autoScale);

default boolean isShowFeatureNames() {return true;}
default boolean isShowFeatureNames() {
return true;
}

default void setShowFeatureNames(boolean b) {}
default void setShowFeatureNames(boolean b) {
}

/**
* Return the java property or attribute for the feature display name. Default is "null", in which case the
Expand All @@ -286,6 +290,19 @@ default String getLabelField() {
return null;
}

/**
* Return true if the track can be searched for a feature by name.
*
* @return
*/
default boolean isSearchable() {
return false;
}

default NamedFeature search(String token) {
return null;
}

default void repaint() {
IGV.getInstance().repaint(this);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/broad/igv/ucsc/bb/BBFeatureSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

package org.broad.igv.ucsc.bb;

import htsjdk.tribble.NamedFeature;
import org.broad.igv.feature.BasicFeature;
import org.broad.igv.feature.LocusScore;
import org.broad.igv.feature.genome.Genome;
import org.broad.igv.logging.LogManager;
import org.broad.igv.logging.Logger;
import org.broad.igv.track.FeatureSource;
import org.broad.igv.track.WindowFunction;
import org.broad.igv.ucsc.Trix;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -111,7 +111,8 @@ public boolean isSearchable() {
return reader.isSearchable();
}

BasicFeature search(String term) {
@Override
public NamedFeature search(String term) {
try {
return reader.search(term);
} catch (IOException e) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/broad/igv/ucsc/bb/BBFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.broad.igv.ucsc.bb;

import htsjdk.samtools.seekablestream.SeekableStream;
import htsjdk.tribble.NamedFeature;
import org.broad.igv.data.BasicScore;
import org.broad.igv.feature.BasicFeature;
import org.broad.igv.feature.LocusScore;
Expand Down Expand Up @@ -400,6 +401,7 @@ public boolean isSearchable() {
* @param term
* @returns {Promise<void>}
*/

public BasicFeature search(String term) throws IOException {

if (this.header == null) {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/broad/igv/ucsc/twobit/TwoBitSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.broad.igv.feature.Chromosome;
import org.broad.igv.feature.genome.Sequence;
import org.broad.igv.feature.genome.SequenceNotFoundException;
import org.broad.igv.logging.LogManager;
import org.broad.igv.logging.Logger;
import org.broad.igv.ucsc.BPIndex;
import org.broad.igv.ucsc.BPTree;

Expand All @@ -22,6 +25,8 @@

public class TwoBitSequence implements Sequence {

private static Logger log = LogManager.getLogger(TwoBitSequence.class);

// the number 0x1A412743 in the architecture of the machine that created the file
static int SIGNATURE = 0x1a412743;
String path;
Expand Down Expand Up @@ -85,12 +90,15 @@ public List<String> getChromosomeNames() {
}

@Override
public int getChromosomeLength(String chrname) {
public int getChromosomeLength(String seq) {
try {
SequenceRecord sequenceRecord = getSequenceRecord(chrname);
SequenceRecord sequenceRecord = getSequenceRecord(seq);
return sequenceRecord.getDnaSize();
} catch (Exception e) {
throw new RuntimeException(e);
} catch (SequenceNotFoundException e) {
return -1;
} catch (IOException e) {
log.error("Error reading sequence " + seq, e);
return -1;
}
}

Expand Down Expand Up @@ -183,7 +191,7 @@ public SequenceRecord getSequenceRecord(String seqName) throws IOException {
if (record == null) {
long[] offset_length = this.index.search(seqName);
if (offset_length == null) {
throw new RuntimeException("Unknown sequence: " + seqName);
throw new SequenceNotFoundException("Unknown sequence: " + seqName);
}
long offset = offset_length[0];

Expand Down
Loading

0 comments on commit 7df59de

Please sign in to comment.