Skip to content

Commit

Permalink
refine genome "is downloadable" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Dec 4, 2024
1 parent 75fe8eb commit a67652a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Class of static functions for managing genome downloads
Expand All @@ -30,20 +29,27 @@ public class GenomeDownloadUtils {

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

public static boolean isAnnotationsDownloadable(GenomeListItem item) {
return item.getPath().endsWith(".json");
public static boolean isAnnotationsDownloadable(String path) {
return path.endsWith(".json");
}

public static boolean isSequenceDownloadable(GenomeListItem item) {
if (item.getPath().endsWith(".json")) {
public static boolean isSequenceDownloadable(String path) {
if (path != null && path.endsWith(".json")) {
try {
String jsonString = HttpUtils.getInstance().getContentsAsJSON(new URL(item.getPath()));
return jsonString.contains("twoBitURL");
String jsonString = FileUtils.getContents(path);
GenomeConfig genomeConfig = GenomeConfig.fromJson(jsonString);
String sequenceURL = genomeConfig.getTwoBitURL();
if (sequenceURL == null) {
sequenceURL = genomeConfig.getFastaURL();
}
return isRemoteURL(sequenceURL) && !disallowedBuckets.stream().anyMatch(sequenceURL::contains);

} catch (IOException e) {
log.error("Error fetching genome json " + item.getPath());
log.error("Error fetching genome json " + path);
}
}
return false;

}

public static File downloadGenome(GenomeConfig c, boolean downloadSequence, boolean downloadAnnotations) throws IOException {
Expand Down Expand Up @@ -161,4 +167,13 @@ private static File download(URL url, File directory) throws MalformedURLExcepti
return localFile;
}

private static boolean isRemoteURL(String url) {
return url.startsWith("http://") || url.startsWith("https://");
}

static Set<String> disallowedBuckets = new HashSet<>(Arrays.asList(
"igv.org.genomes",
"igv-genepattern-org",
"igv.broadinstitute.org"));

}
20 changes: 20 additions & 0 deletions src/main/java/org/broad/igv/feature/genome/load/GenomeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class GenomeConfig implements Cloneable {
private List<List<String>> chromAliases;

public static GenomeConfig fromJson(String json) {
if (json.contains("chromosomeOrder")) {
json = fixChromosomeOrder(json);
}
return (new Gson()).fromJson(json, GenomeConfig.class);
}

Expand Down Expand Up @@ -307,4 +310,21 @@ protected GenomeConfig clone() {
}
}

/**
* Fix deprecated form of chromosome order (comma delimited list of strings)
*
* @param jsonString
* @return
*/
private static String fixChromosomeOrder(String jsonString) {
Map obj = (new Gson()).fromJson(jsonString, Map.class);
Object chromosomeOrder = obj.get("chromosomeOrder");
if (chromosomeOrder != null) {
if (chromosomeOrder instanceof String) {
obj.put("chromosomeOrder", Arrays.stream(((String) chromosomeOrder).split(",")).map(c -> c.trim()).toArray());
}
}
return (new Gson()).toJson(obj);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public GenomeConfig loadGenomeConfig() throws IOException {

String jsonString = ParsingUtils.readContentsFromStream(is);

if (jsonString.contains("chromosomeOrder")) {
jsonString = fixChromosomeOrder(jsonString);
}

GenomeConfig genomeConfig = GenomeConfig.fromJson(jsonString);

fixPaths(genomeConfig);
Expand All @@ -79,24 +75,6 @@ public GenomeConfig loadGenomeConfig() throws IOException {
}
}

/**
* Fix deprecated form of chromosome order (comma delimited list of strings)
*
* @param jsonString
* @return
*/
private String fixChromosomeOrder(String jsonString) {
Map obj = (new Gson()).fromJson(jsonString, Map.class);
Object chromosomeOrder = obj.get("chromosomeOrder");
if (chromosomeOrder != null) {
if (chromosomeOrder instanceof String) {
obj.put("chromosomeOrder", Arrays.stream(((String) chromosomeOrder).split(",")).map(c -> c.trim()).toArray());
}
}
return (new Gson()).toJson(obj);

}

/**
* JSON paths/urls can be relative to the path to the genome json file. This method converts them to absolulte
* paths/urls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {

private void configureDownloadButtons(GenomeListItem item) {
if (item != null) {
final boolean sequenceDownloadable = GenomeDownloadUtils.isSequenceDownloadable(item);
final boolean annotationsDownloadable = GenomeDownloadUtils.isAnnotationsDownloadable(item);
final boolean sequenceDownloadable = GenomeDownloadUtils.isSequenceDownloadable(item.getPath());
final boolean annotationsDownloadable = GenomeDownloadUtils.isAnnotationsDownloadable(item.getPath());
downloadSequenceCB.setEnabled(sequenceDownloadable);
downloadAnnotationsCB.setEnabled(annotationsDownloadable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.broad.igv.feature.genome;

import org.broad.igv.util.TestUtils;
import org.junit.Test;

import static org.junit.Assert.*;

public class GenomeDownloadUtilsTest {

@Test
public void isAnnotationsDownloadable() {
assertTrue(GenomeDownloadUtils.isAnnotationsDownloadable(TestUtils.DATA_DIR + "genomes/json/hg38_twobit.json"));

}

@Test
public void isSequenceDownloadable() {

assertTrue(GenomeDownloadUtils.isSequenceDownloadable(TestUtils.DATA_DIR + "genomes/json/hg38_twobit.json"));
assertFalse(GenomeDownloadUtils.isSequenceDownloadable(TestUtils.DATA_DIR + "genomes/json/hg18.unittest.json"));
assertFalse(GenomeDownloadUtils.isSequenceDownloadable(TestUtils.DATA_DIR + "genomes/json/sacCer3.json"));

}
}
21 changes: 21 additions & 0 deletions test/data/genomes/json/hg38_twobit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "hg38",
"name": "Human (GRCh38/hg38)",
"fastaURL": "https://igv.org/genomes/data/hg38/hg38.fa",
"indexURL": "https://igv.org/genomes/data/hg38/hg38.fa.fai",
"cytobandURL": "https://hgdownload.soe.ucsc.edu/goldenPath/hg38/database/cytoBandIdeo.txt.gz",
"aliasURL": "https://igv.org/genomes/data/hg38/hg38_alias.tab",
"twoBitURL": "https://hgdownload.soe.ucsc.edu/goldenPath/hg38/bigZips/hg38.2bit",
"chromSizesURL": "https://hgdownload.soe.ucsc.edu/goldenPath/hg38/bigZips/hg38.chrom.sizes",
"chromosomeOrder": "chr1,chr2,chr3,chr4,chr5,chr6,chr7,chr8,chr9,chr10,chr11,chr12,chr13,chr14,chr15,chr16,chr17,chr18,chr19,chr20,chr21,chr22,chrX,chrY",
"tracks": [
{
"name": "Refseq Select",
"format": "refgene",
"url": "https://hgdownload.soe.ucsc.edu/goldenPath/hg38/database/ncbiRefSeqSelect.txt.gz",
"indexed": false,
"order": 1000001,
"infoURL": "https://www.ncbi.nlm.nih.gov/gene/?term=$$"
}
]
}

0 comments on commit a67652a

Please sign in to comment.