public static List<FeatureTrack> loadTracks( Iterable<Feature> features, ResourceLocator locator, Genome genome, TrackProperties trackProperties) { FeatureCollectionSource source = new FeatureCollectionSource(features, genome); // Load into FeatureDB for searching if (IGV.hasInstance() || Globals.isTesting()) { Set<String> chrs = source.getChrs(); for (String chr : chrs) { List<Feature> feats = source.getFeatures(chr); for (Feature f : feats) { if (f instanceof NamedFeature) { FeatureDB.addFeature((NamedFeature) f); } } } } FeatureTrack track = new FeatureTrack(locator, source); track.setName(locator.getTrackName()); track.setRendererClass(IGVFeatureRenderer.class); track.setHeight(45); if (trackProperties != null) { track.setProperties(trackProperties); } List<FeatureTrack> tracks = new ArrayList(); tracks.add(track); return tracks; }
/** * Main method. Used to run igvtools from the command line * * @param argv * @throws IOException * @throws PreprocessingException */ public static void main(String[] argv) { try { initLogger(); Globals.setHeadless(true); (new IgvTools()).run(argv); System.out.println("Done"); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
public static String getGSUser() throws IOException { if (gsUser == null && !Globals.isTesting()) { BufferedReader br = null; try { File tokenFile = getUsernameFile(); if (tokenFile.exists()) { br = new BufferedReader(new FileReader(tokenFile)); gsUser = br.readLine().trim(); } } finally { try { if (br != null) br.close(); } catch (Exception e) { } } } return gsUser; }
public static void setGSUser(String newUser) { if (gsUser == null || !gsUser.equals(newUser)) { gsUser = newUser; if (Globals.isTesting()) { return; } File gsDir = getTokenSaveDir(); if (!gsDir.isDirectory()) { log.error( "Could not store token for SSO. File " + gsDir.getAbsolutePath() + "exists and is not a directory."); return; // someone made a file with this name... } File userFile = getUsernameFile(); if (userFile.exists()) userFile.delete(); writeToFile(gsUser, userFile); } }
/** * Load a gui with the specified genome file. No genome is loaded if null * * @param genomeFile * @return * @throws IOException */ protected static IGV startGUI(String genomeFile) throws IOException { Globals.setHeadless(false); IGV igv; // If IGV is already open, we get the instance. if (IGV.hasInstance()) { igv = IGV.getInstance(); IGV.getMainFrame().setVisible(true); System.out.println("Using old IGV"); } else { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main.open(frame); System.out.println("Started new IGV"); igv = IGV.getInstance(); assertTrue(IGV.getInstance().waitForNotify(1000)); } if (genomeFile != null) { igv.loadGenome(genomeFile, null); genome = igv.getGenomeManager().getCurrentGenome(); } return igv; }
public static String getGSToken() { if (gsToken == null && !Globals.isTesting()) { File file = GSUtils.getTokenFile(); if (file.exists()) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); gsToken = br.readLine(); } catch (IOException e) { log.error("Error reading GS cookie", e); } finally { if (br != null) try { br.close(); } catch (IOException e) { // Ignore } } } } return gsToken; }
public static void setGSToken(String newToken) { if (gsToken == null || !gsToken.equals(newToken)) { gsToken = newToken; if (Globals.isTesting()) { return; } BufferedWriter bw = null; File gsDir = getTokenSaveDir(); if (!gsDir.isDirectory()) { log.error( "Could not store token for SSO. File " + gsDir.getAbsolutePath() + "exists and is not a directory."); return; // someone made a file with this name... } File tokenFile = getTokenFile(); if (tokenFile.exists()) tokenFile.delete(); writeToFile(gsToken, tokenFile); } }
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; } } }
/** * Generate the "tools" menu. This is imperative, it is written to field {@code toolsMenu}. Reason * being, when we add (TODO remove) a new tool, we need to refresh just this menu */ void refreshToolsMenu() { List<JComponent> menuItems = new ArrayList<JComponent>(10); // batch script MenuAction menuAction = new RunScriptMenuAction("Run Batch Script...", KeyEvent.VK_X, igv); menuItems.add(MenuAndToolbarUtils.createMenuItem(menuAction)); // igvtools // menuItems.add(new JSeparator()); menuAction = new SortTracksMenuAction("Run igvtools...", KeyEvent.VK_T, igv) { @Override public void actionPerformed(ActionEvent e) { IgvToolsGui.launch(false, igv.getGenomeManager().getGenomeId()); } }; menuItems.add(MenuAndToolbarUtils.createMenuItem(menuAction)); List<JComponent> otherToolMenus = igv.getOtherToolMenus(); if (otherToolMenus.size() > 0) { for (JComponent entry : otherToolMenus) { menuItems.add(entry); } } // menuItems.add(new JSeparator()); // -------------------------------------// // "Add tool" option, for loading cli_plugin from someplace else JMenuItem addTool = new JMenuItem("Add Tool..."); addTool.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { File pluginFi = FileDialogUtils.chooseFile("Select cli_plugin .xml spec"); if (pluginFi == null) return; try { PluginSpecReader.addCustomPlugin(pluginFi.getAbsolutePath()); refreshToolsMenu(); } catch (IOException e1) { MessageUtils.showErrorMessage("Error loading custom cli_plugin", e1); } } }); // menuItems.add(addTool); // menuItems.add(new JSeparator()); // -------------------------------------// for (final PluginSpecReader pluginSpecReader : PluginSpecReader.getPlugins()) { for (final PluginSpecReader.Tool tool : pluginSpecReader.getTools()) { final String toolName = tool.name; boolean toolVisible = tool.visible; JMenuItem toolMenu; if (toolVisible) { final String toolPath = pluginSpecReader.getToolPath(tool); final String tool_url = tool.toolUrl; boolean isValid = PluginSpecReader.isToolPathValid(toolPath); ActionListener invalidActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String msg = String.format("%s executable not found at %s", toolName, toolPath); if (tool_url != null) { msg += "<br/>See " + tool_url + " to install"; } MessageUtils.showMessage(msg); } }; toolMenu = new JMenu(toolName); // Kind of overlaps with the side-pull menu, doesn't look great // toolMenu.setToolTipText(tool.getAttribute("description")); for (final PluginSpecReader.Command command : tool.commandList) { final String cmdName = command.name; JMenuItem cmdItem = new JMenuItem(cmdName); toolMenu.add(cmdItem); if (isValid || toolPath == null) { cmdItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { RunPlugin runPlugin = null; try { runPlugin = new RunPlugin(IGV.getMainFrame(), pluginSpecReader, tool, command); } catch (IllegalStateException e1) { MessageUtils.showErrorMessage(e1.getMessage(), e1); return; } runPlugin.setVisible(true); } }); cmdItem.setEnabled(true); } else { cmdItem.setEnabled(false); } } // Hack so we can have a tool which is just general command line stuff // Don't let the user change the path in that case if (tool.defaultPath != null) { JMenuItem setPathItem = new JMenuItem(String.format("Set path to %s...", toolName)); setPathItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { (new SetPluginPathDialog(IGV.getMainFrame(), pluginSpecReader, tool)) .setVisible(true); refreshToolsMenu(); } }); toolMenu.add(setPathItem); } menuItems.add(toolMenu); } } } // -------------------------------------// // -----------SQL DB Tools--------------// boolean showDBEditor = Globals.isDevelopment(); if (showDBEditor) { JMenu sqlDBProfileEditor = new JMenu("SQL DB Profile Editor"); JMenuItem createNewProfile = new JMenuItem("Create New Profile"); createNewProfile.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { File file = FileDialogUtils.chooseFile( "Save DB Profile", DirectoryManager.getUserDirectory(), FileDialogUtils.SAVE); if (file != null) { DBProfileEditor editor = new DBProfileEditor(IGV.getMainFrame(), file.getAbsolutePath()); editor.setVisible(true); } } }); JMenuItem editExistingProfile = new JMenuItem("Edit Existing Profile"); editExistingProfile.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { File file = FileDialogUtils.chooseFile("Select .dbxml database profile"); if (file != null) { if (!file.exists()) {} DBProfileEditor editor = new DBProfileEditor(IGV.getMainFrame(), file.getAbsolutePath()); editor.setVisible(true); } } }); sqlDBProfileEditor.add(createNewProfile); sqlDBProfileEditor.add(editExistingProfile); menuItems.add(sqlDBProfileEditor); } // -------------------------------------// // DataTrack Math------------------------// if (Globals.isDevelopment()) { JMenuItem combineDataItem = new JMenuItem("Combine Data Tracks"); combineDataItem.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CombinedDataSourceDialog dialog = new CombinedDataSourceDialog(IGV.getMainFrame()); dialog.setVisible(true); } }); menuItems.add(combineDataItem); } // -------------------------------------// MenuAction toolsMenuAction = new MenuAction("Tools", null); if (toolsMenu == null) { toolsMenu = MenuAndToolbarUtils.createMenu(menuItems, toolsMenuAction); toolsMenu.setName("Tools"); } else { toolsMenu.removeAll(); for (JComponent item : menuItems) { toolsMenu.add(item); } } }
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; }
public static String getVersionString() { return Globals.applicationString(); }
public static void main(String[] argv) throws IOException, CmdLineParser.UnknownOptionException, CmdLineParser.IllegalOptionValueException { if (argv.length < 4) { System.out.println("Usage: hictools pre <options> <inputFile> <outputFile> <genomeID>"); System.out.println(" <options>: -d only calculate intra chromosome (diagonal) [false]"); System.out.println( " : -o calculate densities (observed/expected), write to file [false]"); System.out.println(" : -t <int> only write cells with count above threshold t [0]"); System.out.println( " : -c <chromosome ID> only calculate map on specific chromosome"); System.exit(0); } Globals.setHeadless(true); CommandLineParser parser = new CommandLineParser(); parser.parse(argv); String[] args = parser.getRemainingArgs(); if (args[0].equals("sort")) { AlignmentsSorter.sort(args[1], args[2], null); } else if (args[0].equals("pairsToBin")) { String ifile = args[1]; String ofile = args[2]; String genomeId = args[3]; List<Chromosome> chromosomes = loadChromosomes(genomeId); AsciiToBinConverter.convert(ifile, ofile, chromosomes); } else if (args[0].equals("binToPairs")) { String ifile = args[1]; String ofile = args[2]; AsciiToBinConverter.convertBack(ifile, ofile); } else if (args[0].equals("printmatrix")) { if (args.length < 5) { System.err.println( "Usage: hictools printmatrix <observed/oe/pearson> hicFile chr1 chr2 binsize"); System.exit(-1); } String type = args[1]; String file = args[2]; String chr1 = args[3]; String chr2 = args[4]; String binSizeSt = args[5]; int binSize = 0; try { binSize = Integer.parseInt(binSizeSt); } catch (NumberFormatException e) { System.err.println("Integer expected. Found: " + binSizeSt); System.exit(-1); } dumpMatrix(file, chr1, chr2, binSize, type); } else if (args[0].equals("eigenvector")) { if (args.length < 4) { System.err.println("Usage: hictools eigenvector hicFile chr binsize"); } String file = args[1]; String chr = args[2]; String binSizeSt = args[3]; int binSize = 0; try { binSize = Integer.parseInt(binSizeSt); } catch (NumberFormatException e) { System.err.println("Integer expected. Found: " + binSizeSt); System.exit(-1); } calculateEigenvector(file, chr, binSize); } else if (args[0].equals("pre")) { String genomeId = ""; try { genomeId = args[3]; } catch (ArrayIndexOutOfBoundsException e) { System.err.println("No genome ID given"); System.exit(0); } List<Chromosome> chromosomes = loadChromosomes(genomeId); long genomeLength = 0; for (Chromosome c : chromosomes) { if (c != null) genomeLength += c.getSize(); } chromosomes.set(0, new Chromosome(0, "All", (int) (genomeLength / 1000))); String[] tokens = args[1].split(","); List<String> files = new ArrayList<String>(tokens.length); for (String f : tokens) { files.add(f); } Preprocessor preprocessor = new Preprocessor(new File(args[2]), chromosomes); preprocessor.setIncludedChromosomes(parser.getChromosomeOption()); preprocessor.setCountThreshold(parser.getCountThresholdOption()); preprocessor.setNumberOfThreads(parser.getThreadedOption()); preprocessor.setDiagonalsOnly(parser.getDiagonalsOption()); preprocessor.setLoadDensities(parser.getDensitiesOption()); preprocessor.preprocess(files); } }