4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import java.util.Locale;
13 import jalview.analysis.AlignmentUtils;
14 import jalview.api.AlignmentViewPanel;
15 import jalview.bin.ArgParser.Arg;
16 import jalview.bin.ArgParser.ArgValues;
17 import jalview.bin.ArgParser.SubVal;
18 import jalview.datamodel.AlignmentAnnotation;
19 import jalview.datamodel.SequenceI;
20 import jalview.gui.AlignFrame;
21 import jalview.gui.AlignmentPanel;
22 import jalview.gui.Desktop;
23 import jalview.gui.StructureChooser;
24 import jalview.io.AppletFormatAdapter;
25 import jalview.io.DataSourceType;
26 import jalview.io.FileFormatException;
27 import jalview.io.FileFormatI;
28 import jalview.io.FileLoader;
29 import jalview.io.HtmlSvgOutput;
30 import jalview.io.IdentifyFile;
31 import jalview.util.HttpUtils;
32 import jalview.util.MessageManager;
33 import jalview.util.Platform;
34 import jalview.ws.dbsources.EBIAlfaFold;
40 private static boolean headless;
42 private static ArgParser argParser;
44 private Map<String, AlignFrame> afMap;
46 public static void processArgs(ArgParser ap, boolean h)
50 if (argParser != null && argParser.linkedIds() != null)
52 for (String id : argParser.linkedIds())
54 Commands cmds = new Commands();
57 cmds.processUnlinked(id);
61 cmds.processLinked(id);
63 cmds.processImages(id);
66 if (argParser.getBool(Arg.QUIT))
68 Jalview.getInstance().quit();
75 this(Desktop.instance);
78 public Commands(Desktop d)
81 afMap = new HashMap<String, AlignFrame>();
84 protected void processUnlinked(String id)
89 protected void processLinked(String id)
91 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
94 // script to execute after all loading is completed one way or another
95 String groovyscript = m.get(Arg.GROOVY) == null ? null
96 : m.get(Arg.GROOVY).getValue();
97 String file = m.get(Arg.OPEN) == null ? null
98 : m.get(Arg.OPEN).getValue();
100 FileFormatI format = null;
101 DataSourceType protocol = null;
104 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
108 boolean first = true;
110 OPEN: for (String openFile : ArgParser.getValues(m, Arg.OPEN))
112 if (openFile == null)
120 desktop.setProgressBar(
121 MessageManager.getString(
122 "status.processing_commandline_args"),
123 progress = System.currentTimeMillis());
127 if (!Platform.isJS())
129 * ignore in JavaScript -- can't just file existence - could load it?
134 if (!HttpUtils.startsWithHttpOrHttps(openFile))
136 if (!(new File(openFile)).exists())
138 Console.warn("Can't find file '" + openFile + "'");
144 DataSourceType protocol = AppletFormatAdapter
145 .checkProtocol(openFile);
147 FileFormatI format = null;
150 format = new IdentifyFile().identify(openFile, protocol);
151 } catch (FileFormatException e1)
153 Console.error("Unknown file format for '" + openFile + "'");
160 * this approach isn't working yet
161 // get default annotations before opening AlignFrame
162 if (m.get(Arg.SSANNOTATION) != null)
164 Console.debug("***** SSANNOTATION="
165 + m.get(Arg.SSANNOTATION).getBoolean());
167 if (m.get(Arg.NOTEMPFAC) != null)
170 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
172 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
173 ? m.get(Arg.SSANNOTATION).getBoolean()
175 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
176 ? !m.get(Arg.NOTEMPFAC).getBoolean()
178 Console.debug("***** tempfac=" + showTemperatureFactor
179 + ", showSS=" + showSecondaryStructure);
180 StructureSelectionManager ssm = StructureSelectionManager
181 .getStructureSelectionManager(Desktop.instance);
184 ssm.setAddTempFacAnnot(showTemperatureFactor);
185 ssm.setProcessSecondaryStructure(showSecondaryStructure);
189 // get kind of temperature factor annotation
190 AlignmentAnnotation.TFType tempfacType = null;
191 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
192 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
196 tempfacType = AlignmentAnnotation.TFType.valueOf(ArgParser
197 .getValue(m, Arg.TEMPFAC).toUpperCase(Locale.ROOT));
198 Console.debug("Obtained Temperature Factor type of '"
199 + tempfacType + "'");
200 } catch (IllegalArgumentException e)
202 // Just an error message!
203 StringBuilder sb = new StringBuilder().append("Cannot set --")
204 .append(Arg.TEMPFAC.getName()).append(" to '")
206 .append("', ignoring. Valid values are: ");
207 Iterator<AlignmentAnnotation.TFType> it = Arrays
208 .stream(AlignmentAnnotation.TFType.values())
212 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
216 Console.warn(sb.toString());
221 "Opening '" + openFile + "' in new alignment frame");
222 FileLoader fileLoader = new FileLoader(!headless);
223 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, format,
227 if (ArgParser.getBoolean(m, Arg.WRAP))
229 af.getCurrentView().setWrapAlignment(true);
232 // change alignment frame title
233 if (ArgParser.getValue(m, Arg.TITLE) != null)
234 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
236 /* hacky approach to hiding the annotations */
237 // show secondary structure annotations?
238 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
240 // do this better (annotation types?)
241 AlignmentUtils.showOrHideSequenceAnnotations(
242 af.getCurrentView().getAlignment(),
243 Collections.singleton("Secondary Structure"), null,
247 // show temperature factor annotations?
248 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
250 // do this better (annotation types?)
251 List<String> hideThese = new ArrayList<>();
252 hideThese.add("Temperature Factor");
253 hideThese.add(MessageManager
254 .getString("label.alphafold_reliability"));
255 AlignmentUtils.showOrHideSequenceAnnotations(
256 af.getCurrentView().getAlignment(), hideThese, null,
260 /* comment out hacky approach up to here and add this line:
261 if (showTemperatureFactor)
264 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
266 AlignmentAnnotation aa = AlignmentUtils
267 .getFirstSequenceAnnotationOfType(
268 af.getCurrentView().getAlignment(),
269 AlignmentAnnotation.LINE_GRAPH);
270 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
278 "Could not find annotation to apply tempfac_label '"
284 // store the AlignFrame for this id
290 "Opening '" + openFile + "' in existing alignment frame");
291 af.getCurrentView().addFile(new File(openFile), format);
295 .println("Command " + Arg.OPEN + " executed successfully!");
298 if (first) // first=true means nothing opened
302 Console.error("Could not open any files in headless mode");
308 Console.warn("No more files to open");
310 desktop.setProgressBar(null, progress);
315 // load a pAE file if given
316 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
318 AlignFrame af = afMap.get(id);
321 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
323 SubVal subVal = ArgParser.getSubVal(val);
324 File paeFile = new File(subVal.content);
325 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
326 paeFile, subVal.index,
327 "id".equals(subVal.keyName) ? subVal.keyValue : null);
328 // required to readjust the height and position of the pAE
330 for (AlignmentViewPanel ap : af.getAlignPanels())
332 ap.adjustAnnotationHeight();
338 // open the structure (from same PDB file or given PDBfile)
339 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
341 AlignFrame af = afMap.get(id);
342 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
344 STRUCTURE: for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
346 SubVal subId = new SubVal(val);
347 SequenceI seq = getSpecifiedSequence(af, subId);
350 Console.warn("Could not find sequence for argument --"
351 + Arg.STRUCTURE + "=" + val);
354 File structureFile = null;
355 if (subId.content != null && subId.content.length() != 0)
357 structureFile = new File(subId.content);
358 Console.debug("Using structure file (from argument) '"
359 + structureFile.getAbsolutePath() + "'");
361 /* THIS DOESN'T WORK */
362 else if (seq.getAllPDBEntries() != null
363 && seq.getAllPDBEntries().size() > 0)
365 structureFile = new File(
366 seq.getAllPDBEntries().elementAt(0).getFile());
367 Console.debug("Using structure file (from sequence) '"
368 + structureFile.getAbsolutePath() + "'");
371 if (structureFile == null)
373 Console.warn("Not provided structure file with '" + val + "'");
377 if (!structureFile.exists())
379 Console.warn("Structure file '"
380 + structureFile.getAbsoluteFile() + "' not found.");
384 Console.debug("Using structure file "
385 + structureFile.getAbsolutePath());
387 // open structure view
388 AlignmentPanel ap = af.alignPanel;
389 StructureChooser.openStructureFileForSequence(ap, seq,
396 protected void processImages(String id)
398 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
399 AlignFrame af = afMap.get(id);
403 Console.warn("Did not have an alignment window for id=" + id);
407 if (ArgParser.getValues(m, Arg.IMAGE) != null)
409 for (String val : ArgParser.getValues(m, Arg.IMAGE))
411 SubVal subVal = new SubVal(val);
412 String type = "png"; // default
413 String fileName = subVal.content;
414 File file = new File(fileName);
415 if ("type".equals(subVal.keyName))
417 type = subVal.keyValue;
419 else if (fileName != null)
421 for (String ext : new String[] { "svg", "png", "html" })
423 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
438 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
439 htmlSVG.exportHTML(fileName);
446 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
448 SequenceI seq = null;
449 SequenceI[] sequences = af.getCurrentView().getAlignment()
450 .getSequencesArray();
451 if (-1 < subId.index && subId.index < sequences.length)
453 seq = sequences[subId.index];
455 else if ("id".equals(subId.keyName))
457 for (SequenceI s : sequences)
459 if (s.getDisplayId(false).equals(subId.keyValue))