Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Nov 27, 2023
1 parent 835ea84 commit 5c6decd
Show file tree
Hide file tree
Showing 39 changed files with 617 additions and 815 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/bbfile/BBDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private List<LocusScore> getWholeGenomeScores() {

if (wholeGenomeScores.get(windowFunction) == null) {

double scale = genome.getNominalLength() / screenWidth;
double scale = genome.getWGLength() / screenWidth;

int maxChromId = reader.getChromosomeNames().size() - 1;
String firstChr = reader.getChromsomeFromId(0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/data/AbstractDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public AbstractDataSource(Genome genome) {

public int getChrLength(String chr) {
if (chr.equals(Globals.CHR_ALL)) {
return (int) (genome.getNominalLength() / 1000);
return (int) (genome.getWGLength() / 1000);
} else {
Chromosome c = genome.getChromosome(chr);
return c == null ? 0 : c.getLength();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/broad/igv/data/GenomeSummaryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class GenomeSummaryData {
public GenomeSummaryData(Genome genome, String[] samples) {
this.genome = genome;
this.samples = samples;
scale = (genome.getNominalLength() / locationUnit) / nPixels;
scale = (genome.getWGLength() / locationUnit) / nPixels;

List<String> chrNames = genome.getLongChromosomeNames();
locationMap = new HashMap<String, IntArrayList>();
Expand All @@ -115,7 +115,7 @@ public GenomeSummaryData(Genome genome, String[] samples) {
void setScale(double scale){
if(nDataPts > 0) throw new IllegalStateException("Can't alter scale after adding data");
this.scale = scale;
nPixels = (int) (((double) this.genome.getNominalLength() / locationUnit) / scale);
nPixels = (int) (((double) this.genome.getWGLength() / locationUnit) / scale);
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/data/seg/FreqData.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void compute(float ampThreshold, float delThreshold) {
amp.clear();
del.clear();

int sizeInKB = (int) (genome.getNominalLength() / 1000);
int sizeInKB = (int) (genome.getWGLength() / 1000);
int wgBinSize = sizeInKB / 700;
int wgBinCount = sizeInKB / wgBinSize + 1;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/feature/genome/ChromAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ChromAlias {
public ChromAlias(String chr) {
this.chr = chr;
this.aliases = new HashMap<>();
this.aliases.put("chr", chr);
this.aliases.put("chr", chr); // "chr" is the name set
}

public String getChr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ private void init(String id, List<String> chromosomeNames) {
switch (name) {
case "chrX":
record.put("ncbi", "23");
record.put("?", "X");
skipRest = true;
break;
case "chrY":
record.put("ncbi", "24");
record.put("?", "Y");
skipRest = true;
break;
}
Expand Down
112 changes: 67 additions & 45 deletions src/main/java/org/broad/igv/feature/genome/ChromAliasFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,91 @@ public class ChromAliasFile extends ChromAliasSource {
private String[] nameSets;


public ChromAliasFile(String path, Genome genome) throws IOException {
public ChromAliasFile(String path, List<String> chromosomeNames) throws IOException {
try (BufferedReader br = ParsingUtils.openBufferedReader(path)) {
init(br, genome.getAllChromosomeNames());

this.aliasCache = new HashMap<>();

Set<String> chromosomeNameSet = chromosomeNames != null ? new HashSet<>(chromosomeNames) : Collections.EMPTY_SET;

// First line
boolean firstLine = true;
String line;
while ((line = br.readLine()) != null) {
if (firstLine && line.startsWith("#")) {
String[] tokens = line.substring(1).split("\t");
this.nameSets = new String[tokens.length];
for (int i = 0; i < tokens.length; i++) {
this.nameSets[i] = tokens[i].trim();
}
} else {
String[] tokens = line.split("\t");
if (tokens.length > 1) {

// Find the canonical chromosome
String chr = null;
for (String c : tokens) {
if (chromosomeNameSet.contains(c)) {
chr = c;
break;
}
}
if (chr == null) {
chr = tokens[0];
}

ChromAlias aliasRecord = new ChromAlias(chr);
this.aliasCache.put(chr, aliasRecord);
for (int i = 0; i < tokens.length; i++) {
String key = this.nameSets != null ? this.nameSets[i] : String.valueOf(i);
aliasRecord.put(key, tokens[i]);
this.aliasCache.put(tokens[i], aliasRecord);
}
}
}
firstLine = false;
}

}
}

/**
* Constructor for legacy .genome files
* @param br
* @param genome
*
* @throws IOException
*/
public ChromAliasFile(BufferedReader br, Genome genome) throws IOException {
init(br, genome.getAllChromosomeNames());

}

private void init(BufferedReader br, List<String> chromosomeNames) throws IOException {

this.aliasCache = new HashMap<>();
public ChromAliasFile(List<List<String>> chromAliases, List<String> chromosomeNames) throws IOException {

Set<String> chromosomeNameSet = chromosomeNames != null ? new HashSet<>(chromosomeNames) : Collections.EMPTY_SET;

// First line
boolean firstLine = true;
String line;
while ((line = br.readLine()) != null) {
if (firstLine && line.startsWith("#")) {
String[] tokens = line.substring(1).split("\t");
this.nameSets = new String[tokens.length];
for (int i = 0; i < tokens.length; i++) {
this.nameSets[i] = tokens[i].trim();
}
} else {
String[] tokens = line.split("\t");
if (tokens.length > 1) {

// Find the canonical chromosome
String chr = null;
for (String c : tokens) {
if (chromosomeNameSet.contains(c)) {
chr = c;
break;
}
}
if (chr == null) {
chr = tokens[0];
}
for (List<String> aliases : chromAliases) {
if (aliases.size() > 1) {

ChromAlias aliasRecord = new ChromAlias(chr);
this.aliasCache.put(chr, aliasRecord);
for (int i = 0; i < tokens.length; i++) {
String key = this.nameSets != null ? this.nameSets[i] : String.valueOf(i);
aliasRecord.put(key, tokens[i]);
this.aliasCache.put(tokens[i], aliasRecord);
// Find the canonical chromosome
String chr = null;
for (String c : aliases) {
if (chromosomeNameSet.contains(c)) {
chr = c;
break;
}
}
if (chr == null) {
chr = aliases.get(0);
}

ChromAlias aliasRecord = new ChromAlias(chr);
this.aliasCache.put(chr, aliasRecord);
int idx = 0;
for (String a : aliases) {
String key = String.valueOf(idx++);
aliasRecord.put(key, a);
this.aliasCache.put(a, aliasRecord);
}
}
firstLine = false;
}

}


/**
* Return the canonical chromosome name for the alias. If none found return the alias
*
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/org/broad/igv/feature/genome/ChromAliasManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,49 @@
import java.io.IOException;
import java.util.*;

/**
* A data/feature source helper class for managing chromosome aliasing. Maps reference sequence names to aliases
* used by the feature source (e.g. chr20 -> 20).
*/
public class ChromAliasManager {

Set<String> sequenceNames;

Genome genome;

Map<String, String> aliasCache;
private Set<String> sequenceNames;

private Genome genome;

private Map<String, String> aliasCache;

/**
* @param sequenceNames - Sequence names defined by the data source (e.g. bam or feature file)
* @param genome - Reference genome object.
*/
public ChromAliasManager(Collection<String> sequenceNames, Genome genome) {
this.sequenceNames = new HashSet<>(sequenceNames);
this.aliasCache = new HashMap<>();
this.genome = genome;
}

public String getAliasName(String chr) {
if(genome == null) {
return chr; // A no-op manager, used mostly in testing.
public String getAliasName(String seqName) {
if (genome == null) {
return seqName; // A no-op manager, used in testing.
}
try {
if(!aliasCache.containsKey(chr)) {
ChromAlias aliasRecord = genome.getAliasRecord(chr);
for(String alias : aliasRecord.values()) {
if(sequenceNames.contains(alias));
aliasCache.put(chr, alias);
return alias;
if (!aliasCache.containsKey(seqName)) {
ChromAlias aliasRecord = genome.getAliasRecord(seqName);
if (aliasRecord == null) {
aliasCache.put(seqName, null); // No know alias, record to prevent searching again
} else {
for (String alias : aliasRecord.values()) {
if (sequenceNames.contains(alias)) {
aliasCache.put(seqName, alias);
}
}
}
}
return aliasCache.get(chr);
return aliasCache.get(seqName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public ChromAliasSource() {

public abstract ChromAlias search(String alias) throws IOException;

/**
* Add a ChromAlias record directly to the cache, overriding any existing alias. This is primarily used for
* user-defined overrides.
*
* @param chromAlias
*/
public void add(ChromAlias chromAlias) {
aliasCache.put(chromAlias.getChr(), chromAlias);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* For comparing chromosomes. We order by "important" vs "unimportant".
* The idea being there are main chromosomes (chr1...chrX, chrM) and
* sometimes many smaller contigs.
*
* <p>
* Date: 2012-Aug-16
*/
public class ChromosomeComparator implements Comparator<Chromosome> {
Expand All @@ -62,6 +62,10 @@ public int compare(Chromosome o1, Chromosome o2) {
return -1;
} else if (isMyto(o2.getName())) {
return 1;
} else if (o1.getName().startsWith("chr") && !o2.getName().startsWith("chr")) {
return -1;
} else if (o2.getName().startsWith("chr") && !o1.getName().startsWith("chr")) {
return 1;
} else {
return o2.getLength() - o1.getLength();
}
Expand Down
Loading

0 comments on commit 5c6decd

Please sign in to comment.