package jalview.bin; import java.io.File; import java.util.Collections; import java.util.HashMap; import java.util.Map; import jalview.analysis.AlignmentUtils; import jalview.bin.ArgParser.Arg; import jalview.bin.ArgParser.ArgValues; import jalview.gui.AlignFrame; import jalview.gui.Desktop; import jalview.io.AppletFormatAdapter; import jalview.io.DataSourceType; import jalview.io.FileFormatException; import jalview.io.FileFormatI; import jalview.io.FileLoader; import jalview.io.IdentifyFile; import jalview.util.HttpUtils; import jalview.util.MessageManager; import jalview.util.Platform; public class Commands { Desktop desktop; private static boolean headless; private static ArgParser argParser; private Map afMap; public static void processArgs(ArgParser ap, boolean h) { argParser = ap; headless = h; for (String id : argParser.linkedIds()) { Commands cmds = new Commands(); if (id == null) { cmds.processUnlinked(id); } else { cmds.processLinked(id); } } } public Commands() { this(Desktop.instance); } public Commands(Desktop d) { this.desktop = d; afMap = new HashMap(); } protected void processUnlinked(String id) { processLinked(id); } protected void processLinked(String id) { Map m = argParser.linkedArgs(id); FileLoader fileLoader = new FileLoader(!headless); /* // script to execute after all loading is completed one way or another String groovyscript = m.get(Arg.GROOVY) == null ? null : m.get(Arg.GROOVY).getValue(); String file = m.get(Arg.OPEN) == null ? null : m.get(Arg.OPEN).getValue(); String data = null; FileFormatI format = null; DataSourceType protocol = null; */ if (m.get(Arg.OPEN) != null) { long progress = -1; boolean first = true; AlignFrame af; OPEN: for (String openFile : m.get(Arg.OPEN).getValues()) { if (openFile == null) continue OPEN; Console.debug("********** id = " + id + ", openFile = " + openFile); if (first) { first = false; if (!headless) { desktop.setProgressBar( MessageManager.getString( "status.processing_commandline_args"), progress = System.currentTimeMillis()); } } if (!Platform.isJS()) /** * ignore in JavaScript -- can't just file existence - could load it? * * @j2sIgnore */ { if (!HttpUtils.startsWithHttpOrHttps(openFile)) { if (!(new File(openFile)).exists()) { Console.warn("Can't find file '" + openFile + "'"); continue OPEN; } } } DataSourceType protocol = AppletFormatAdapter .checkProtocol(openFile); FileFormatI format = null; try { format = new IdentifyFile().identify(openFile, protocol); } catch (FileFormatException e1) { Console.error("Unknown file format for '" + openFile + "'"); } af = afMap.get(id); if (af == null) { Console.debug( "Opening '" + openFile + "' in new alignment frame"); af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, format); if (m.get(Arg.TITLE) != null) af.setTitle(m.get(Arg.TITLE).getValue()); if (m.get(Arg.SSANNOTATION) != null && !m.get(Arg.SSANNOTATION).getBoolean()) { // do this better (annotation types?) AlignmentUtils.showOrHideSequenceAnnotations( af.getCurrentView().getAlignment(), Collections.singleton("Secondary Structure"), null, false, false); } if (m.get(Arg.NOTEMPFAC) != null && m.get(Arg.NOTEMPFAC).getBoolean()) { // do this better (annotation types?) AlignmentUtils.showOrHideSequenceAnnotations( af.getCurrentView().getAlignment(), Collections.singleton("Temperature Factor"), null, false, false); AlignmentUtils.showOrHideSequenceAnnotations( af.getCurrentView().getAlignment(), Collections.singleton("Alphafold Reliability"), null, false, false); } // store the AlignFrame for this id afMap.put(id, af); } else { Console.debug( "Opening '" + openFile + "' in existing alignment frame"); af.getCurrentView().addFile(new File(openFile), format); } System.out .println("Command " + Arg.OPEN + " executed successfully!"); } if (first) // first=true means nothing opened { if (headless) { Console.error("Could not open any files in headless mode"); System.exit(1); } } else { Console.warn("No more files to open"); if (desktop != null) desktop.setProgressBar(null, progress); } } } }