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 boolean headless;
55 private ArgParser argParser;
57 private Map<String, AlignFrame> afMap;
59 private boolean commandArgsProvided = false;
61 private boolean argsWereParsed = false;
63 public Commands(ArgParser argparser, boolean headless)
65 this(Desktop.instance, argparser, headless);
68 public Commands(Desktop d, ArgParser argparser, boolean h)
70 argParser = argparser;
73 afMap = new HashMap<String, AlignFrame>();
74 if (argparser != null)
76 processArgs(argparser, headless);
80 private boolean processArgs(ArgParser argparser, boolean h)
82 argParser = argparser;
84 boolean theseArgsWereParsed = false;
86 if (argParser != null && argParser.linkedIds() != null)
88 for (String id : argParser.linkedIds())
90 ArgValuesMap avm = argParser.linkedArgs(id);
91 theseArgsWereParsed = true;
94 theseArgsWereParsed &= processUnlinked(id);
98 theseArgsWereParsed &= processLinked(id);
100 theseArgsWereParsed &= processImages(id);
103 if (avm.getBoolean(Arg.CLOSE))
105 AlignFrame af = afMap.get(id);
108 af.closeMenuItem_actionPerformed(true);
115 if (argParser.getBool(Arg.QUIT))
117 Jalview.getInstance().quit();
120 // carry on with jalview.bin.Jalview
121 argsWereParsed = theseArgsWereParsed;
122 return argsWereParsed;
125 public boolean commandArgsProvided()
127 return commandArgsProvided;
130 public boolean argsWereParsed()
132 return argsWereParsed;
135 protected boolean processUnlinked(String id)
137 return processLinked(id);
140 protected boolean processLinked(String id)
142 boolean theseArgsWereParsed = false;
143 ArgValuesMap avm = argParser.linkedArgs(id);
148 // script to execute after all loading is completed one way or another
149 String groovyscript = m.get(Arg.GROOVY) == null ? null
150 : m.get(Arg.GROOVY).getValue();
151 String file = m.get(Arg.OPEN) == null ? null
152 : m.get(Arg.OPEN).getValue();
154 FileFormatI format = null;
155 DataSourceType protocol = null;
157 if (avm.containsArg(Arg.OPEN))
159 commandArgsProvided = true;
162 boolean first = true;
163 boolean progressBarSet = false;
165 for (ArgValue av : avm.getArgValueList(Arg.OPEN))
167 String openFile = av.getValue();
168 if (openFile == null)
171 theseArgsWereParsed = true;
175 if (!headless && desktop != null)
177 desktop.setProgressBar(
178 MessageManager.getString(
179 "status.processing_commandline_args"),
180 progress = System.currentTimeMillis());
181 progressBarSet = true;
185 if (!Platform.isJS())
187 * ignore in JavaScript -- can't just file existence - could load it?
192 if (!HttpUtils.startsWithHttpOrHttps(openFile))
194 if (!(new File(openFile)).exists())
196 Console.warn("Can't find file '" + openFile + "'");
201 DataSourceType protocol = AppletFormatAdapter
202 .checkProtocol(openFile);
204 FileFormatI format = null;
207 format = new IdentifyFile().identify(openFile, protocol);
208 } catch (FileFormatException e1)
210 Console.error("Unknown file format for '" + openFile + "'");
217 * this approach isn't working yet
218 // get default annotations before opening AlignFrame
219 if (m.get(Arg.SSANNOTATION) != null)
221 Console.debug("***** SSANNOTATION="
222 + m.get(Arg.SSANNOTATION).getBoolean());
224 if (m.get(Arg.NOTEMPFAC) != null)
227 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
229 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
230 ? m.get(Arg.SSANNOTATION).getBoolean()
232 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
233 ? !m.get(Arg.NOTEMPFAC).getBoolean()
235 Console.debug("***** tempfac=" + showTemperatureFactor
236 + ", showSS=" + showSecondaryStructure);
237 StructureSelectionManager ssm = StructureSelectionManager
238 .getStructureSelectionManager(Desktop.instance);
241 ssm.setAddTempFacAnnot(showTemperatureFactor);
242 ssm.setProcessSecondaryStructure(showSecondaryStructure);
246 // get kind of temperature factor annotation
247 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
248 if ((!avm.getBoolean(Arg.NOTEMPFAC))
249 && avm.containsArg(Arg.TEMPFAC))
253 tempfacType = StructureImportSettings.TFType
254 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
255 .toUpperCase(Locale.ROOT));
256 Console.debug("Obtained Temperature Factor type of '"
257 + tempfacType + "'");
258 } catch (IllegalArgumentException e)
260 // Just an error message!
261 StringBuilder sb = new StringBuilder().append("Cannot set --")
262 .append(Arg.TEMPFAC.getName()).append(" to '")
264 .append("', ignoring. Valid values are: ");
265 Iterator<StructureImportSettings.TFType> it = Arrays
266 .stream(StructureImportSettings.TFType.values())
270 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
274 Console.warn(sb.toString());
279 "Opening '" + openFile + "' in new alignment frame");
280 FileLoader fileLoader = new FileLoader(!headless);
282 StructureImportSettings.setTemperatureFactorType(tempfacType);
284 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
288 if (avm.getBoolean(Arg.WRAP))
290 af.getCurrentView().setWrapAlignment(true);
294 if (avm.containsArg(Arg.COLOUR))
296 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
299 // change alignment frame title
300 if (avm.containsArg(Arg.TITLE))
301 af.setTitle(avm.getValue(Arg.TITLE));
303 /* hacky approach to hiding the annotations */
304 // show secondary structure annotations?
305 if (avm.getBoolean(Arg.SSANNOTATION))
307 // do this better (annotation types?)
308 AlignmentUtils.showOrHideSequenceAnnotations(
309 af.getCurrentView().getAlignment(),
310 Collections.singleton("Secondary Structure"), null,
314 // show temperature factor annotations?
315 if (avm.getBoolean(Arg.NOTEMPFAC))
317 // do this better (annotation types?)
318 List<String> hideThese = new ArrayList<>();
319 hideThese.add("Temperature Factor");
320 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
321 AlignmentUtils.showOrHideSequenceAnnotations(
322 af.getCurrentView().getAlignment(), hideThese, null,
326 /* comment out hacky approach up to here and add this line:
327 if (showTemperatureFactor)
330 if (avm.containsArg(Arg.TEMPFAC_LABEL))
332 AlignmentAnnotation aa = AlignmentUtils
333 .getFirstSequenceAnnotationOfType(
334 af.getCurrentView().getAlignment(),
335 AlignmentAnnotation.LINE_GRAPH);
336 String label = avm.getValue(Arg.TEMPFAC_LABEL);
344 "Could not find annotation to apply tempfac_label '"
350 // store the AlignFrame for this id
353 // is it its own structure file?
354 if (format.isStructureFile())
356 StructureSelectionManager ssm = StructureSelectionManager
357 .getStructureSelectionManager(Desktop.instance);
358 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
359 ssm.computeMapping(false, new SequenceI[] { seq }, null,
360 openFile, DataSourceType.FILE, null, null, null);
366 "Opening '" + openFile + "' in existing alignment frame");
367 af.getCurrentView().addFile(new File(openFile), format, false);
370 Console.debug("Command " + Arg.OPEN + " executed successfully!");
373 if (first) // first=true means nothing opened
377 Console.error("Could not open any files in headless mode");
382 Console.warn("No more files to open");
385 if (progressBarSet && desktop != null)
386 desktop.setProgressBar(null, progress);
390 // open the structure (from same PDB file or given PDBfile)
391 if (!avm.getBoolean(Arg.NOSTRUCTURE))
393 AlignFrame af = afMap.get(id);
394 if (avm.containsArg(Arg.STRUCTURE))
396 commandArgsProvided = true;
397 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
399 String val = av.getValue();
400 SubVals subId = new SubVals(val);
401 SequenceI seq = getSpecifiedSequence(af, subId);
404 Console.warn("Could not find sequence for argument --"
405 + Arg.STRUCTURE + "=" + val);
406 // you probably want to continue here, not break
410 File structureFile = null;
411 if (subId.getContent() != null
412 && subId.getContent().length() != 0)
414 structureFile = new File(subId.getContent());
415 Console.debug("Using structure file (from argument) '"
416 + structureFile.getAbsolutePath() + "'");
421 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
422 .associatePdbWithSeq(selectedPdbFileName,
423 DataSourceType.FILE, selectedSequence, true,
426 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
428 { selectedSequence });
432 /* THIS DOESN'T WORK */
433 else if (seq.getAllPDBEntries() != null
434 && seq.getAllPDBEntries().size() > 0)
436 structureFile = new File(
437 seq.getAllPDBEntries().elementAt(0).getFile());
438 Console.debug("Using structure file (from sequence) '"
439 + structureFile.getAbsolutePath() + "'");
442 if (structureFile == null)
444 Console.warn("Not provided structure file with '" + val + "'");
448 if (!structureFile.exists())
450 Console.warn("Structure file '"
451 + structureFile.getAbsoluteFile() + "' not found.");
455 Console.debug("Using structure file "
456 + structureFile.getAbsolutePath());
458 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
459 .associatePdbWithSeq(structureFile.getAbsolutePath(),
460 DataSourceType.FILE, seq, true, Desktop.instance);
462 // open structure view
463 AlignmentPanel ap = af.alignPanel;
466 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
467 StructureViewer.ViewerType.JMOL.toString());
470 // get tft, paeFilename, label?
472 ArgValue tftAv = avm.getArgValuesReferringTo("structid", structId,
475 StructureChooser.openStructureFileForSequence(null, null, ap, seq,
476 false, structureFile.getAbsolutePath(), null, null); // tft,
482 // load a pAE file if given
483 if (avm.containsArg(Arg.PAEMATRIX))
485 AlignFrame af = afMap.get(id);
488 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
490 String val = av.getValue();
491 SubVals subVals = ArgParser.getSubVals(val);
492 File paeFile = new File(subVals.getContent());
493 String paePath = null;
496 paePath = paeFile.getCanonicalPath();
497 } catch (IOException e)
499 paePath = paeFile.getAbsolutePath();
501 "Problem with the PAE file path: '" + paePath + "'");
503 String structId = subVals.get("structid");
504 if (subVals.notSet())
506 // take structid from pdbfilename
508 if (subVals.has("structfile"))
510 Console.info("***** Attaching paeFile '" + paePath + "' to "
511 + "structfile=" + subVals.get("structfile"));
512 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
513 paeFile, subVals.getIndex(), subVals.get("structfile"),
516 else if (subVals.has("structid"))
518 Console.info("***** Attaching paeFile '" + paePath + "' to "
519 + "structid=" + subVals.get("structid"));
520 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
521 paeFile, subVals.getIndex(), subVals.get("structid"),
526 Console.debug("***** Attaching paeFile '" + paePath
527 + "' to sequence index " + subVals.getIndex());
528 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
529 paeFile, subVals.getIndex(), null, false, false);
530 // required to readjust the height and position of the pAE
533 for (AlignmentViewPanel ap : af.getAlignPanels())
535 ap.adjustAnnotationHeight();
541 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
544 AlignFrame af = afMap.get(id);
545 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
546 .findAnnotation(PDBChain.class.getName().toString()))
548 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
549 af.alignPanel.av.getGlobalColourScheme(), 0);
550 acg.setSeqAssociated(true);
551 af.changeColour(acg);
552 Console.info("Changed colour " + acg.toString());
556 return theseArgsWereParsed;
559 protected boolean processImages(String id)
561 ArgValuesMap avm = argParser.linkedArgs(id);
562 AlignFrame af = afMap.get(id);
566 Console.warn("Did not have an alignment window for id=" + id);
570 if (avm.containsArg(Arg.IMAGE))
572 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
574 String val = av.getValue();
575 SubVals subVal = new SubVals(val);
576 String type = "png"; // default
577 String fileName = subVal.getContent();
578 File file = new File(fileName);
579 if (subVal.has("type"))
581 type = subVal.get("type");
583 else if (fileName != null)
585 for (String ext : new String[] { "svg", "png", "html" })
587 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
593 // for moment we disable JSON export
594 Cache.setPropsAreReadOnly(true);
595 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
600 Console.debug("Outputting type '" + type + "' to " + fileName);
604 Console.debug("Outputting type '" + type + "' to " + fileName);
608 Console.debug("Outputting type '" + type + "' to " + fileName);
609 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
610 htmlSVG.exportHTML(fileName);
613 Console.warn("--image type '" + type + "' not known. Ignoring");
621 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
623 AlignmentI al = af.getCurrentView().getAlignment();
624 if (-1 < subId.getIndex()
625 && subId.getIndex() < al.getSequences().size())
627 return al.getSequenceAt(subId.getIndex());
629 else if (subId.has("seqid"))
631 return al.findName(subId.get("seqid"));