Skip to content

Commit

Permalink
More track hub support
Browse files Browse the repository at this point in the history
* Rewrite of BB is complete
* Chromosome aliasing rewrite complete
* Removed old MethylTrack and associated files
  • Loading branch information
jrobinso committed Nov 22, 2023
1 parent 4ac4227 commit 6cce11c
Show file tree
Hide file tree
Showing 67 changed files with 5,600 additions and 1,690 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/broad/igv/bbfile/codecs/BBBedCodec.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.broad.igv.bbfile.codecs;

import org.broad.igv.Globals;
import org.broad.igv.bbfile.BBUtils;
import org.broad.igv.ucsc.bb.BBUtils;
import org.broad.igv.bbfile.BedData;
import org.broad.igv.feature.*;
import org.broad.igv.feature.genome.Genome;
import org.broad.igv.feature.tribble.IGVBEDCodec;

import java.util.LinkedHashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.broad.igv.bbfile.codecs;

import org.broad.igv.bbfile.BBUtils;

import java.util.Collections;
import java.util.List;
import org.broad.igv.ucsc.bb.BBUtils;

public class BBCodecFactory {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/bbfile/codecs/BBRmskCodec.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.broad.igv.bbfile.codecs;

import org.broad.igv.Globals;
import org.broad.igv.bbfile.BBUtils;
import org.broad.igv.ucsc.bb.BBUtils;
import org.broad.igv.bbfile.BedData;
import org.broad.igv.feature.BasicFeature;
import org.broad.igv.feature.Exon;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/broad/igv/feature/genome/ChromAlias.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.broad.igv.feature.genome;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ChromAlias {

private String chr;
private Map<String, String> aliases;

public ChromAlias(String chr) {
this.chr = chr;
this.aliases = new HashMap<>();
this.aliases.put("chr", chr);
}

public String getChr() {
return chr;
}

public void put(String nameSet, String alias) {
aliases.put(nameSet, alias);
}
public String get(String nameSet) {
return aliases.get(nameSet);
}

public boolean containsKey(String nameSet) {
return aliases.containsKey(nameSet);
}

public Collection<String> values() {
return aliases.values();
}



}
76 changes: 76 additions & 0 deletions src/main/java/org/broad/igv/feature/genome/ChromAliasBB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.broad.igv.feature.genome;

import org.broad.igv.feature.BasicFeature;
import org.broad.igv.ucsc.bb.BBFile;
import java.io.IOException;

public class ChromAliasBB extends ChromAliasSource {


BBFile reader;

public ChromAliasBB(String path, Genome genome) throws IOException {
this.reader = new BBFile(path, genome);
}


/**
* Return the canonical chromosome name for the alias. If none found return the alias
*
* @param alias
* @returns {*}
*/
public String getChromosomeName(String alias) {
if(aliasCache.containsKey(alias)) {
return aliasCache.get(alias).get("chr");
} else {
try {
ChromAlias aliasRecord = search(alias);
if(aliasRecord == null) {
aliasRecord.put(alias, null); // Prevents future attempts
} else {
return aliasRecord.get("chr");
}
} catch (IOException e) {
// TODO -- log
}
}
return null;
}

/**
* Return an alternate chromosome name (alias).
*
* @param chr
* @param nameSet -- The name set, e.g. "ucsc"
* @returns {*|undefined}
*/
public String getChromosomeAlias(String chr, String nameSet) {
ChromAlias aliasRecord = this.aliasCache.get(chr);
return aliasRecord != null && aliasRecord.containsKey(nameSet) ? aliasRecord.get(nameSet) : chr;
}

/**
* Search for chromosome alias bed record. If found, cache results in the alias -> chr map
*
* @param alias
* @returns {Promise<any>}
*/
public ChromAlias search(String alias) throws IOException {
if (!this.aliasCache.containsKey(alias)) {
BasicFeature f = this.reader.search(alias);
if (f != null) {
String chr = f.getChr();
ChromAlias aliasRecord = new ChromAlias(chr);
this.aliasCache.put(chr, aliasRecord);
for (String key : f.getAttributeKeys()){
final String a = f.getAttribute(key);
aliasRecord.put(key, a);
this.aliasCache.put(a, aliasRecord); // One entry for each alias
}
}
}
return this.aliasCache.get(alias);
}

}
156 changes: 156 additions & 0 deletions src/main/java/org/broad/igv/feature/genome/ChromAliasDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package org.broad.igv.feature.genome;

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

public class ChromAliasDefaults extends ChromAliasSource {

public ChromAliasDefaults(String id, List<String> chromosomeNames) {

this.init(id, chromosomeNames);
}

private void init(String id, List<String> chromosomeNames) {

List<ChromAlias> aliasRecords = new ArrayList<>();
for (String name : chromosomeNames) {

boolean skipRest = false;
ChromAlias record = new ChromAlias(name);
aliasRecords.add(record);

if (name.startsWith("gi|")) {
// NCBI
String alias = ChromAliasDefaults.getNCBIName(name);
record.put("ncbi-gi-versioned", alias);

// Also strip version number out, if present
int dotIndex = alias.lastIndexOf('.');
if (dotIndex > 0) {
alias = alias.substring(0, dotIndex);
record.put("ncbi-gi", alias);
}
} else {
// Special cases for human and mouse
if (id.startsWith("hg") || id.equals("1kg_ref") || id.equals("b37")) {
switch (name) {
case "chrX":
record.put("ncbi", "23");
skipRest = true;
break;
case "chrY":
record.put("ncbi", "24");
skipRest = true;
break;
}
} else if (id.startsWith("GRCh")) {
switch (name) {
case "23":
record.put("ucsc", "chrX");
skipRest = true;
break;
case "24":
record.put("ucsc", "chrY");
skipRest = true;
break;
}
} else if (id.startsWith("mm") || id.startsWith("rheMac")) {
switch (name) {
case "chrX":
record.put("ncbi", "21");
skipRest = true;
break;
case "chrY":
record.put("ncbi", "22");
skipRest = true;
break;
}
} else if (id.startsWith("GRCm")) {
switch (name) {
case "21":
record.put("ucsc", "chrX");
skipRest = true;
break;
case "22":
record.put("ucsc", "chrY");
skipRest = true;
break;
}
}
if (skipRest) continue;

//
if (name.equals("chrM")) {
record.put("ncbi", "MT");
} else if (name.equals("MT")) {
record.put("ucsc", "chrM");
} else if (name.toLowerCase().startsWith("chr")) {
record.put("ncbi", name.substring(3));
} else if (isSmallPositiveInteger(name)) {
record.put("ucsc", "chr" + name);
}
}
}

for (ChromAlias rec : aliasRecords) {
for (String a : rec.values()) {
this.aliasCache.put(a, rec);
}
}
}

/**
* Return the canonical chromosome name for the alias. If none found return the alias
*
* @param alias
* @returns {*}
*/
@Override
public String getChromosomeName(String alias) {
return this.aliasCache.containsKey(alias) ? this.aliasCache.get(alias).get("chr") : alias;
}

/**
* Return an alternate chromosome name (alias).
*
* @param chr
* @param nameSet -- The name set, e.g. "ucsc"
* @returns {*|undefined}
*/
@Override
public String getChromosomeAlias(String chr, String nameSet) {
ChromAlias aliasRecord = this.aliasCache.get(chr);
return aliasRecord != null && aliasRecord.containsKey(nameSet) ? aliasRecord.get(nameSet) : chr;
}

@Override
public ChromAlias search(String alias) {
return this.aliasCache.get(alias);
}

/**
* Extract the user friendly name from an NCBI accession
* example: gi|125745044|ref|NC_002229.3| => NC_002229.3
*/

public static String getNCBIName(String name) {

String[] tokens = name.split("\\|");
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;
}
}
return true;
}
}
Loading

0 comments on commit 6cce11c

Please sign in to comment.