4 import java.io.IOException;
5 import java.util.AbstractMap;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Locale;
14 import java.util.Map.Entry;
16 import jalview.analysis.AlignmentUtils;
17 import jalview.api.AlignmentViewPanel;
18 import jalview.bin.argparser.Arg;
19 import jalview.bin.argparser.ArgParser;
20 import jalview.bin.argparser.ArgValue;
21 import jalview.bin.argparser.ArgValues;
22 import jalview.bin.argparser.ArgValuesMap;
23 import jalview.bin.argparser.SubVals;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.AlignmentPanel;
31 import jalview.gui.AssociatePdbFileWithSeq;
32 import jalview.gui.Desktop;
33 import jalview.gui.Preferences;
34 import jalview.gui.StructureChooser;
35 import jalview.gui.StructureViewer;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.DataSourceType;
38 import jalview.io.FileFormat;
39 import jalview.io.FileFormatException;
40 import jalview.io.FileFormatI;
41 import jalview.io.FileLoader;
42 import jalview.io.HtmlSvgOutput;
43 import jalview.io.IdentifyFile;
44 import jalview.schemes.AnnotationColourGradient;
45 import jalview.structure.StructureImportSettings;
46 import jalview.structure.StructureImportSettings.TFType;
47 import jalview.structure.StructureSelectionManager;
48 import jalview.util.HttpUtils;
49 import jalview.util.MessageManager;
50 import jalview.util.Platform;
51 import jalview.ws.dbsources.EBIAlfaFold;
52 import mc_view.PDBChain;
58 private boolean headless;
60 private ArgParser argParser;
62 private Map<String, AlignFrame> afMap;
64 private boolean commandArgsProvided = false;
66 private boolean argsWereParsed = false;
68 public Commands(ArgParser argparser, boolean headless)
70 this(Desktop.instance, argparser, headless);
73 public Commands(Desktop d, ArgParser argparser, boolean h)
75 argParser = argparser;
78 afMap = new HashMap<String, AlignFrame>();
79 if (argparser != null)
81 processArgs(argparser, headless);
85 private boolean processArgs(ArgParser argparser, boolean h)
87 argParser = argparser;
89 boolean theseArgsWereParsed = false;
91 if (argParser != null && argParser.linkedIds() != null)
93 for (String id : argParser.linkedIds())
95 ArgValuesMap avm = argParser.linkedArgs(id);
96 theseArgsWereParsed = true;
99 theseArgsWereParsed &= processUnlinked(id);
103 theseArgsWereParsed &= processLinked(id);
105 theseArgsWereParsed &= processImages(id);
108 if (avm.getBoolean(Arg.CLOSE))
110 AlignFrame af = afMap.get(id);
113 af.closeMenuItem_actionPerformed(true);
120 if (argParser.getBool(Arg.QUIT))
122 Jalview.getInstance().quit();
125 // carry on with jalview.bin.Jalview
126 argsWereParsed = theseArgsWereParsed;
127 return argsWereParsed;
130 public boolean commandArgsProvided()
132 return commandArgsProvided;
135 public boolean argsWereParsed()
137 return argsWereParsed;
140 protected boolean processUnlinked(String id)
142 return processLinked(id);
145 protected boolean processLinked(String id)
147 boolean theseArgsWereParsed = false;
148 ArgValuesMap avm = argParser.linkedArgs(id);
153 // script to execute after all loading is completed one way or another
154 String groovyscript = m.get(Arg.GROOVY) == null ? null
155 : m.get(Arg.GROOVY).getValue();
156 String file = m.get(Arg.OPEN) == null ? null
157 : m.get(Arg.OPEN).getValue();
159 FileFormatI format = null;
160 DataSourceType protocol = null;
162 if (avm.containsArg(Arg.OPEN) || avm.containsArg(Arg.OPENNEW))
164 commandArgsProvided = true;
167 boolean first = true;
168 boolean progressBarSet = false;
170 // Combine the OPEN and OPENNEW files into one list, along with whether it
171 // was OPEN or OPENNEW
172 List<Entry<Arg, ArgValue>> openAvList = new ArrayList<>();
173 avm.getArgValueList(Arg.OPEN).stream()
174 .forEachOrdered(av -> openAvList.add(
175 new AbstractMap.SimpleEntry<Arg, ArgValue>(Arg.OPEN,
177 avm.getArgValueList(Arg.OPENNEW).stream()
178 .forEachOrdered(av -> openAvList
179 .add(new AbstractMap.SimpleEntry<Arg, ArgValue>(
181 for (Entry<Arg, ArgValue> aav : openAvList)
183 Arg a = aav.getKey();
184 ArgValue av = aav.getValue();
185 String openFile = av.getValue();
186 if (openFile == null)
189 theseArgsWereParsed = true;
193 if (!headless && desktop != null)
195 desktop.setProgressBar(
196 MessageManager.getString(
197 "status.processing_commandline_args"),
198 progress = System.currentTimeMillis());
199 progressBarSet = true;
203 if (!Platform.isJS())
205 * ignore in JavaScript -- can't just file existence - could load it?
210 if (!HttpUtils.startsWithHttpOrHttps(openFile))
212 if (!(new File(openFile)).exists())
214 Console.warn("Can't find file '" + openFile + "'");
219 DataSourceType protocol = AppletFormatAdapter
220 .checkProtocol(openFile);
222 FileFormatI format = null;
225 format = new IdentifyFile().identify(openFile, protocol);
226 } catch (FileFormatException e1)
228 Console.error("Unknown file format for '" + openFile + "'");
232 if (af == null || "true".equals(av.getSubVal("new"))
233 || a == Arg.OPENNEW || format == FileFormat.Jalview)
236 * this approach isn't working yet
237 // get default annotations before opening AlignFrame
238 if (m.get(Arg.SSANNOTATION) != null)
240 Console.debug("***** SSANNOTATION="
241 + m.get(Arg.SSANNOTATION).getBoolean());
243 if (m.get(Arg.NOTEMPFAC) != null)
246 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
248 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
249 ? m.get(Arg.SSANNOTATION).getBoolean()
251 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
252 ? !m.get(Arg.NOTEMPFAC).getBoolean()
254 Console.debug("***** tempfac=" + showTemperatureFactor
255 + ", showSS=" + showSecondaryStructure);
256 StructureSelectionManager ssm = StructureSelectionManager
257 .getStructureSelectionManager(Desktop.instance);
260 ssm.setAddTempFacAnnot(showTemperatureFactor);
261 ssm.setProcessSecondaryStructure(showSecondaryStructure);
265 // get kind of temperature factor annotation
266 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
267 if ((!avm.getBoolean(Arg.NOTEMPFAC))
268 && avm.containsArg(Arg.TEMPFAC))
272 tempfacType = StructureImportSettings.TFType
273 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
274 .toUpperCase(Locale.ROOT));
275 Console.debug("Obtained Temperature Factor type of '"
276 + tempfacType + "'");
277 } catch (IllegalArgumentException e)
279 // Just an error message!
280 StringBuilder sb = new StringBuilder().append("Cannot set --")
281 .append(Arg.TEMPFAC.getName()).append(" to '")
283 .append("', ignoring. Valid values are: ");
284 Iterator<StructureImportSettings.TFType> it = Arrays
285 .stream(StructureImportSettings.TFType.values())
289 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
293 Console.warn(sb.toString());
298 "Opening '" + openFile + "' in new alignment frame");
299 FileLoader fileLoader = new FileLoader(!headless);
301 StructureImportSettings.setTemperatureFactorType(tempfacType);
303 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
307 if (avm.getBoolean(Arg.WRAP))
309 af.getCurrentView().setWrapAlignment(true);
313 if (avm.containsArg(Arg.COLOUR))
315 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
318 // change alignment frame title
319 if (avm.containsArg(Arg.TITLE))
320 af.setTitle(avm.getValue(Arg.TITLE));
322 /* hacky approach to hiding the annotations */
323 // show secondary structure annotations?
324 if (avm.getBoolean(Arg.SSANNOTATION))
326 // do this better (annotation types?)
327 AlignmentUtils.showOrHideSequenceAnnotations(
328 af.getCurrentView().getAlignment(),
329 Collections.singleton("Secondary Structure"), null,
333 // show temperature factor annotations?
334 if (avm.getBoolean(Arg.NOTEMPFAC))
336 // do this better (annotation types?)
337 List<String> hideThese = new ArrayList<>();
338 hideThese.add("Temperature Factor");
339 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
340 AlignmentUtils.showOrHideSequenceAnnotations(
341 af.getCurrentView().getAlignment(), hideThese, null,
345 /* comment out hacky approach up to here and add this line:
346 if (showTemperatureFactor)
349 if (avm.containsArg(Arg.TEMPFAC_LABEL))
351 AlignmentAnnotation aa = AlignmentUtils
352 .getFirstSequenceAnnotationOfType(
353 af.getCurrentView().getAlignment(),
354 AlignmentAnnotation.LINE_GRAPH);
355 String label = avm.getValue(Arg.TEMPFAC_LABEL);
363 "Could not find annotation to apply tempfac_label '"
369 // store the AlignFrame for this id
372 // is it its own structure file?
373 if (format.isStructureFile())
375 StructureSelectionManager ssm = StructureSelectionManager
376 .getStructureSelectionManager(Desktop.instance);
377 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
378 ssm.computeMapping(false, new SequenceI[] { seq }, null,
379 openFile, DataSourceType.FILE, null, null, null);
385 "Opening '" + openFile + "' in existing alignment frame");
386 af.getCurrentView().addFile(new File(openFile), format, false);
389 Console.debug("Command " + Arg.OPEN + " executed successfully!");
392 if (first) // first=true means nothing opened
396 Jalview.exit("Could not open any files in headless mode", 1);
400 Console.warn("No more files to open");
403 if (progressBarSet && desktop != null)
404 desktop.setProgressBar(null, progress);
408 // open the structure (from same PDB file or given PDBfile)
409 if (!avm.getBoolean(Arg.NOSTRUCTURE))
411 AlignFrame af = afMap.get(id);
412 if (avm.containsArg(Arg.STRUCTURE))
414 commandArgsProvided = true;
415 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
417 String val = av.getValue();
418 SubVals subId = new SubVals(val);
419 SequenceI seq = getSpecifiedSequence(af, subId);
422 Console.warn("Could not find sequence for argument --"
423 + Arg.STRUCTURE + "=" + val);
424 // you probably want to continue here, not break
428 File structureFile = null;
429 if (subId.getContent() != null
430 && subId.getContent().length() != 0)
432 structureFile = new File(subId.getContent());
433 Console.debug("Using structure file (from argument) '"
434 + structureFile.getAbsolutePath() + "'");
439 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
440 .associatePdbWithSeq(selectedPdbFileName,
441 DataSourceType.FILE, selectedSequence, true,
444 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
446 { selectedSequence });
450 /* THIS DOESN'T WORK */
451 else if (seq.getAllPDBEntries() != null
452 && seq.getAllPDBEntries().size() > 0)
454 structureFile = new File(
455 seq.getAllPDBEntries().elementAt(0).getFile());
456 Console.debug("Using structure file (from sequence) '"
457 + structureFile.getAbsolutePath() + "'");
460 if (structureFile == null)
462 Console.warn("Not provided structure file with '" + val + "'");
466 if (!structureFile.exists())
468 Console.warn("Structure file '"
469 + structureFile.getAbsoluteFile() + "' not found.");
473 Console.debug("Using structure file "
474 + structureFile.getAbsolutePath());
476 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
477 .associatePdbWithSeq(structureFile.getAbsolutePath(),
478 DataSourceType.FILE, seq, true, Desktop.instance);
480 // open structure view
481 AlignmentPanel ap = af.alignPanel;
484 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
485 StructureViewer.ViewerType.JMOL.toString());
488 // get tft, paeFilename, label?
490 ArgValue tftAv = avm.getArgValuesReferringTo("structid", structId,
493 StructureChooser.openStructureFileForSequence(null, null, ap, seq,
494 false, structureFile.getAbsolutePath(), null, null); // tft,
500 // load a pAE file if given
501 if (avm.containsArg(Arg.PAEMATRIX))
503 AlignFrame af = afMap.get(id);
506 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
508 String val = av.getValue();
509 SubVals subVals = ArgParser.getSubVals(val);
510 String paeLabel = subVals.get("label");
511 File paeFile = new File(subVals.getContent());
512 String paePath = null;
515 paePath = paeFile.getCanonicalPath();
516 } catch (IOException e)
518 paePath = paeFile.getAbsolutePath();
520 "Problem with the PAE file path: '" + paePath + "'");
522 String structid = null;
523 String structfile = null;
525 if (subVals.notSet())
527 ArgValue likelyStructure = avm
528 .getClosestPreviousArgValueOfArg(av, Arg.STRUCTURE);
529 if (likelyStructure != null)
531 SubVals sv = likelyStructure.getSubVals();
532 if (sv != null && sv.has(ArgValues.ID))
534 structid = sv.get(ArgValues.ID);
538 structfile = likelyStructure.getValue();
542 else if (subVals.has("structfile"))
544 structfile = subVals.get("structfile");
546 else if (subVals.has("structid"))
548 structid = subVals.get("structid");
550 if (structfile != null)
552 Console.info("***** Attaching paeFile '" + paePath + "' to "
553 + "structfile=" + subVals.get("structfile"));
554 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
555 paeFile, subVals.getIndex(), subVals.get("structfile"),
556 true, false, paeLabel);
558 else if (structid != null)
560 Console.info("***** Attaching paeFile '" + paePath + "' to "
561 + "structid=" + subVals.get("structid"));
562 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
563 paeFile, subVals.getIndex(), subVals.get("structid"),
564 true, true, paeLabel);
568 Console.debug("***** Attaching paeFile '" + paePath
569 + "' to sequence index " + subVals.getIndex());
570 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
571 paeFile, subVals.getIndex(), null, false, false,
573 // required to readjust the height and position of the pAE
576 for (AlignmentViewPanel ap : af.getAlignPanels())
578 ap.adjustAnnotationHeight();
584 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
587 AlignFrame af = afMap.get(id);
588 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
589 .findAnnotation(PDBChain.class.getName().toString()))
591 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
592 af.alignPanel.av.getGlobalColourScheme(), 0);
593 acg.setSeqAssociated(true);
594 af.changeColour(acg);
595 Console.info("Changed colour " + acg.toString());
599 return theseArgsWereParsed;
602 protected boolean processImages(String id)
604 ArgValuesMap avm = argParser.linkedArgs(id);
605 AlignFrame af = afMap.get(id);
609 Console.warn("Did not have an alignment window for id=" + id);
613 if (avm.containsArg(Arg.IMAGE))
615 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
617 String val = av.getValue();
618 SubVals subVal = new SubVals(val);
619 String type = "png"; // default
620 String fileName = subVal.getContent();
621 File file = new File(fileName);
622 if (subVal.has("type"))
624 type = subVal.get("type");
626 else if (fileName != null)
628 for (String ext : new String[] { "svg", "png", "html" })
630 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
636 // for moment we disable JSON export
637 Cache.setPropsAreReadOnly(true);
638 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
643 Console.debug("Outputting type '" + type + "' to " + fileName);
647 Console.debug("Outputting type '" + type + "' to " + fileName);
651 Console.debug("Outputting type '" + type + "' to " + fileName);
652 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
653 htmlSVG.exportHTML(fileName);
656 Console.warn("--image type '" + type + "' not known. Ignoring");
664 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
666 AlignmentI al = af.getCurrentView().getAlignment();
667 if (-1 < subId.getIndex()
668 && subId.getIndex() < al.getSequences().size())
670 return al.getSequenceAt(subId.getIndex());
672 else if (subId.has("seqid"))
674 return al.findName(subId.get("seqid"));