Skip to content

Commit

Permalink
Add ncbi version aliasing, .x -> no version
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Dec 8, 2023
1 parent cee21a1 commit bf0811b
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 20 deletions.
38 changes: 26 additions & 12 deletions src/main/java/org/broad/igv/feature/genome/ChromAliasDefaults.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.broad.igv.feature.genome;

import org.broad.igv.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChromAliasDefaults extends ChromAliasSource {

Expand All @@ -21,6 +21,14 @@ private void init(String id, List<String> chromosomeNames) {
ChromAlias record = new ChromAlias(name);
aliasRecords.add(record);

//
int version = getVersion(name);
if (version >= 0) {
int idx = name.indexOf(".");
String alias = name.substring(0, idx);
record.put("ncbi-noversion", alias);
}

if (name.startsWith("gi|")) {
// NCBI
String alias = ChromAliasDefaults.getNCBIName(name);
Expand Down Expand Up @@ -90,7 +98,7 @@ private void init(String id, List<String> chromosomeNames) {
record.put("ucsc", "chrM");
} else if (name.toLowerCase().startsWith("chr")) {
record.put("ncbi", name.substring(3));
} else if (isSmallPositiveInteger(name)) {
} else if (StringUtils.isSmallPositiveInteger(name)) {
record.put("ucsc", "chr" + name);
}
}
Expand Down Expand Up @@ -143,16 +151,22 @@ public static String getNCBIName(String name) {
return tokens[tokens.length - 1];
}


public static boolean isSmallPositiveInteger(String str) {
int length = str.length();
if (length > 100) return false;
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
/**
* If name has what looks like an NCBI version, return the version
*
* @param name
* @return
*/
private static Integer getVersion(String name) {
int idx = name.lastIndexOf(".");
if (idx > 1) {
String possibleVersion = name.substring(idx + 1);
if (StringUtils.isSmallPositiveInteger(possibleVersion)) {
return Integer.parseInt(possibleVersion);
}
}
return true;
return -1;
}


}
77 changes: 70 additions & 7 deletions src/main/java/org/broad/igv/sam/AlignmentDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public double getVisibilityWindow() {
}



private IGVPreferences getPreferences() {
String category = NULL_CATEGORY;
AlignmentTrack.ExperimentType experimentType = getExperimentType();
Expand Down Expand Up @@ -329,17 +328,18 @@ private boolean intervalInView(AlignmentInterval interval) {

AlignmentInterval loadInterval(String chr, int start, int end, AlignmentTrack.RenderOptions renderOptions) {


String sequence = sequenceNames.contains(chr) ? chr : chromAliasManager.getAliasName(chr);
String seqName = sequenceNames.contains(chr) ? chr : chromAliasManager.getAliasName(chr);
if (seqName == null) {
// No alignments with this chr name -- return empty interval
return new AlignmentInterval(chr, start, end);
}

DownsampleOptions downsampleOptions = new DownsampleOptions();

final AlignmentTrack.BisulfiteContext bisulfiteContext =
renderOptions != null ? renderOptions.bisulfiteContext : null;

SpliceJunctionHelper spliceJunctionHelper = new SpliceJunctionHelper();

AlignmentTileLoader.AlignmentTile t = getLoader().loadTile(sequence, start, end, spliceJunctionHelper,
AlignmentTileLoader.AlignmentTile t = getLoader().loadTile(seqName, start, end, spliceJunctionHelper,
downsampleOptions, peStats, bisulfiteContext, renderOptions);
List<Alignment> alignments = t.getAlignments();
List<DownsampledInterval> downsampledIntervals = t.getDownsampledIntervals();
Expand Down Expand Up @@ -371,7 +371,7 @@ private void updateBaseModfications(List<Alignment> alignments) {
}

public AlignmentTrack.ExperimentType inferType() {
if(this.inferredType != null) {
if (this.inferredType != null) {
return this.inferredType;
}

Expand Down Expand Up @@ -548,5 +548,68 @@ public int getMaxReadCount() {

}


/**
* Create an alias -> chromosome lookup map. Enables loading BAM files that use alternative names for chromosomes
* (e.g. 1 -> chr1, etc).
*/
// private void initChrMap(Genome genome) throws IOException {
//
// if (genome != null) {
//
// // Build a chr size -> name lookup table. We will assume sizes are unique. This will be used if no alias
// // is defined for a sequence.
// Map<Long, String> inverseDict = null;
// Map<String, Long> sequenceDictionary = getLoader().getSequenceDictionary();
//
// if (sequenceDictionary != null) {
//
// Set<Long> nonUnique = new HashSet<>();
// Set<Long> seen = new HashSet<>();
// // First find sequences whose size are not unique, we'll filter these
// for (Long size : sequenceDictionary.values()) {
// if (seen.contains(size)) {
// nonUnique.add(size);
// } else {
// seen.add(size);
// }
// }
//
// inverseDict = new HashMap<>();
//
// for (Chromosome chromosome : genome.getChromosomes()) {
//
// Long size = (long) chromosome.getLength();
// if (!nonUnique.contains(size)) {
// if (inverseDict.containsKey(size)) {
// inverseDict.remove(size);
// nonUnique.add(size);
// } else {
// inverseDict.put(size, chromosome.getName());
// }
// }
// }
// }
//
//
// List<String> seqNames = getLoader().getSequenceNames();
// if (seqNames != null) {
// for (String seq : seqNames) {
//
// if (genome.isKnownChr(seq)) {
// String chr = genome.getCanonicalChrName(seq);
// chrMappings.put(chr, seq);
// } else if (sequenceDictionary != null) {
// Long size = sequenceDictionary.get(seq);
// String chr = inverseDict.get(size);
// if (chr != null) {
// chrMappings.put(chr, seq);
// }
// }
// }
// }
// }
// }
//
}

15 changes: 15 additions & 0 deletions src/main/java/org/broad/igv/sam/AlignmentInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public AlignmentInterval(String chr, int start, int end,
this.downsampledIntervals = downsampledIntervals;
}

/**
* Constructor to create empty interval, that is interval with no alignments
*
* @param chr
* @param start
* @param end
*/
public AlignmentInterval(String chr, int start, int end) {
super(chr, start, end);
alignments = Collections.EMPTY_LIST;
counts = EmptyAlignmentCounts.getInstance();
spliceJunctionHelper = new SpliceJunctionHelper();
downsampledIntervals = Collections.EMPTY_LIST;
}

static Alignment getFeatureContaining(List<Alignment> features, int right) {

int leftBounds = 0;
Expand Down
133 changes: 133 additions & 0 deletions src/main/java/org/broad/igv/sam/EmptyAlignmentCounts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.broad.igv.sam;

public class EmptyAlignmentCounts implements AlignmentCounts {

private static EmptyAlignmentCounts instance;

public static AlignmentCounts getInstance() {
if(instance == null) {
instance = new EmptyAlignmentCounts();
}
return instance;
}

@Override
public void incCounts(Alignment alignment) {

}

@Override
public int getTotalCount(int pos) {
return 0;
}

@Override
public int getTotalPositiveCount(int pos) {
return 0;
}

@Override
public int getTotalNegativeCount(int pos) {
return 0;
}

@Override
public int getTotalQuality(int pos) {
return 0;
}

@Override
public int getCount(int pos, byte b) {
return 0;
}

@Override
public int getNegCount(int pos, byte b) {
return 0;
}

@Override
public int getPosCount(int pos, byte b) {
return 0;
}

@Override
public int getDelCount(int pos) {
return 0;
}

@Override
public int getInsCount(int pos) {
return 0;
}

@Override
public int getQuality(int pos, byte b) {
return 0;
}

@Override
public int getNumberOfPoints() {
return 0;
}

@Override
public int getMaxCount(int origin, int end) {
return 0;
}

@Override
public String getValueStringAt(int pos) {
return null;
}

@Override
public boolean isConsensusMismatch(int pos, byte ref, String chr, float snpThreshold) {
return false;
}

@Override
public boolean isConsensusDeletion(int start, int end, float snpThreshold) {
return false;
}

@Override
public boolean isConsensusInsertion(int pos, float snpThreshold) {
return false;
}

@Override
public BisulfiteCounts getBisulfiteCounts() {
return null;
}

@Override
public int getBucketSize() {
return 0;
}

@Override
public boolean hasBaseCounts() {
return false;
}

@Override
public void finish() {

}

@Override
public String getContig() {
return null;
}

@Override
public int getStart() {
return 0;
}

@Override
public int getEnd() {
return 0;
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/broad/igv/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,15 @@ public static boolean isQuoted(String fileString) {
(fileString.startsWith("'") && fileString.endsWith("'"));
}

public static boolean isSmallPositiveInteger(String str) {
int length = str.length();
if (length > 2) return false;
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
}
10 changes: 9 additions & 1 deletion src/test/java/org/broad/igv/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

/**
* Created by IntelliJ IDEA.
Expand Down Expand Up @@ -66,7 +66,15 @@ public void testBreakQuotedStrin2() throws Exception {
assertEquals(tokens.get(0), "abc");
assertEquals(tokens.get(1), "def");
assertEquals(tokens.get(2), "'beforeComma,afterComma'");
}

@Test
public void testIsSmallPositiveInteger() {
assertTrue(StringUtils.isSmallPositiveInteger("99"));
assertFalse(StringUtils.isSmallPositiveInteger("100"));
assertFalse(StringUtils.isSmallPositiveInteger("1.5"));
assertFalse(StringUtils.isSmallPositiveInteger("-1"));
assertFalse(StringUtils.isSmallPositiveInteger("foo"));
}

}

0 comments on commit bf0811b

Please sign in to comment.