4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Locale;
14 import jalview.analysis.AlignmentUtils;
15 import jalview.api.AlignmentViewPanel;
16 import jalview.bin.ArgParser.Arg;
17 import jalview.bin.ArgParser.ArgValue;
18 import jalview.bin.ArgParser.ArgValuesMap;
19 import jalview.bin.ArgParser.SubVals;
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.PDBEntry;
23 import jalview.datamodel.SequenceI;
24 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.AlignmentPanel;
27 import jalview.gui.AssociatePdbFileWithSeq;
28 import jalview.gui.Desktop;
29 import jalview.gui.Preferences;
30 import jalview.gui.StructureChooser;
31 import jalview.gui.StructureViewer;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.DataSourceType;
34 import jalview.io.FileFormatException;
35 import jalview.io.FileFormatI;
36 import jalview.io.FileLoader;
37 import jalview.io.HtmlSvgOutput;
38 import jalview.io.IdentifyFile;
39 import jalview.schemes.AnnotationColourGradient;
40 import jalview.structure.StructureImportSettings;
41 import jalview.structure.StructureImportSettings.TFType;
42 import jalview.structure.StructureSelectionManager;
43 import jalview.util.HttpUtils;
44 import jalview.util.MessageManager;
45 import jalview.util.Platform;
46 import jalview.ws.dbsources.EBIAlfaFold;
47 import mc_view.PDBChain;
53 private static boolean headless;
55 private static ArgParser argParser;
57 private Map<String, AlignFrame> afMap;
59 public static boolean processArgs(ArgParser ap, boolean h)
63 boolean argsWereParsed = true;
66 System.setProperty("java.awt.headless", "true");
69 if (argParser != null && argParser.linkedIds() != null)
71 for (String id : argParser.linkedIds())
73 Commands cmds = new Commands();
76 cmds.processUnlinked(id);
77 argsWereParsed &= cmds.wereParsed();
81 cmds.processLinked(id);
82 argsWereParsed &= cmds.wereParsed();
84 cmds.processImages(id);
85 argsWereParsed &= cmds.wereParsed();
89 if (argParser.getBool(Arg.QUIT))
91 Jalview.getInstance().quit();
94 // carry on with jalview.bin.Jalview
95 return argsWereParsed;
98 boolean argsWereParsed = true; // set false as soon as an arg is found
100 private boolean wereParsed()
102 return argsWereParsed;
107 this(Desktop.instance);
110 public Commands(Desktop d)
113 afMap = new HashMap<String, AlignFrame>();
116 protected void processUnlinked(String id)
121 protected void processLinked(String id)
123 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
127 argsWereParsed = false;
130 // script to execute after all loading is completed one way or another
131 String groovyscript = m.get(Arg.GROOVY) == null ? null
132 : m.get(Arg.GROOVY).getValue();
133 String file = m.get(Arg.OPEN) == null ? null
134 : m.get(Arg.OPEN).getValue();
136 FileFormatI format = null;
137 DataSourceType protocol = null;
139 if (avm.hasValue(Arg.OPEN))
143 boolean first = true;
145 for (ArgValue av : avm.getArgValueList(Arg.OPEN))
147 String openFile = av.getValue();
148 if (openFile == null)
151 argsWereParsed = true;
155 if (!headless && desktop != null)
157 desktop.setProgressBar(
158 MessageManager.getString(
159 "status.processing_commandline_args"),
160 progress = System.currentTimeMillis());
164 if (!Platform.isJS())
166 * ignore in JavaScript -- can't just file existence - could load it?
171 if (!HttpUtils.startsWithHttpOrHttps(openFile))
173 if (!(new File(openFile)).exists())
175 Console.warn("Can't find file '" + openFile + "'");
180 DataSourceType protocol = AppletFormatAdapter
181 .checkProtocol(openFile);
183 FileFormatI format = null;
186 format = new IdentifyFile().identify(openFile, protocol);
187 } catch (FileFormatException e1)
189 Console.error("Unknown file format for '" + openFile + "'");
196 * this approach isn't working yet
197 // get default annotations before opening AlignFrame
198 if (m.get(Arg.SSANNOTATION) != null)
200 Console.debug("***** SSANNOTATION="
201 + m.get(Arg.SSANNOTATION).getBoolean());
203 if (m.get(Arg.NOTEMPFAC) != null)
206 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
208 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
209 ? m.get(Arg.SSANNOTATION).getBoolean()
211 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
212 ? !m.get(Arg.NOTEMPFAC).getBoolean()
214 Console.debug("***** tempfac=" + showTemperatureFactor
215 + ", showSS=" + showSecondaryStructure);
216 StructureSelectionManager ssm = StructureSelectionManager
217 .getStructureSelectionManager(Desktop.instance);
220 ssm.setAddTempFacAnnot(showTemperatureFactor);
221 ssm.setProcessSecondaryStructure(showSecondaryStructure);
225 // get kind of temperature factor annotation
226 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
227 if ((!avm.getBoolean(Arg.NOTEMPFAC)) && avm.hasValue(Arg.TEMPFAC))
231 tempfacType = StructureImportSettings.TFType
232 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
233 .toUpperCase(Locale.ROOT));
234 Console.debug("Obtained Temperature Factor type of '"
235 + tempfacType + "'");
236 } catch (IllegalArgumentException e)
238 // Just an error message!
239 StringBuilder sb = new StringBuilder().append("Cannot set --")
240 .append(Arg.TEMPFAC.getName()).append(" to '")
242 .append("', ignoring. Valid values are: ");
243 Iterator<StructureImportSettings.TFType> it = Arrays
244 .stream(StructureImportSettings.TFType.values())
248 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
252 Console.warn(sb.toString());
257 "Opening '" + openFile + "' in new alignment frame");
258 FileLoader fileLoader = new FileLoader(!headless);
260 StructureImportSettings.setTemperatureFactorType(tempfacType);
262 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
266 if (avm.getBoolean(Arg.WRAP))
268 af.getCurrentView().setWrapAlignment(true);
272 if (avm.hasValue(Arg.COLOUR))
274 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
277 // change alignment frame title
278 if (avm.hasValue(Arg.TITLE))
279 af.setTitle(avm.getValue(Arg.TITLE));
281 /* hacky approach to hiding the annotations */
282 // show secondary structure annotations?
283 if (avm.getBoolean(Arg.SSANNOTATION))
285 // do this better (annotation types?)
286 AlignmentUtils.showOrHideSequenceAnnotations(
287 af.getCurrentView().getAlignment(),
288 Collections.singleton("Secondary Structure"), null,
292 // show temperature factor annotations?
293 if (avm.getBoolean(Arg.NOTEMPFAC))
295 // do this better (annotation types?)
296 List<String> hideThese = new ArrayList<>();
297 hideThese.add("Temperature Factor");
298 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
299 AlignmentUtils.showOrHideSequenceAnnotations(
300 af.getCurrentView().getAlignment(), hideThese, null,
304 /* comment out hacky approach up to here and add this line:
305 if (showTemperatureFactor)
308 if (avm.hasValue(Arg.TEMPFAC_LABEL))
310 AlignmentAnnotation aa = AlignmentUtils
311 .getFirstSequenceAnnotationOfType(
312 af.getCurrentView().getAlignment(),
313 AlignmentAnnotation.LINE_GRAPH);
314 String label = avm.getValue(Arg.TEMPFAC_LABEL);
322 "Could not find annotation to apply tempfac_label '"
328 // store the AlignFrame for this id
331 // is it its own structure file?
332 if (format.isStructureFile())
334 StructureSelectionManager ssm = StructureSelectionManager
335 .getStructureSelectionManager(Desktop.instance);
336 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
337 ssm.computeMapping(false, new SequenceI[] { seq }, null,
338 openFile, DataSourceType.FILE, null, null, null);
344 "Opening '" + openFile + "' in existing alignment frame");
345 af.getCurrentView().addFile(new File(openFile), format);
348 Console.debug("Command " + Arg.OPEN + " executed successfully!");
351 if (first) // first=true means nothing opened
355 Console.error("Could not open any files in headless mode");
360 Console.warn("No more files to open");
362 desktop.setProgressBar(null, progress);
368 // open the structure (from same PDB file or given PDBfile)
369 if (!avm.getBoolean(Arg.NOSTRUCTURE))
371 AlignFrame af = afMap.get(id);
372 if (avm.hasValue(Arg.STRUCTURE))
374 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
376 String val = av.getValue();
377 SubVals subId = new SubVals(val);
378 SequenceI seq = getSpecifiedSequence(af, subId);
381 Console.warn("Could not find sequence for argument --"
382 + Arg.STRUCTURE + "=" + val);
383 // you probably want to continue here, not break
387 File structureFile = null;
388 if (subId.getContent() != null
389 && subId.getContent().length() != 0)
391 structureFile = new File(subId.getContent());
392 Console.debug("Using structure file (from argument) '"
393 + structureFile.getAbsolutePath() + "'");
398 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
399 .associatePdbWithSeq(selectedPdbFileName,
400 DataSourceType.FILE, selectedSequence, true,
403 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
405 { selectedSequence });
409 /* THIS DOESN'T WORK */
410 else if (seq.getAllPDBEntries() != null
411 && seq.getAllPDBEntries().size() > 0)
413 structureFile = new File(
414 seq.getAllPDBEntries().elementAt(0).getFile());
415 Console.debug("Using structure file (from sequence) '"
416 + structureFile.getAbsolutePath() + "'");
419 if (structureFile == null)
421 Console.warn("Not provided structure file with '" + val + "'");
425 if (!structureFile.exists())
427 Console.warn("Structure file '"
428 + structureFile.getAbsoluteFile() + "' not found.");
432 Console.debug("Using structure file "
433 + structureFile.getAbsolutePath());
435 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
436 .associatePdbWithSeq(structureFile.getAbsolutePath(),
437 DataSourceType.FILE, seq, true, Desktop.instance);
439 // open structure view
440 AlignmentPanel ap = af.alignPanel;
443 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
444 StructureViewer.ViewerType.JMOL.toString());
446 StructureChooser.openStructureFileForSequence(ap, seq,
452 // load a pAE file if given
453 if (avm.hasValue(Arg.PAEMATRIX))
455 AlignFrame af = afMap.get(id);
458 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
460 String val = av.getValue();
461 SubVals subVals = ArgParser.getSubVals(val);
462 File paeFile = new File(subVals.getContent());
463 String paePath = null;
466 paePath = paeFile.getCanonicalPath();
467 } catch (IOException e)
469 paePath = paeFile.getAbsolutePath();
471 "Problem with the PAE file path: '" + paePath + "'");
473 String structId = subVals.get("structid");
474 if (subVals.notSet())
476 // take structid from pdbfilename
478 if (subVals.has("structfile"))
480 Console.info("***** Attaching paeFile '" + paePath + "' to "
481 + "structfile=" + subVals.get("structfile"));
482 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
483 paeFile, subVals.getIndex(), subVals.get("structfile"),
486 else if (subVals.has("structid"))
488 Console.info("***** Attaching paeFile '" + paePath + "' to "
489 + "structid=" + subVals.get("structid"));
490 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
491 paeFile, subVals.getIndex(), subVals.get("structid"),
496 Console.debug("***** Attaching paeFile '" + paePath
497 + "' to sequence index " + subVals.getIndex());
498 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
499 paeFile, subVals.getIndex(), null, false, false);
500 // required to readjust the height and position of the pAE
503 for (AlignmentViewPanel ap : af.getAlignPanels())
505 ap.adjustAnnotationHeight();
511 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
514 AlignFrame af = afMap.get(id);
515 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
516 .findAnnotation(PDBChain.class.getName().toString()))
518 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
519 af.alignPanel.av.getGlobalColourScheme(), 0);
520 acg.setSeqAssociated(true);
521 af.changeColour(acg);
522 Console.info("Changed colour " + acg.toString());
527 protected void processImages(String id)
529 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
530 AlignFrame af = afMap.get(id);
534 Console.warn("Did not have an alignment window for id=" + id);
538 if (avm.hasValue(Arg.IMAGE))
540 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
542 String val = av.getValue();
543 SubVals subVal = new SubVals(val);
544 String type = "png"; // default
545 String fileName = subVal.getContent();
546 File file = new File(fileName);
547 if (subVal.has("type"))
549 type = subVal.get("type");
551 else if (fileName != null)
553 for (String ext : new String[] { "svg", "png", "html" })
555 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
561 // for moment we disable JSON export
562 Cache.setPropsAreReadOnly(true);
563 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
568 Console.debug("Outputting type '" + type + "' to " + fileName);
572 Console.debug("Outputting type '" + type + "' to " + fileName);
576 Console.debug("Outputting type '" + type + "' to " + fileName);
577 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
578 htmlSVG.exportHTML(fileName);
581 Console.warn("--image type '" + type + "' not known. Ignoring");
588 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
590 AlignmentI al = af.getCurrentView().getAlignment();
591 if (-1 < subId.getIndex()
592 && subId.getIndex() < al.getSequences().size())
594 return al.getSequenceAt(subId.getIndex());
596 else if (subId.has("seqid"))
598 return al.findName(subId.get("seqid"));