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 = 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.containsArg(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))
228 && avm.containsArg(Arg.TEMPFAC))
232 tempfacType = StructureImportSettings.TFType
233 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
234 .toUpperCase(Locale.ROOT));
235 Console.debug("Obtained Temperature Factor type of '"
236 + tempfacType + "'");
237 } catch (IllegalArgumentException e)
239 // Just an error message!
240 StringBuilder sb = new StringBuilder().append("Cannot set --")
241 .append(Arg.TEMPFAC.getName()).append(" to '")
243 .append("', ignoring. Valid values are: ");
244 Iterator<StructureImportSettings.TFType> it = Arrays
245 .stream(StructureImportSettings.TFType.values())
249 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
253 Console.warn(sb.toString());
258 "Opening '" + openFile + "' in new alignment frame");
259 FileLoader fileLoader = new FileLoader(!headless);
261 StructureImportSettings.setTemperatureFactorType(tempfacType);
263 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
267 if (avm.getBoolean(Arg.WRAP))
269 af.getCurrentView().setWrapAlignment(true);
273 if (avm.containsArg(Arg.COLOUR))
275 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
278 // change alignment frame title
279 if (avm.containsArg(Arg.TITLE))
280 af.setTitle(avm.getValue(Arg.TITLE));
282 /* hacky approach to hiding the annotations */
283 // show secondary structure annotations?
284 if (avm.getBoolean(Arg.SSANNOTATION))
286 // do this better (annotation types?)
287 AlignmentUtils.showOrHideSequenceAnnotations(
288 af.getCurrentView().getAlignment(),
289 Collections.singleton("Secondary Structure"), null,
293 // show temperature factor annotations?
294 if (avm.getBoolean(Arg.NOTEMPFAC))
296 // do this better (annotation types?)
297 List<String> hideThese = new ArrayList<>();
298 hideThese.add("Temperature Factor");
299 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
300 AlignmentUtils.showOrHideSequenceAnnotations(
301 af.getCurrentView().getAlignment(), hideThese, null,
305 /* comment out hacky approach up to here and add this line:
306 if (showTemperatureFactor)
309 if (avm.containsArg(Arg.TEMPFAC_LABEL))
311 AlignmentAnnotation aa = AlignmentUtils
312 .getFirstSequenceAnnotationOfType(
313 af.getCurrentView().getAlignment(),
314 AlignmentAnnotation.LINE_GRAPH);
315 String label = avm.getValue(Arg.TEMPFAC_LABEL);
323 "Could not find annotation to apply tempfac_label '"
329 // store the AlignFrame for this id
332 // is it its own structure file?
333 if (format.isStructureFile())
335 StructureSelectionManager ssm = StructureSelectionManager
336 .getStructureSelectionManager(Desktop.instance);
337 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
338 ssm.computeMapping(false, new SequenceI[] { seq }, null,
339 openFile, DataSourceType.FILE, null, null, null);
345 "Opening '" + openFile + "' in existing alignment frame");
346 af.getCurrentView().addFile(new File(openFile), format);
349 Console.debug("Command " + Arg.OPEN + " executed successfully!");
352 if (first) // first=true means nothing opened
356 Console.error("Could not open any files in headless mode");
361 Console.warn("No more files to open");
363 desktop.setProgressBar(null, progress);
369 // open the structure (from same PDB file or given PDBfile)
370 if (!avm.getBoolean(Arg.NOSTRUCTURE))
372 AlignFrame af = afMap.get(id);
373 if (avm.containsArg(Arg.STRUCTURE))
375 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
377 String val = av.getValue();
378 SubVals subId = new SubVals(val);
379 SequenceI seq = getSpecifiedSequence(af, subId);
382 Console.warn("Could not find sequence for argument --"
383 + Arg.STRUCTURE + "=" + val);
384 // you probably want to continue here, not break
388 File structureFile = null;
389 if (subId.getContent() != null
390 && subId.getContent().length() != 0)
392 structureFile = new File(subId.getContent());
393 Console.debug("Using structure file (from argument) '"
394 + structureFile.getAbsolutePath() + "'");
399 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
400 .associatePdbWithSeq(selectedPdbFileName,
401 DataSourceType.FILE, selectedSequence, true,
404 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
406 { selectedSequence });
410 /* THIS DOESN'T WORK */
411 else if (seq.getAllPDBEntries() != null
412 && seq.getAllPDBEntries().size() > 0)
414 structureFile = new File(
415 seq.getAllPDBEntries().elementAt(0).getFile());
416 Console.debug("Using structure file (from sequence) '"
417 + structureFile.getAbsolutePath() + "'");
420 if (structureFile == null)
422 Console.warn("Not provided structure file with '" + val + "'");
426 if (!structureFile.exists())
428 Console.warn("Structure file '"
429 + structureFile.getAbsoluteFile() + "' not found.");
433 Console.debug("Using structure file "
434 + structureFile.getAbsolutePath());
436 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
437 .associatePdbWithSeq(structureFile.getAbsolutePath(),
438 DataSourceType.FILE, seq, true, Desktop.instance);
440 // open structure view
441 AlignmentPanel ap = af.alignPanel;
444 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
445 StructureViewer.ViewerType.JMOL.toString());
448 // get tft, paeFilename, label?
450 ArgValue tftAv = avm.getArgValuesReferringTo("structid", structId,
453 StructureChooser.openStructureFileForSequence(null, null, ap, seq,
454 false, structureFile.getAbsolutePath(), null, null); // tft,
460 // load a pAE file if given
461 if (avm.containsArg(Arg.PAEMATRIX))
463 AlignFrame af = afMap.get(id);
466 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
468 String val = av.getValue();
469 SubVals subVals = ArgParser.getSubVals(val);
470 File paeFile = new File(subVals.getContent());
471 String paePath = null;
474 paePath = paeFile.getCanonicalPath();
475 } catch (IOException e)
477 paePath = paeFile.getAbsolutePath();
479 "Problem with the PAE file path: '" + paePath + "'");
481 String structId = subVals.get("structid");
482 if (subVals.notSet())
484 // take structid from pdbfilename
486 if (subVals.has("structfile"))
488 Console.info("***** Attaching paeFile '" + paePath + "' to "
489 + "structfile=" + subVals.get("structfile"));
490 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
491 paeFile, subVals.getIndex(), subVals.get("structfile"),
494 else if (subVals.has("structid"))
496 Console.info("***** Attaching paeFile '" + paePath + "' to "
497 + "structid=" + subVals.get("structid"));
498 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
499 paeFile, subVals.getIndex(), subVals.get("structid"),
504 Console.debug("***** Attaching paeFile '" + paePath
505 + "' to sequence index " + subVals.getIndex());
506 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
507 paeFile, subVals.getIndex(), null, false, false);
508 // required to readjust the height and position of the pAE
511 for (AlignmentViewPanel ap : af.getAlignPanels())
513 ap.adjustAnnotationHeight();
519 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
522 AlignFrame af = afMap.get(id);
523 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
524 .findAnnotation(PDBChain.class.getName().toString()))
526 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
527 af.alignPanel.av.getGlobalColourScheme(), 0);
528 acg.setSeqAssociated(true);
529 af.changeColour(acg);
530 Console.info("Changed colour " + acg.toString());
535 protected void processImages(String id)
537 ArgValuesMap avm = argParser.linkedArgs(id);
538 AlignFrame af = afMap.get(id);
542 Console.warn("Did not have an alignment window for id=" + id);
546 if (avm.containsArg(Arg.IMAGE))
548 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
550 String val = av.getValue();
551 SubVals subVal = new SubVals(val);
552 String type = "png"; // default
553 String fileName = subVal.getContent();
554 File file = new File(fileName);
555 if (subVal.has("type"))
557 type = subVal.get("type");
559 else if (fileName != null)
561 for (String ext : new String[] { "svg", "png", "html" })
563 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
569 // for moment we disable JSON export
570 Cache.setPropsAreReadOnly(true);
571 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
576 Console.debug("Outputting type '" + type + "' to " + fileName);
580 Console.debug("Outputting type '" + type + "' to " + fileName);
584 Console.debug("Outputting type '" + type + "' to " + fileName);
585 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
586 htmlSVG.exportHTML(fileName);
589 Console.warn("--image type '" + type + "' not known. Ignoring");
596 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
598 AlignmentI al = af.getCurrentView().getAlignment();
599 if (-1 < subId.getIndex()
600 && subId.getIndex() < al.getSequences().size())
602 return al.getSequenceAt(subId.getIndex());
604 else if (subId.has("seqid"))
606 return al.findName(subId.get("seqid"));