Skip to content

Commit

Permalink
Genome improvements (#1589)
Browse files Browse the repository at this point in the history
Refactoring genome load management to simplify code.
  • Loading branch information
jrobinso authored Sep 27, 2024
1 parent ab536a5 commit c7ef831
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 152 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/feature/genome/Genome.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public Genome(GenomeConfig config) throws IOException {
} else if (config.fastaURL != null) {
String fastaPath = config.fastaURL;
String indexPath = config.indexURL;
String gziIndexPath = config.gziIndexURL;
String gziIndexPath = config.gziIndexURL != null ? config.gziIndexURL : config.compressedIndexURL; // Synonyms
uncachedSequence = fastaPath.endsWith(".gz") ?
new FastaBlockCompressedSequence(fastaPath, gziIndexPath, indexPath) :
new FastaIndexedSequence(fastaPath, indexPath);
Expand Down
61 changes: 31 additions & 30 deletions src/main/java/org/broad/igv/feature/genome/GenomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ private GenomeManager() {
* @throws UnsupportedEncodingException
*/
public static File getGenomeFile(String genomePath) throws MalformedURLException, UnsupportedEncodingException {

File archiveFile;

if (HttpUtils.isRemoteURL(genomePath.toLowerCase())) {
// We need a local copy, as there is no http zip file reader
URL genomeArchiveURL = HttpUtils.createURL(genomePath);
Expand Down Expand Up @@ -195,10 +197,11 @@ public Genome loadGenome(String genomePath) throws IOException {
Genome newGenome = GenomeLoader.getLoader(genomePath).loadGenome();

// Load user-defined chr aliases, if any. This is done last so they have priority
final File genomeCacheDirectory = DirectoryManager.getGenomeCacheDirectory();
try {
String aliasPath = (new File(DirectoryManager.getGenomeCacheDirectory(), newGenome.getId() + "_alias.tab")).getAbsolutePath();
String aliasPath = (new File(genomeCacheDirectory, newGenome.getId() + "_alias.tab")).getAbsolutePath();
if (!(new File(aliasPath)).exists()) {
aliasPath = (new File(DirectoryManager.getGenomeCacheDirectory(), newGenome.getId() + "_alias.tab.txt")).getAbsolutePath();
aliasPath = (new File(genomeCacheDirectory, newGenome.getId() + "_alias.tab.txt")).getAbsolutePath();
}
if ((new File(aliasPath)).exists()) {
newGenome.addChrAliases(ChromAliasParser.loadChrAliases(aliasPath));
Expand All @@ -212,7 +215,13 @@ public Genome loadGenome(String genomePath) throws IOException {
IGV.getInstance().resetSession(null);
}

setCurrentGenome(genomePath, newGenome);
// If the genome is loaded from anywhere other than the cache directory add an entry to the pulldown
if(newGenome != null && !(new File(genomePath).getParentFile().equals(genomeCacheDirectory))) {
GenomeListItem genomeListItem = new GenomeListItem(newGenome.getDisplayName(), genomePath, newGenome.getId());
GenomeListManager.getInstance().addGenomeItem(genomeListItem, true);
}

setCurrentGenome(newGenome);

return currentGenome;

Expand All @@ -226,13 +235,7 @@ public Genome loadGenome(String genomePath) throws IOException {
}
}

public void setCurrentGenome(String genomePath, Genome newGenome) {

GenomeListItem genomeListItem = new GenomeListItem(newGenome.getDisplayName(), genomePath, newGenome.getId());
final Set<String> serverGenomeIDs = genomeListManager.getServerGenomeIDs();

boolean userDefined = !serverGenomeIDs.contains(newGenome.getId());
genomeListManager.addGenomeItem(genomeListItem, userDefined);
public void setCurrentGenome(Genome newGenome) {

this.currentGenome = newGenome;

Expand Down Expand Up @@ -354,26 +357,32 @@ public Genome getCurrentGenome() {
}


public boolean downloadGenome(GenomeListItem item, boolean downloadSequence) {
public boolean downloadGenome(GenomeListItem item, boolean downloadDataFiles) {

boolean success;
try {
File genomeFile = getGenomeFile(item.getPath()); // Has side affect of downloading .genome file
if (downloadSequence) {
String fastaPath = null;
File genomeFile = getGenomeFile(item.getPath()); // Has side affect of downloading .genome file

if (downloadDataFiles) {


if (item.getPath().endsWith(".genome")) {
GenomeDescriptor genomeDescriptor = GenomeDescriptor.parseGenomeArchiveFile(genomeFile);
fastaPath = genomeDescriptor.getSequencePath();
String fastaPath = genomeDescriptor.getSequencePath();
if (fastaPath != null && FileUtils.isRemote(fastaPath)) {
File localFile = downloadFasta(fastaPath);
if (localFile != null) {
addLocalFasta(item.getId(), localFile);
}
}

} else if (item.getPath().endsWith(".json")) {

JsonGenomeLoader.GenomeDescriptor desc = (new JsonGenomeLoader(item.getPath())).loadDescriptor();
fastaPath = desc.getFastaURL();
}
if (fastaPath != null && FileUtils.isRemote(fastaPath)) {
File localFile = downloadFasta(fastaPath);
if (localFile != null) {
addLocalFasta(item.getId(), localFile);
}


}

}

success = true;
Expand All @@ -391,7 +400,6 @@ public boolean downloadGenome(GenomeListItem item, boolean downloadSequence) {
}

return success;

}


Expand Down Expand Up @@ -466,11 +474,4 @@ private static void updateSequenceMapFile() {
}
}

public void refreshHostedGenome(String genomeId) {

Map<String, GenomeListItem> itemMap = GenomeListManager.getInstance().getServerGenomeMap();
if (itemMap.containsKey(genomeId)) {
downloadGenome(itemMap.get(genomeId), false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ private String fixChromosomeOrder(String jsonString) {
* @return
*/
private GenomeConfig fixPaths(GenomeConfig config) {

if (config.chromAliasBbURL != null) {
config.chromAliasBbURL = FileUtils.getAbsolutePath(config.chromAliasBbURL, genomePath);
}
if (config.twoBitURL != null) {
config.twoBitURL = FileUtils.getAbsolutePath(config.twoBitURL, genomePath);
}
if (config.cytobandBbURL != null) {
config.cytobandBbURL = FileUtils.getAbsolutePath(config.cytobandBbURL, genomePath);
}
if (config.chromSizesURL != null) {
config.chromSizesURL = FileUtils.getAbsolutePath(config.chromSizesURL, genomePath);
}
if (config.fastaURL != null) {
config.fastaURL = FileUtils.getAbsolutePath(config.fastaURL, genomePath);
}
Expand All @@ -107,6 +120,9 @@ private GenomeConfig fixPaths(GenomeConfig config) {
if (config.gziIndexURL != null) {
config.gziIndexURL = FileUtils.getAbsolutePath(config.gziIndexURL, genomePath);
}
if (config.compressedIndexURL != null) {
config.compressedIndexURL = FileUtils.getAbsolutePath(config.compressedIndexURL, genomePath);
}
if (config.cytobandURL != null) {
config.cytobandURL = FileUtils.getAbsolutePath(config.cytobandURL, genomePath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/ui/IGV.java
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ public void run() {

if (!genomeLoaded) {
Genome genome = Genome.nullGenome();
GenomeManager.getInstance().setCurrentGenome("", genome);
GenomeManager.getInstance().setCurrentGenome(genome);


//GenomeManager.getInstance().setCurrentGenome(Genome.NoneGenome());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/broad/igv/ui/IGVMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.broad.igv.feature.genome.GenomeUtils;
import org.broad.igv.track.AttributeManager;
import org.broad.igv.track.Track;
import org.broad.igv.ui.commandbar.GenomeComboBox;
import org.broad.igv.ui.commandbar.GenomeListManager;
import org.broad.igv.ui.commandbar.GenomeSelectionDialog;
import org.broad.igv.util.GoogleUtils;
Expand Down Expand Up @@ -435,7 +434,7 @@ private JMenu createGenomesMenu() {
JMenu menu = new JMenu("Genomes");

loadGenomeFromServerMenuItem = new JMenuItem("Download Hosted Genome...");
loadGenomeFromServerMenuItem.addActionListener(e -> GenomeComboBox.loadGenomeFromServer());
loadGenomeFromServerMenuItem.addActionListener(e -> GenomeSelectionDialog.selectGenomesFromServer());
loadGenomeFromServerMenuItem.setToolTipText(LOAD_GENOME_SERVER_TOOLTIP);
menu.add(loadGenomeFromServerMenuItem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package org.broad.igv.ui.action;

import org.broad.igv.Globals;
import org.broad.igv.feature.genome.Genome;
import org.broad.igv.feature.genome.load.HubGenomeLoader;
import org.broad.igv.logging.*;
import org.broad.igv.feature.genome.GenomeManager;
Expand Down Expand Up @@ -81,6 +82,7 @@ public void actionPerformed(ActionEvent e) {

if (!dlg.isCanceled()) {


String inputURLs = dlg.getFileURL();
if (inputURLs != null && inputURLs.trim().length() > 0) {

Expand All @@ -89,7 +91,7 @@ public void actionPerformed(ActionEvent e) {
if (inputs.length == 1 && HubGenomeLoader.isHubURL(inputs[0])) {
LongRunningTask.submit(() -> {
try {
GenomeManager.getInstance().loadGenome(inputs[0]);
Genome newGenome = GenomeManager.getInstance().loadGenome(inputs[0]);
} catch (IOException ex) {
log.error("Error loading tack hub", ex);
MessageUtils.showMessage("Error loading track hub: " + ex.getMessage());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/broad/igv/ui/action/UCSCGenArkAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

import org.broad.igv.Globals;
import org.broad.igv.feature.genome.Genome;
import org.broad.igv.feature.genome.GenomeListItem;
import org.broad.igv.feature.genome.GenomeManager;
import org.broad.igv.feature.genome.load.HubGenomeLoader;
import org.broad.igv.logging.LogManager;
import org.broad.igv.logging.Logger;
import org.broad.igv.track.AttributeManager;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.commandbar.GenomeListManager;
import org.broad.igv.ui.table.SearchableTableDialog;
import org.broad.igv.ui.table.SearchableTableModel;
import org.broad.igv.ui.table.SearchableTableRecord;
Expand Down
80 changes: 1 addition & 79 deletions src/main/java/org/broad/igv/ui/commandbar/GenomeComboBox.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package org.broad.igv.ui.commandbar;

import org.broad.igv.logging.*;
import org.broad.igv.DirectoryManager;
import org.broad.igv.event.GenomeResetEvent;
import org.broad.igv.event.IGVEventBus;
import org.broad.igv.feature.genome.GenomeListItem;
import org.broad.igv.feature.genome.GenomeManager;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.UIConstants;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.ui.util.UIUtilities;
import org.broad.igv.util.LongRunningTask;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

/**
* Created by jrobinso on 7/6/17.
Expand Down Expand Up @@ -100,7 +94,7 @@ private void loadGenomeListItem(final GenomeListItem genomeListItem) {
if (genomeListItem != null && genomeListItem.getPath() != null) {

if (genomeListItem == GenomeListItem.DOWNLOAD_ITEM) {
loadGenomeFromServer();
GenomeSelectionDialog.selectGenomesFromServer();
} else {

try {
Expand Down Expand Up @@ -197,76 +191,4 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
}


/**
* Open a selection list to load a genome from the server. This method is static because its used by multiple
* UI elements (menu bar and genome selection pulldown).
*/
public static void loadGenomeFromServer() {

Runnable showDialog = () -> {

Collection<GenomeListItem> inputListItems = GenomeListManager.getInstance().getServerGenomeList();
if (inputListItems == null) {
return;
}
GenomeSelectionDialog dialog = new GenomeSelectionDialog(IGV.getInstance().getMainFrame(), inputListItems);
UIUtilities.invokeAndWaitOnEventThread(() -> dialog.setVisible(true));

if (dialog.isCanceled()) {
IGVEventBus.getInstance().post(new GenomeResetEvent());
} else {
List<GenomeListItem> selectedValueList = dialog.getSelectedValues();
GenomeListItem firstItem = null;
for (GenomeListItem selectedValue : selectedValueList) {
if (selectedValue != null) {
boolean downloadSequence = false;
boolean success = GenomeManager.getInstance().downloadGenome(selectedValue, downloadSequence);
if (success) {
GenomeListManager.getInstance().addServerGenomeItem(selectedValue);
firstItem = selectedValue;
}
}
}
if (firstItem != null && selectedValueList.size() == 1) {
try {

GenomeManager.getInstance().loadGenome(firstItem.getPath());
// If the user has previously defined this genome, remove it.
GenomeListManager.getInstance().removeUserDefinedGenome(firstItem.getId());

// If this is a .json genome, attempt to remove existing .genome files
if (firstItem.getPath().endsWith(".json")) {
removeDotGenomeFile(firstItem.getId());
}


} catch (IOException e) {
GenomeListManager.getInstance().removeGenomeListItem(firstItem);
MessageUtils.showErrorMessage("Error loading genome " + firstItem.getDisplayableName(), e);
log.error("Error loading genome " + firstItem.getDisplayableName(), e);
}
}
}
};

if (SwingUtilities.isEventDispatchThread()) {
LongRunningTask.submit(showDialog);
} else {
showDialog.run();
}
}

public static void removeDotGenomeFile(String id) {
try {
File dotGenomeFile = new File(DirectoryManager.getGenomeCacheDirectory(), id + ".genome");
if (dotGenomeFile.exists()) {
dotGenomeFile.delete();
}
} catch (Exception e) {
// If anything goes wrong, just log it, this cleanup is not essential
log.error("Error deleting .genome file", e);
}
}


}
Loading

0 comments on commit c7ef831

Please sign in to comment.