Skip to content

Commit

Permalink
Refactor progress indication for genome loading - remove inoperable p…
Browse files Browse the repository at this point in the history
…rogress dialog, add wait cursor and status messages (#1451)
  • Loading branch information
jrobinso authored Nov 30, 2023
1 parent e458a57 commit 88f54a5
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 1,145 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/batch/CommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ private String genome(String param1) {

String genomePath = resolveFileReference(genomeID);
try {
GenomeManager.getInstance().loadGenome(genomePath, null);
GenomeManager.getInstance().loadGenome(genomePath);
} catch (IOException e) {
result = "ERROR: Could not load genome: " + genomeID;
MessageUtils.showMessage(result);
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/org/broad/igv/batch/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.ui.util.UIUtilities;
import org.broad.igv.util.LongRunningTask;
import org.broad.igv.util.StringUtils;

import java.awt.*;
Expand Down Expand Up @@ -377,14 +376,7 @@ private String processGet(String command, Map<String, String> params, CommandExe

if (command.equals("/")) {
if (params.containsKey("hubURL")) {
final String hubURL = URLDecoder.decode(params.get("hubURL")); // what if not encoded? Usually its not
LongRunningTask.submit(() -> {
try {
GenomeManager.getInstance().loadGenome(hubURL, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
GenomeManager.getInstance().loadGenome(URLDecoder.decode(params.get("hubURL")));
}
} else if (command.equals("/load")) {
String file = null;
Expand Down
197 changes: 33 additions & 164 deletions src/main/java/org/broad/igv/feature/genome/GenomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@
import org.broad.igv.track.Track;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.PanelName;
import org.broad.igv.ui.WaitCursorManager;
import org.broad.igv.ui.commandbar.GenomeListManager;
import org.broad.igv.ui.panel.FrameManager;
import org.broad.igv.ui.panel.ReferenceFrame;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.ui.util.ProgressBar;
import org.broad.igv.ui.util.ProgressMonitor;
import org.broad.igv.ui.util.UIUtilities;
import org.broad.igv.ui.util.*;
import org.broad.igv.ui.util.download.Downloader;
import org.broad.igv.util.FileUtils;
import org.broad.igv.util.HttpUtils;
Expand All @@ -73,13 +70,10 @@
import java.net.SocketException;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.Ref;
import java.util.*;
import java.util.List;

import static org.broad.igv.prefs.Constants.SHOW_SINGLE_TRACK_PANE_KEY;
import static org.broad.igv.ui.IGV.DATA_PANEL_NAME;
import static org.broad.igv.ui.IGV.FEATURE_PANEL_NAME;

/**
* @author jrobinso
Expand Down Expand Up @@ -156,45 +150,45 @@ public void setCurrentGenome(Genome genome) {


public void loadGenomeById(String genomeId) throws IOException {

final Genome currentGenome = getCurrentGenome();
if (currentGenome != null && genomeId.equals(currentGenome.getId())) {
return; // Already loaded
}

// If genomeId is a file path load it
String genomePath = null;
if (org.broad.igv.util.ParsingUtils.fileExists(genomeId)) {
loadGenome(genomeId, null);

genomePath = genomeId;
} else {
final ProgressMonitor[] monitor = {new ProgressMonitor()};
final ProgressBar.ProgressDialog[] progressDialog = new ProgressBar.ProgressDialog[1];
UIUtilities.invokeAndWaitOnEventThread(() -> {
progressDialog[0] = ProgressBar.showProgressDialog(IGV.getInstance().getMainFrame(), "Loading Genome...", monitor[0], false);
});

try {
GenomeListItem item = genomeListManager.getGenomeListItem(genomeId);
if (item == null) {
MessageUtils.showMessage("Could not locate genome with ID: " + genomeId);
} else {
loadGenome(item.getPath(), monitor[0]);
}
} finally {
UIUtilities.invokeOnEventThread(() -> {
progressDialog[0].setVisible(false);
});
GenomeListItem item = genomeListManager.getGenomeListItem(genomeId);
if (item == null) {
MessageUtils.showMessage("Could not locate genome with ID: " + genomeId);
return;
} else {
genomePath = item.getPath();
}
}

loadGenome(genomePath); // monitor[0]);

}

public Genome loadGenome(String genomePath, ProgressMonitor monitor) throws IOException {

/**
* The main load method -- loads a genome from a file or url path. Note this is a long running operation and
* should not be done on the Swing event thread as it will block the UI.
*
* @param genomePath
* @return
* @throws IOException
*/
public Genome loadGenome(String genomePath) throws IOException {

WaitCursorManager.CursorToken cursorToken = null;
try {
log.info("Loading genome: " + genomePath);

if (monitor != null) {
UIUtilities.invokeAndWaitOnEventThread(() -> monitor.fireProgress(25));
if (IGV.hasInstance()) {
IGV.getInstance().setStatusBarMessage("<html><font color=blue>Loading genome</font></html>");
cursorToken = WaitCursorManager.showWaitCursor();
}

// Clear Feature DB
Expand All @@ -215,9 +209,6 @@ public Genome loadGenome(String genomePath, ProgressMonitor monitor) throws IOEx
log.error("Failed to load user defined alias", e);
}

if (monitor != null) {
monitor.fireProgress(25);
}

if (IGV.hasInstance()) {
IGV.getInstance().resetSession(null);
Expand Down Expand Up @@ -247,6 +238,11 @@ public Genome loadGenome(String genomePath, ProgressMonitor monitor) throws IOEx

} catch (SocketException e) {
throw new RuntimeException("Server connection error", e);
} finally {
if (IGV.hasInstance()) {
IGV.getInstance().setStatusBarMessage("");
WaitCursorManager.removeWaitCursor(cursorToken);
}
}
}

Expand All @@ -267,7 +263,7 @@ public void loadGenomeAnnotations(Genome genome) {
} catch (DataLoadException e) {
log.error("Error loading genome annotations", e);
}
}//
}
genome.setAnnotationTracks(annotationTracks);
}
restoreGenomeTracks(genome);
Expand Down Expand Up @@ -324,91 +320,12 @@ public void restoreGenomeTracks(Genome genome) {
* Delete .genome files from the cache directory
*/
public void clearGenomeCache() {

File[] files = DirectoryManager.getGenomeCacheDirectory().listFiles();
for (File file : files) {
if (file.getName().toLowerCase().endsWith(Globals.GENOME_FILE_EXTENSION)) {
file.delete();
}
}

}

/**
* Create a genome archive (.genome) file.
*
* @param genomeFile
* @param cytobandFileName A File path to a file that contains cytoband data.
* @param refFlatFileName A File path to a gene file.
* @param fastaFileName A File path to a FASTA file, a .gz file containing a
* single FASTA file, or a directory containing ONLY FASTA files.
* (relative to the .genome file to be created) where the sequence data for
* the new genome will be written.
* @param genomeDisplayName The unique user-readable name of the new genome.
* @param genomeId The id to be assigned to the genome.
* @param monitor A ProgressMonitor used to track progress - null,
* if no progress updating is required.
* @return GenomeListItem
* @throws FileNotFoundException
*/
public GenomeListItem defineGenome(File genomeFile,
String cytobandFileName,
String refFlatFileName,
String fastaFileName,
String chrAliasFileName,
String genomeDisplayName,
String genomeId,
javax.swing.ProgressMonitor monitor)
throws IOException {

File refFlatFile = null;
File cytobandFile = null;
File chrAliasFile = null;

if (genomeFile != null) {
PreferencesManager.getPreferences().setLastGenomeImportDirectory(genomeFile.getParentFile());
}

if ((cytobandFileName != null) && (cytobandFileName.trim().length() != 0)) {
cytobandFile = new File(cytobandFileName);
}

if ((refFlatFileName != null) && (refFlatFileName.trim().length() != 0)) {
refFlatFile = new File(refFlatFileName);
}

if ((chrAliasFileName != null) && (chrAliasFileName.trim().length() != 0)) {
chrAliasFile = new File(chrAliasFileName);
}

if (monitor != null) monitor.setProgress(25);

(new GenomeImporter()).createGenomeArchive(genomeFile, genomeId,
genomeDisplayName, fastaFileName, refFlatFile, cytobandFile, chrAliasFile);

if (monitor != null) monitor.setProgress(75);

GenomeListItem newItem = new GenomeListItem(genomeDisplayName, genomeFile.getAbsolutePath(), genomeId);
genomeListManager.addGenomeItem(newItem, true);

if (monitor != null) monitor.setProgress(100);

return newItem;

}

/**
* Specific to Broad Amazon servers -- use S3 downwload rather than cloudfront
*
* @param path
* @return
*/
private String convertToS3(String path) {
if (path.startsWith("http://igvdata") || path.startsWith("https://igvdata")) {
return path.replaceFirst("igvdata", "igv");
} else {
return path;
}
}

public String getGenomeId() {
Expand All @@ -426,54 +343,6 @@ public Genome getCurrentGenome() {
return currentGenome;
}

/**
* Given a directory, looks for all .genome files,
* and outputs a list of these genomes suitable for parsing by IGV.
* Intended to be run on server periodically.
*
* @param inDir Directory in which all genome files live
* @param rootPath The path to be prepended to file names (e.g. http://igvdata.broadinstitute.org)
* @param outPath Path to output file, where we will write the results
*/
public void generateGenomeList(File inDir, String rootPath, String outPath) {
File[] genomeFiles = inDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name == null) return false;
return name.toLowerCase().endsWith(".genome");
}
});

PrintWriter writer;
try {
writer = new PrintWriter(outPath);
} catch (FileNotFoundException e) {
log.error("Error opening " + outPath);
e.printStackTrace();
return;
}


GenomeDescriptor descriptor;
for (File f : genomeFiles) {
String curLine = "";
try {
descriptor = GenomeDescriptor.parseGenomeArchiveFile(f);
curLine += descriptor.getName();
curLine += "\t" + rootPath + "/" + f.getName();
curLine += "\t" + descriptor.getId();
} catch (IOException e) {
log.error("Error parsing genome file. Skipping " + f.getAbsolutePath());
log.error(e);
continue;
}
writer.println(curLine);
}

writer.close();

}


public boolean downloadGenome(GenomeListItem item, boolean downloadSequence) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void updateChromSizes(String genomeListPath, File directory) throw
System.out.println("Updating " + genomeID);
String genomePath = tokens[1];
try {
Genome genome = GenomeManager.getInstance().loadGenome(genomePath, null);
Genome genome = GenomeManager.getInstance().loadGenome(genomePath);
System.out.println(genome.getId());
exportChromSizes(directory, genome);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/broad/igv/session/IGVSessionReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ private void processRootNode(Session session, Node node, String sessionPath) {
GenomeListItem item = GenomeListManager.getInstance().getGenomeListItem(genomeId);
if (item != null) {
genomePath = item.getPath();
GenomeManager.getInstance().loadGenome(item.getPath(), null);
GenomeManager.getInstance().loadGenome(item.getPath());
} else {
genomePath = genomeId;
if (!FileUtils.isRemote(genomePath) && !ParsingUtils.fileExists(genomePath)) {
genomePath = getAbsolutePath(genomeId, sessionPath);
}
GenomeManager.getInstance().loadGenome(genomePath, null);
GenomeManager.getInstance().loadGenome(genomePath);
}
} catch (IOException e) {
MessageUtils.showErrorMessage("Error loading genome: " + genomeId, e);
Expand Down
Loading

0 comments on commit 88f54a5

Please sign in to comment.