public void generateTrackFromLocation(int mousePos, boolean isHorizontal) { if (!MatrixType.isObservedOrControl(displayOption)) { MessageUtils.showMessage("This feature is only available for Observed or Control views"); return; } // extract the starting position int binStartPosition = (int) (getXContext().getBinOrigin() + mousePos / getScaleFactor()); if (isHorizontal) binStartPosition = (int) (getYContext().getBinOrigin() + mousePos / getScaleFactor()); // Initialize default file name String filename = displayOption == MatrixType.OBSERVED ? "obs" : "ctrl"; filename += isHorizontal ? "_horz" : "_vert"; filename += "_bin" + binStartPosition + "_res" + currentZoom.getBinSize(); filename = cleanUpNumbersInName(filename); // allow user to customize or change the name filename = MessageUtils.showInputDialog("Enter a name for the resulting .wig file", filename); if (filename == null || filename.equalsIgnoreCase("null")) return; File outputWigFile = new File(DirectoryManager.getHiCDirectory(), filename + ".wig"); MessageUtils.showMessage("Data will be saved to " + outputWigFile.getAbsolutePath()); Chromosome chromosomeForPosition = getXContext().getChromosome(); if (isHorizontal) chromosomeForPosition = getYContext().getChromosome(); safeSave1DTrackToWigFile(chromosomeForPosition, outputWigFile, binStartPosition); }
public List<Feature> getFeatures(final String chr) { List<Feature> features = featureCache.get(chr); if (features == null) { final String filename = fileMap.getProperty(chr); if (filename != null) { BufferedReader reader = null; String path = rootDir + "/" + filename; try { log.info("Loading " + path); // Load features here ResourceLocator loc = new ResourceLocator(path); FeatureParser fp = AbstractFeatureParser.getInstanceFor(loc, genome); reader = ParsingUtils.openBufferedReader(loc); features = fp.loadFeatures(reader, genome); featureCache.put(chr, features); } catch (IOException ex) { MessageUtils.showMessage("Error loading file: " + path + " (" + ex.toString() + ")"); log.info("Error loading feature file: " + filename, ex); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } } } return featureCache.get(chr); }
public void drop(DropTargetDropEvent event) { if (!isDropAcceptable(event)) { event.rejectDrop(); return; } event.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = event.getTransferable(); MessageCollection messages = new MessageCollection(); try { List<File> files = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor); for (File file : files) { try { ResourceLocator locator = new ResourceLocator(file.getAbsolutePath()); IGV.getInstance().load(locator, panel); } catch (DataLoadException de) { messages.append(de.getMessage()); } } String obj = transferable.getTransferData(DataFlavor.stringFlavor).toString(); if (HttpUtils.isRemoteURL(obj)) { IGV.getInstance().load(new ResourceLocator(obj), panel); } if (messages != null && !messages.isEmpty()) { log.error(messages.getFormattedMessage()); MessageUtils.showMessage(messages.getFormattedMessage()); } } catch (Exception e) { String obj = null; try { obj = transferable.getTransferData(DataFlavor.stringFlavor).toString(); if (HttpUtils.isRemoteURL(obj)) { IGV.getInstance().load(new ResourceLocator(obj), panel); } } catch (Exception e1) { log.error(e1); if (messages != null && !messages.isEmpty()) { MessageUtils.showMessage(messages.getFormattedMessage()); } } } IGV.getMainFrame().repaint(); event.dropComplete(true); }
private void loadGMT(ResourceLocator locator) throws IOException { List<GeneList> lists = GeneListManager.getInstance().loadGMTFile(locator.getPath()); if (lists.size() == 1) { GeneList gl = lists.get(0); IGV.getInstance().setGeneList(gl, true); } else { MessageUtils.showMessage("Loaded " + lists.size() + " gene lists."); } }
private void unsafeInitializeGeneHashMap(String genomeID) { // Custom format parsed from ref Gene file. // Name1 Name2 chromosome position (where position is midpoint of transcription start and end) String path = "http://hicfiles.s3.amazonaws.com/internal/" + genomeID + "_refGene.txt"; BufferedReader reader; try { SeekableHTTPStream stream = new SeekableHTTPStream(new URL(path)); reader = new BufferedReader(new InputStreamReader(stream), HiCGlobals.bufferSize); MessageUtils.showMessage( "Loading gene database for " + genomeID + ".\nIt might take a minute or so. "); } catch (Exception error) { MessageUtils.showErrorMessage("Failed to read gene database", error); positionChrTop.setBackground(Color.yellow); geneLocationHashMap = null; return; } geneLocationHashMap = new HashMap<String, GeneLocation>(); String nextLine; try { while ((nextLine = reader.readLine()) != null) { String[] values = nextLine.split(" "); GeneLocation location = new GeneLocation(values[2].trim(), Integer.valueOf(values[3].trim())); geneLocationHashMap.put(values[0].trim().toLowerCase(), location); geneLocationHashMap.put(values[1].trim().toLowerCase(), location); } } catch (Exception error) { MessageUtils.showErrorMessage("Failed to parse gene database", error); positionChrTop.setBackground(Color.yellow); geneLocationHashMap = null; } if (geneLocationHashMap != null) this.genomeID = genomeID; extractGeneLocation(); }
private void extractGeneLocation() { GeneLocation location1 = geneLocationHashMap.get(positionChrTop.getText().trim().toLowerCase()); GeneLocation location2 = geneLocationHashMap.get(positionChrLeft.getText().trim().toLowerCase()); if (location1 == null) { positionChrTop.setBackground(Color.yellow); MessageUtils.showMessage( "Gene location map doesn't contain " + positionChrTop.getText().trim()); return; } if (location2 == null) { positionChrLeft.setBackground(Color.yellow); MessageUtils.showMessage( "Gene location map doesn't contain " + positionChrLeft.getText().trim()); return; } List<Integer> bpResolutions = Ints.asList(HiCGlobals.bpBinSizes); int geneZoomResolution = hic.getZoom().getBinSize(); if (!bpResolutions.contains(geneZoomResolution)) { geneZoomResolution = Collections.min(bpResolutions); } hic.setLocation( location1.chromosome, location2.chromosome, HiC.Unit.BP, geneZoomResolution, location1.centerPosition, location2.centerPosition, hic.getScaleFactor(), HiC.ZoomCallType.STANDARD, "Gene Goto", true); superAdapter.setNormalizationDisplayState(); }
private void loadGctFile(ResourceLocator locator, List<Track> newTracks, Genome genome) throws IOException { if (locator.isLocal()) { if (!checkSize(locator)) { return; } } ExpressionFileParser parser = null; ExpressionDataset ds = null; parser = new ExpressionFileParser(locator, null, genome); ds = parser.createDataset(); if (ds.isEmpty()) { String message = "The probes in the file <br> " + locator.getPath() + "<br>" + "could not be mapped to genomic positions. This can be corrected by specify a probe mapping<br>" + "file from the Preferences window (Probes tab), or by specifing the genomic positions in the<br>" + "expression data file. Please see the user guide for more details."; MessageUtils.showMessage(message); } else { ds.setName(locator.getTrackName()); ds.setNormalized(true); ds.setLogValues(true); /* * File outputFile = new File(IGV.DEFAULT_USER_DIRECTORY, file.getName() + ".h5"); * OverlappingProcessor proc = new OverlappingProcessor(ds); * proc.setZoomMax(0); * proc.process(outputFile.getAbsolutePath()); * loadH5File(outputFile, messages, attributeList, group); */ // Counter for generating ID TrackProperties trackProperties = ds.getTrackProperties(); String path = locator.getPath(); for (String trackName : ds.getTrackNames()) { DatasetDataSource dataSource = new DatasetDataSource(trackName, ds, genome); String trackId = path + "_" + trackName; Track track = new DataSourceTrack(locator, trackId, trackName, dataSource); track.setRendererClass(HeatmapRenderer.class); track.setProperties(trackProperties); newTracks.add(track); } } }
private void notifyGenomesAddedRemoved(List<GenomeListItem> selectedValues, boolean added) { if (selectedValues == null || selectedValues.size() == 0) return; int size = selectedValues.size(); String msg = ""; if (size == 1) { msg += selectedValues.get(0) + " genome"; } else { msg += size + " genomes"; } if (added) { msg += " added to"; } else { msg += " removed from"; } msg += " list"; MessageUtils.setStatusBarMessage(msg); }
private void initializeGeneHashMap(String genomeID) { if (genomeID.equals("hg19") || genomeID.equals("hg38") || genomeID.equals("mm9") || genomeID.equals("mm10")) { final String gID = genomeID; Runnable runnable = new Runnable() { @Override public void run() { unsafeInitializeGeneHashMap(gID); } }; superAdapter.executeLongRunningTask(runnable, "Initialize Gene Hash Map"); } else { MessageUtils.showErrorMessage("Cannot find genes for " + genomeID, null); positionChrTop.setBackground(Color.yellow); geneLocationHashMap = null; } }
/** * Compare the sequence names against sequence (chromosome) names in the genome. If no matches * warn the user. * * @param filename * @param genome * @param seqNames * @return true if there is at least one sequence match, false otherwise */ private boolean checkSequenceNames(String filename, Genome genome, List<String> seqNames) { boolean atLeastOneMatch = false; for (String seqName : seqNames) { if (genome.getChromosome(seqName) != null) { atLeastOneMatch = true; break; } } if (!atLeastOneMatch) { StringBuffer message = new StringBuffer(); message.append( "<html>File: " + filename + "<br>does not contain any sequence names which match the current genome."); message.append("<br><br>File: "); int n = 0; for (String sn : seqNames) { message.append(sn + ", "); n++; if (n > 3) { message.append(" ..."); break; } } message.append("<br>Genome: "); n = 0; for (String cn : genome.getAllChromosomeNames()) { message.append(cn + ", "); n++; if (n > 3) { message.append(" ..."); break; } } MessageUtils.showMessage(message.toString()); } return atLeastOneMatch; }
private static ConnectionWrapper connect(String url) { try { return new ConnectionWrapper(DriverManager.getConnection(url, username, password)); } catch (SQLException e) { int errorCode = e.getErrorCode(); if (errorCode == 1044 || errorCode == 1045) { String host = PreferenceManager.getInstance().get(PreferenceManager.DB_HOST); Frame parent = Globals.isHeadless() ? null : IGV.getMainFrame(); LoginDialog dlg = new LoginDialog(parent, false, host, false); dlg.setVisible(true); if (dlg.isCanceled()) { throw new RuntimeException("Must login to access" + host); } username = dlg.getUsername(); password = new String(dlg.getPassword()); return connect(url); } else { MessageUtils.showMessage("<html>Error connecting to database: <br>" + e.getMessage()); return null; } } }
/** * Switches on various attributes of locator (mainly locator path extension and whether the * locator is indexed) to call the appropriate loading method. * * @param locator * @param genome * @return */ public List<Track> load(ResourceLocator locator, Genome genome) throws DataLoadException { final String path = locator.getPath().trim(); log.info("Loading resource, path " + path); try { String typeString = locator.getTypeString(); if (typeString.endsWith(".tbi")) { MessageUtils.showMessage( "<html><b>Error:</b>File type '.tbi' is not recognized. If this is a 'tabix' index <br>" + " load the associated gzipped file, which should have an extension of '.gz'"); } // This list will hold all new tracks created for this locator List<Track> newTracks = new ArrayList<Track>(); String dbUrl = locator.getDBUrl(); LoadHandler handler = getTrackLoaderHandler(typeString); if (dbUrl != null) { this.loadFromDatabase(locator, newTracks, genome); } else if (typeString.endsWith(".dbxml")) { loadFromDBProfile(locator, newTracks); } else if (typeString.endsWith(".gmt")) { loadGMT(locator); } else if (typeString.equals("das")) { loadDASResource(locator, newTracks); } else if (typeString.endsWith(".vcf.list")) { loadVCFListFile(locator, newTracks, genome); } else if (typeString.endsWith(".trio")) { loadTrioData(locator); } else if (typeString.endsWith("varlist")) { VariantListManager.loadVariants(locator); } else if (typeString.endsWith("samplepathmap")) { VariantListManager.loadSamplePathMap(locator); } else if (typeString.endsWith(".rnai.gct")) { loadRnaiGctFile(locator, newTracks, genome); } else if (typeString.endsWith(".gct") || typeString.endsWith("res") || typeString.endsWith("tab")) { loadGctFile(locator, newTracks, genome); } else if (typeString.endsWith(".gbk") || typeString.endsWith(".gb")) { loadGbkFile(locator, newTracks, genome); } else if (typeString.endsWith(".cn") || typeString.endsWith(".xcn") || typeString.endsWith(".snp") || typeString.endsWith(".igv") || typeString.endsWith(".loh")) { loadIGVFile(locator, newTracks, genome); } else if (typeString.endsWith(".cbs") || typeString.endsWith(".seg") || typeString.endsWith("glad") || typeString.endsWith("birdseye_canary_calls") || typeString.endsWith(".seg.zip")) { loadSegFile(locator, newTracks, genome); } else if (typeString.endsWith(".gistic")) { loadGisticFile(locator, newTracks); } else if (typeString.endsWith(".gs")) { loadRNAiGeneScoreFile(locator, newTracks, RNAIGeneScoreParser.Type.GENE_SCORE, genome); } else if (typeString.endsWith(".riger")) { loadRNAiGeneScoreFile(locator, newTracks, RNAIGeneScoreParser.Type.POOLED, genome); } else if (typeString.endsWith(".hp")) { loadRNAiHPScoreFile(locator); } else if (typeString.contains(".tabblastn") || typeString.endsWith(".orthologs")) { loadSyntentyMapping(locator, newTracks); } else if (typeString.endsWith(".sam") || typeString.endsWith(".bam") || typeString.endsWith(".cram") || typeString.endsWith(".sam.list") || typeString.endsWith(".bam.list") || typeString.endsWith(".aligned") || typeString.endsWith(".sai") || typeString.endsWith(".bai") || typeString.equals("alist") || typeString.equals(Ga4ghAPIHelper.RESOURCE_TYPE)) { loadAlignmentsTrack(locator, newTracks, genome); } else if (typeString.endsWith(".wig") || typeString.endsWith(".bedgraph") || typeString.endsWith(".bdg") || typeString.endsWith("cpg.txt") || typeString.endsWith(".expr")) { loadWigFile(locator, newTracks, genome); } else if (typeString.endsWith("fpkm_tracking") || typeString.endsWith("gene_exp.diff") || typeString.endsWith("cds_exp.diff")) { loadCufflinksFile(locator, newTracks, genome); } else if (typeString.contains(".dranger")) { loadDRangerFile(locator, newTracks, genome); } else if (typeString.endsWith(".ewig.tdf") || (typeString.endsWith(".ewig.ibf"))) { loadEwigIBFFile(locator, newTracks, genome); } else if (typeString.endsWith(".bw") || typeString.endsWith(".bb") || typeString.endsWith(".bigwig") || typeString.endsWith(".bigbed")) { loadBWFile(locator, newTracks, genome); } else if (typeString.endsWith(".ibf") || typeString.endsWith(".tdf")) { loadTDFFile(locator, newTracks, genome); } else if (typeString.endsWith(".counts")) { loadGobyCountsArchive(locator, newTracks, genome); } else if (WiggleParser.isWiggle(locator)) { loadWigFile(locator, newTracks, genome); } else if (typeString.endsWith(".maf")) { loadMultipleAlignmentTrack(locator, newTracks, genome); } else if (typeString.endsWith(".maf.dict")) { loadMultipleAlignmentTrack(locator, newTracks, genome); } else if (typeString.contains(".peak.bin")) { loadPeakTrack(locator, newTracks, genome); } else if (typeString.endsWith("mage-tab") || ExpressionFileParser.parsableMAGE_TAB(locator)) { locator.setDescription("MAGE_TAB"); loadGctFile(locator, newTracks, genome); } else if (typeString.endsWith(".bp")) { loadBasePairFile(locator, newTracks, genome); } else if (GWASParser.isGWASFile(typeString)) { loadGWASFile(locator, newTracks, genome); } else if (GobyAlignmentQueryReader.supportsFileType(path)) { loadAlignmentsTrack(locator, newTracks, genome); } else if (typeString.endsWith(".list")) { // This should be deprecated loadListFile(locator, newTracks, genome); } else if (typeString.endsWith(".smap")) { loadSMAPFile(locator, newTracks, genome); } else if (CodecFactory.hasCodec(locator, genome) && !forceNotTribble(typeString)) { loadTribbleFile(locator, newTracks, genome); } else if (handler != null) { // Custom loader specified log.info(String.format("Loading %s with %s", path, handler)); handler.load(path, newTracks); } else if (AttributeManager.isSampleInfoFile(locator)) { // This might be a sample information file. AttributeManager.getInstance().loadSampleInfo(locator); } else { MessageUtils.showMessage("<html>Unknown file type: " + path + "<br>Check file extension"); } // Track line TrackProperties tp = null; String trackLine = locator.getTrackLine(); if (trackLine != null) { tp = new TrackProperties(); ParsingUtils.parseTrackLine(trackLine, tp); } for (Track track : newTracks) { if (locator.getFeatureInfoURL() != null) { track.setUrl(locator.getFeatureInfoURL()); } if (tp != null) { track.setProperties(tp); } if (locator.getColor() != null) { track.setColor(locator.getColor()); } if (locator.getSampleId() != null) { track.setSampleId(locator.getSampleId()); } } return newTracks; } catch (Exception e) { if (!NOLogExceptions.contains(e.getClass())) { log.error(e.getMessage(), e); } throw new DataLoadException(e.getMessage()); } }
private void processResource(Session session, Element element, HashMap additionalInformation) { String nodeName = element.getNodeName(); boolean oldSession = nodeName.equals(SessionElement.DATA_FILE.getText()); String label = getAttribute(element, SessionAttribute.LABEL.getText()); String name = getAttribute(element, SessionAttribute.NAME.getText()); String sampleId = getAttribute(element, SessionAttribute.SAMPLE_ID.getText()); String description = getAttribute(element, SessionAttribute.DESCRIPTION.getText()); String type = getAttribute(element, SessionAttribute.TYPE.getText()); String coverage = getAttribute(element, SessionAttribute.COVERAGE.getText()); String trackLine = getAttribute(element, SessionAttribute.TRACK_LINE.getText()); String colorString = getAttribute(element, SessionAttribute.COLOR.getText()); String relPathValue = getAttribute(element, SessionAttribute.RELATIVE_PATH.getText()); boolean isRelativePath = ((relPathValue != null) && relPathValue.equalsIgnoreCase("true")); String serverURL = getAttribute(element, SessionAttribute.SERVER_URL.getText()); // Older sessions used the "name" attribute for the path. String path = getAttribute(element, SessionAttribute.PATH.getText()); if (oldSession && name != null) { path = name; int idx = name.lastIndexOf("/"); if (idx > 0 && idx + 1 < name.length()) { name = name.substring(idx + 1); } } ResourceLocator resourceLocator; if (isRelativePath) { final String sessionPath = session.getPath(); String absolutePath; if (sessionPath == null) { log.error("Null session path -- this is not expected"); MessageUtils.showMessage("Unexpected error loading session: null session path"); return; } absolutePath = FileUtils.getAbsolutePath(path, sessionPath); fullToRelPathMap.put(absolutePath, path); resourceLocator = new ResourceLocator(serverURL, absolutePath); // If the resourceLocator is relative, we assume coverage is as well if (coverage != null) { String absoluteCoveragePath = FileUtils.getAbsolutePath(coverage, sessionPath); resourceLocator.setCoverage(absoluteCoveragePath); } } else { resourceLocator = new ResourceLocator(serverURL, path); resourceLocator.setCoverage(coverage); } String url = getAttribute(element, SessionAttribute.URL.getText()); if (url == null) { url = getAttribute(element, SessionAttribute.FEATURE_URL.getText()); } resourceLocator.setUrl(url); String infolink = getAttribute(element, SessionAttribute.HYPERLINK.getText()); if (infolink == null) { infolink = getAttribute(element, SessionAttribute.INFOLINK.getText()); } resourceLocator.setInfolink(infolink); // Label is deprecated in favor of name. if (name != null) { resourceLocator.setName(name); } else { resourceLocator.setName(label); } resourceLocator.setSampleId(sampleId); resourceLocator.setDescription(description); // This test added to get around earlier bug in the writer if (type != null && !type.equals("local")) { resourceLocator.setType(type); } resourceLocator.setCoverage(coverage); resourceLocator.setTrackLine(trackLine); if (colorString != null) { try { Color c = ColorUtilities.stringToColor(colorString); resourceLocator.setColor(c); } catch (Exception e) { log.error("Error setting color: ", e); } } dataFiles.add(resourceLocator); NodeList elements = element.getChildNodes(); process(session, elements, additionalInformation); }
private void processResources(Session session, Element element, HashMap additionalInformation) { dataFiles = new ArrayList(); missingDataFiles = new ArrayList(); NodeList elements = element.getChildNodes(); process(session, elements, additionalInformation); if (missingDataFiles.size() > 0) { StringBuffer message = new StringBuffer(); message.append("<html>The following data file(s) could not be located.<ul>"); for (ResourceLocator file : missingDataFiles) { if (file.isLocal()) { message.append("<li>"); message.append(file.getPath()); message.append("</li>"); } else { message.append("<li>Server: "); message.append(file.getServerURL()); message.append(" Path: "); message.append(file.getPath()); message.append("</li>"); } } message.append("</ul>"); message.append("Common reasons for this include: "); message.append("<ul><li>The session or data files have been moved.</li> "); message.append( "<li>The data files are located on a drive that is not currently accessible.</li></ul>"); message.append("</html>"); MessageUtils.showMessage(message.toString()); } if (dataFiles.size() > 0) { final List<String> errors = new ArrayList<String>(); // Load files concurrently -- TODO, put a limit on # of threads? List<Thread> threads = new ArrayList(dataFiles.size()); long t0 = System.currentTimeMillis(); int i = 0; List<Runnable> synchronousLoads = new ArrayList<Runnable>(); for (final ResourceLocator locator : dataFiles) { final String suppliedPath = locator.getPath(); final String relPath = fullToRelPathMap.get(suppliedPath); Runnable runnable = new Runnable() { public void run() { List<Track> tracks = null; try { tracks = igv.load(locator); for (Track track : tracks) { if (track == null) { log.info("Null track for resource " + locator.getPath()); continue; } String id = track.getId(); if (id == null) { log.info("Null track id for resource " + locator.getPath()); continue; } if (relPath != null) { id = id.replace(suppliedPath, relPath); } List<Track> trackList = trackDictionary.get(id); if (trackList == null) { trackList = new ArrayList(); trackDictionary.put(id, trackList); } trackList.add(track); } } catch (Exception e) { log.error("Error loading resource " + locator.getPath(), e); String ms = "<b>" + locator.getPath() + "</b><br>&nbs;p " + e.toString() + "<br>"; errors.add(ms); } } }; boolean isAlignment = locator.getPath().endsWith(".bam") || locator.getPath().endsWith(".entries") || locator.getPath().endsWith(".sam"); // Run synchronously if in batch mode or if there are no "track" elments, or if this is an // alignment file if (isAlignment || Globals.isBatch() || !hasTrackElments) { synchronousLoads.add(runnable); } else { Thread t = new Thread(runnable); threads.add(t); t.start(); } i++; } // Wait for all threads to complete for (Thread t : threads) { try { t.join(); } catch (InterruptedException ignore) { } } // Now load data that must be loaded synchronously for (Runnable runnable : synchronousLoads) { runnable.run(); } long dt = System.currentTimeMillis() - t0; log.debug("Total load time = " + dt); if (errors.size() > 0) { StringBuffer buf = new StringBuffer(); buf.append("<html>Errors were encountered loading the session:<br>"); for (String msg : errors) { buf.append(msg); } MessageUtils.showMessage(buf.toString()); } } dataFiles = null; }
private void processRootNode(Session session, Node node, HashMap additionalInformation) { if ((node == null) || (session == null)) { MessageUtils.showMessage("Invalid session file: root node not found"); return; } String nodeName = node.getNodeName(); if (!(nodeName.equalsIgnoreCase(SessionElement.GLOBAL.getText()) || nodeName.equalsIgnoreCase(SessionElement.SESSION.getText()))) { MessageUtils.showMessage( "Session files must begin with a \"Global\" or \"Session\" element. Found: " + nodeName); } process(session, node, additionalInformation); Element element = (Element) node; // Load the genome, which can be an ID, or a path or URL to a .genome or indexed fasta file. String genomeId = getAttribute(element, SessionAttribute.GENOME.getText()); if (genomeId != null && genomeId.length() > 0) { if (genomeId.equals(GenomeManager.getInstance().getGenomeId())) { // We don't have to reload the genome, but the gene track for the current genome should be // restored. Genome genome = GenomeManager.getInstance().getCurrentGenome(); IGV.getInstance().setGenomeTracks(genome.getGeneTrack()); } else { // Selecting a genome will actually "reset" the session so we have to // save the path and restore it. String sessionPath = session.getPath(); if (IGV.getInstance().getGenomeIds().contains(genomeId)) { IGV.getInstance().selectGenomeFromList(genomeId); } else { String genomePath = genomeId; if (!ParsingUtils.pathExists(genomePath)) { genomePath = FileUtils.getAbsolutePath(genomeId, session.getPath()); } if (ParsingUtils.pathExists(genomePath)) { try { IGV.getInstance().loadGenome(genomePath, null); } catch (IOException e) { throw new RuntimeException("Error loading genome: " + genomeId); } } else { MessageUtils.showMessage("Warning: Could not locate genome: " + genomeId); } } session.setPath(sessionPath); } } session.setLocus(getAttribute(element, SessionAttribute.LOCUS.getText())); session.setGroupTracksBy(getAttribute(element, SessionAttribute.GROUP_TRACKS_BY.getText())); String removeEmptyTracks = getAttribute(element, "removeEmptyTracks"); if (removeEmptyTracks != null) { try { Boolean b = Boolean.parseBoolean(removeEmptyTracks); session.setRemoveEmptyPanels(b); } catch (Exception e) { log.error("Error parsing removeEmptyTracks string: " + removeEmptyTracks, e); } } String versionString = getAttribute(element, SessionAttribute.VERSION.getText()); try { version = Integer.parseInt(versionString); } catch (NumberFormatException e) { log.error("Non integer version number in session file: " + versionString); } session.setVersion(version); NodeList elements = element.getChildNodes(); process(session, elements, additionalInformation); // ReferenceFrame.getInstance().invalidateLocationScale(); }
private void loadAlignmentsTrack(ResourceLocator locator, List<Track> newTracks, Genome genome) throws IOException { try { String dsName = locator.getTrackName(); // If the user tried to load the index, look for the file (this is a common mistake) if (locator.getTypeString().endsWith(".sai") || locator.getTypeString().endsWith(".bai")) { MessageUtils.showMessage( "<html><b>ERROR:</b> Loading SAM/BAM index files are not supported: " + locator.getPath() + "<br>Load the SAM or BAM file directly. "); return; } AlignmentDataManager dataManager = new AlignmentDataManager(locator, genome); // Check that alignments we loaded actually match some data. Many BAM files will contain some // sequences // not represented in the genome, buf if there are no matches warn the user. List<String> seqNames = dataManager.getSequenceNames(); if (seqNames != null && seqNames.size() > 0) { if (!checkSequenceNames(locator.getPath(), genome, seqNames)) { return; } } if (locator.getTypeString().endsWith("bam") || locator.getTypeString().endsWith("cram")) { if (!dataManager.hasIndex()) { MessageUtils.showMessage( "<html>Could not load index file for: " + locator.getPath() + "<br> An index file is required for SAM & BAM files."); return; } } AlignmentTrack alignmentTrack = new AlignmentTrack(locator, dataManager, genome); // parser.loadTrack(locator, dsName); alignmentTrack.setName(dsName); alignmentTrack.setVisible( PreferenceManager.getInstance().getAsBoolean(PreferenceManager.SAM_SHOW_ALIGNMENT_TRACK)); // Create coverage track CoverageTrack covTrack = new CoverageTrack(locator, dsName + " Coverage", alignmentTrack, genome); covTrack.setVisible( PreferenceManager.getInstance().getAsBoolean(PreferenceManager.SAM_SHOW_COV_TRACK)); newTracks.add(covTrack); covTrack.setDataManager(dataManager); dataManager.setCoverageTrack(covTrack); alignmentTrack.setCoverageTrack(covTrack); // Search for precalculated coverage data // Skip for GA4GH & SU2C resources if (!(Ga4ghAPIHelper.RESOURCE_TYPE.equals(locator.getType()) || locator.getPath().contains("dataformat=.bam") || OAuthUtils.isGoogleCloud(locator.getPath()))) { String covPath = locator.getCoverage(); if (covPath == null) { boolean bypassFileAutoDiscovery = PreferenceManager.getInstance() .getAsBoolean(PreferenceManager.BYPASS_FILE_AUTO_DISCOVERY); String path = locator.getPath(); if (!bypassFileAutoDiscovery && !path.contains("/query.cgi?")) { covPath = path + ".tdf"; } } if (covPath != null) { if (FileUtils.resourceExists(covPath)) { log.debug("Loading TDF for coverage: " + covPath); try { TDFReader reader = TDFReader.getReader(covPath); TDFDataSource ds = new TDFDataSource(reader, 0, dsName + " coverage", genome); covTrack.setDataSource(ds); } catch (Exception e) { log.error("Error loading coverage TDF file", e); } } } } boolean showSpliceJunctionTrack = PreferenceManager.getInstance().getAsBoolean(PreferenceManager.SAM_SHOW_JUNCTION_TRACK); SpliceJunctionTrack spliceJunctionTrack = new SpliceJunctionTrack( locator, dsName + " Junctions", dataManager, alignmentTrack, SpliceJunctionTrack.StrandOption.BOTH); spliceJunctionTrack.setHeight(60); spliceJunctionTrack.setVisible(showSpliceJunctionTrack); newTracks.add(spliceJunctionTrack); alignmentTrack.setSpliceJunctionTrack(spliceJunctionTrack); newTracks.add(alignmentTrack); log.debug("Alignment track loaded"); } catch (IndexNotFoundException e) { MessageUtils.showMessage( "<html>Could not find the index file for <br><br> " + e.getSamFile() + "<br><br>Note: The index file can be created using igvtools and must be in the same directory as the .sam file."); } }
/** * Create an index for an alignment or feature file The output index will have the same base name * is the input file, although it may be in a different directory. An appropriate index extension * (.sai, .idx, etc.) will be appended. * * @param ifile * @param typeString * @param outputDir * @param indexType * @param binSize * @throws IOException */ public String doIndex( String ifile, String typeString, String outputDir, int indexType, int binSize) throws IOException { File inputFile = new File(ifile); if (outputDir == null) { outputDir = inputFile.getParent(); } String outputFileName = (new File(outputDir, inputFile.getName())).getAbsolutePath(); if (typeString.endsWith("gz")) { System.out.println("Cannot index a gzipped file"); throw new PreprocessingException("Cannot index a gzipped file"); } if (typeString.endsWith("bam")) { String msg = "Cannot index a BAM file. Use the samtools package for sorting and indexing BAM files."; System.out.println(msg); throw new PreprocessingException(msg); } // We have different naming conventions for different index files if (typeString.endsWith("sam") && !outputFileName.endsWith(".sai")) { outputFileName += ".sai"; } else if (typeString.endsWith("bam") && !outputFileName.endsWith(".bai")) { outputFileName += ".bai"; } else if (typeString.endsWith("fa") && !outputFileName.endsWith(".fai")) { outputFileName += ".fai"; } else if (typeString.endsWith("fasta") && !outputFileName.endsWith(".fai")) { outputFileName += ".fai"; } else if (!typeString.endsWith("sam") && !typeString.endsWith("bam") && !outputFileName.endsWith(".idx")) { outputFileName += ".idx"; } File outputFile = new File(outputFileName); // Sam/FASTA files are special try { if (typeString.endsWith("sam")) { AlignmentIndexer indexer = AlignmentIndexer.getInstance(inputFile, null, null); indexer.createSamIndex(outputFile); return outputFileName; } else if (typeString.equals(".fa") || typeString.equals(".fasta")) { FastaUtils.createIndexFile(inputFile.getAbsolutePath(), outputFileName); return outputFileName; } } catch (Exception e) { e.printStackTrace(); // Delete output file as it is probably corrupt if (outputFile.exists()) { outputFile.delete(); } } Genome genome = null; // <= don't do chromosome conversion FeatureCodec codec = CodecFactory.getCodec(ifile, genome); if (codec != null) { try { createTribbleIndex(ifile, outputFile, indexType, binSize, codec); } catch (TribbleException.MalformedFeatureFile e) { StringBuffer buf = new StringBuffer(); buf.append("<html>Files must be sorted by start position prior to indexing.<br>"); buf.append(e.getMessage()); buf.append( "<br><br>Note: igvtools can be used to sort the file, select \"File > Run igvtools...\"."); MessageUtils.showMessage(buf.toString()); } } else { throw new DataLoadException("Unknown File Type", ifile); } System.out.flush(); return outputFileName; }
private void parseColumnLine(String tmp) { String[] tokens = tmp.split("\\s+"); String neg_colnum = "Error parsing column line: " + tmp + "<br>Column numbers must be > 0"; if (tokens.length > 1) { for (int i = 1; i < tokens.length; i++) { String[] kv = tokens[i].split("="); if (kv.length == 2) { if (kv[0].toLowerCase().equals("chr")) { int c = Integer.parseInt(kv[1]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { chrColumn = c - 1; } } else if (kv[0].toLowerCase().equals("start")) { int c = Integer.parseInt(kv[1]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { startColumn = c - 1; } } else if (kv[0].toLowerCase().equals("end")) { int c = Integer.parseInt(kv[1]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { endColumn = c - 1; hasEndLocations = true; } } else if (kv[0].toLowerCase().equals("probe")) { int c = Integer.parseInt(kv[1]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { probeColumn = c - 1; } } else if (kv[0].toLowerCase().equals("data")) { // examples 4, 4-8, 4- String[] se = kv[1].split("-"); int c = Integer.parseInt(se[0]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { this.firstDataColumn = c - 1; } if (se.length > 1) { c = Integer.parseInt(se[1]); if (c < 1) { MessageUtils.showMessage(neg_colnum); } else { this.lastDataColumn = c - 1; } } } } } } }