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 String
154 * groovyscript = m.get(Arg.GROOVY) == null ? null :
155 * m.get(Arg.GROOVY).getValue(); String file = m.get(Arg.OPEN) == null ? null :
156 * m.get(Arg.OPEN).getValue(); String data = null; FileFormatI format = null;
157 * DataSourceType protocol = null;
159 if (avm.containsArg(Arg.OPEN) || avm.containsArg(Arg.OPENNEW))
161 commandArgsProvided = true;
164 boolean first = true;
165 boolean progressBarSet = false;
167 // Combine the OPEN and OPENNEW files into one list, along with whether it
168 // was OPEN or OPENNEW
169 List<Entry<Arg, ArgValue>> openAvList = new ArrayList<>();
170 avm.getArgValueList(Arg.OPEN).stream()
171 .forEachOrdered(av -> openAvList.add(
172 new AbstractMap.SimpleEntry<Arg, ArgValue>(Arg.OPEN,
174 avm.getArgValueList(Arg.OPENNEW).stream()
175 .forEachOrdered(av -> openAvList
176 .add(new AbstractMap.SimpleEntry<Arg, ArgValue>(
178 for (Entry<Arg, ArgValue> aav : openAvList)
180 Arg a = aav.getKey();
181 ArgValue av = aav.getValue();
182 String openFile = av.getValue();
183 if (openFile == null)
186 theseArgsWereParsed = true;
190 if (!headless && desktop != null)
192 desktop.setProgressBar(
193 MessageManager.getString(
194 "status.processing_commandline_args"),
195 progress = System.currentTimeMillis());
196 progressBarSet = true;
200 if (!Platform.isJS())
202 * ignore in JavaScript -- can't just file existence - could load it?
207 if (!HttpUtils.startsWithHttpOrHttps(openFile))
209 if (!(new File(openFile)).exists())
211 Console.warn("Can't find file '" + openFile + "'");
216 DataSourceType protocol = AppletFormatAdapter
217 .checkProtocol(openFile);
219 FileFormatI format = null;
222 format = new IdentifyFile().identify(openFile, protocol);
223 } catch (FileFormatException e1)
225 Console.error("Unknown file format for '" + openFile + "'");
229 if (af == null || "true".equals(av.getSubVal("new"))
230 || a == Arg.OPENNEW || format == FileFormat.Jalview)
233 * this approach isn't working yet // get default annotations before opening
234 * AlignFrame if (m.get(Arg.SSANNOTATION) != null) {
235 * Console.debug("##### SSANNOTATION=" + m.get(Arg.SSANNOTATION).getBoolean());
236 * } if (m.get(Arg.NOTEMPFAC) != null) { Console.debug( "##### NOTEMPFAC=" +
237 * m.get(Arg.NOTEMPFAC).getBoolean()); } boolean showSecondaryStructure =
238 * (m.get(Arg.SSANNOTATION) != null) ? m.get(Arg.SSANNOTATION).getBoolean() :
239 * false; boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null) ?
240 * !m.get(Arg.NOTEMPFAC).getBoolean() : false; Console.debug("##### tempfac=" +
241 * showTemperatureFactor + ", showSS=" + showSecondaryStructure);
242 * StructureSelectionManager ssm = StructureSelectionManager
243 * .getStructureSelectionManager(Desktop.instance); if (ssm != null) {
244 * ssm.setAddTempFacAnnot(showTemperatureFactor);
245 * ssm.setProcessSecondaryStructure(showSecondaryStructure); }
248 // get kind of temperature factor annotation
249 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
250 if ((!avm.getBoolean(Arg.NOTEMPFAC))
251 && avm.containsArg(Arg.TEMPFAC))
255 tempfacType = StructureImportSettings.TFType
256 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
257 .toUpperCase(Locale.ROOT));
258 Console.debug("Obtained Temperature Factor type of '"
259 + tempfacType + "'");
260 } catch (IllegalArgumentException e)
262 // Just an error message!
263 StringBuilder sb = new StringBuilder().append("Cannot set ")
264 .append(Arg.TEMPFAC.argString()).append(" to '")
266 .append("', ignoring. Valid values are: ");
267 Iterator<StructureImportSettings.TFType> it = Arrays
268 .stream(StructureImportSettings.TFType.values())
272 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
276 Console.warn(sb.toString());
281 "Opening '" + openFile + "' in new alignment frame");
282 FileLoader fileLoader = new FileLoader(!headless);
284 StructureImportSettings.setTemperatureFactorType(tempfacType);
286 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
290 if (avm.getBoolean(Arg.WRAP))
292 af.getCurrentView().setWrapAlignment(true);
296 if (avm.containsArg(Arg.COLOUR))
298 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
301 // change alignment frame title
302 if (avm.containsArg(Arg.TITLE))
303 af.setTitle(avm.getValue(Arg.TITLE));
305 /* hacky approach to hiding the annotations */
306 // show secondary structure annotations?
307 if (avm.getBoolean(Arg.SSANNOTATION))
309 // do this better (annotation types?)
310 AlignmentUtils.showOrHideSequenceAnnotations(
311 af.getCurrentView().getAlignment(),
312 Collections.singleton("Secondary Structure"), null,
316 // show temperature factor annotations?
317 if (avm.getBoolean(Arg.NOTEMPFAC))
319 // do this better (annotation types?)
320 List<String> hideThese = new ArrayList<>();
321 hideThese.add("Temperature Factor");
322 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
323 AlignmentUtils.showOrHideSequenceAnnotations(
324 af.getCurrentView().getAlignment(), hideThese, null,
329 * comment out hacky approach up to here and add this line: if
330 * (showTemperatureFactor)
333 if (avm.containsArg(Arg.TEMPFAC_LABEL))
335 AlignmentAnnotation aa = AlignmentUtils
336 .getFirstSequenceAnnotationOfType(
337 af.getCurrentView().getAlignment(),
338 AlignmentAnnotation.LINE_GRAPH);
339 String label = avm.getValue(Arg.TEMPFAC_LABEL);
347 "Could not find annotation to apply tempfac_label '"
353 // store the AlignFrame for this id
356 // is it its own structure file?
357 if (format.isStructureFile())
359 StructureSelectionManager ssm = StructureSelectionManager
360 .getStructureSelectionManager(Desktop.instance);
361 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
362 ssm.computeMapping(false, new SequenceI[] { seq }, null,
363 openFile, DataSourceType.FILE, null, null, null);
369 "Opening '" + openFile + "' in existing alignment frame");
370 af.getCurrentView().addFile(new File(openFile), format, false);
373 Console.debug("Command " + Arg.OPEN + " executed successfully!");
376 if (first) // first=true means nothing opened
380 Jalview.exit("Could not open any files in headless mode", 1);
384 Console.warn("No more files to open");
387 if (progressBarSet && desktop != null)
388 desktop.setProgressBar(null, progress);
392 // open the structure (from same PDB file or given PDBfile)
393 if (!avm.getBoolean(Arg.NOSTRUCTURE))
395 AlignFrame af = afMap.get(id);
396 if (avm.containsArg(Arg.STRUCTURE))
398 commandArgsProvided = true;
399 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
401 String val = av.getValue();
402 SubVals subId = new SubVals(val);
403 SequenceI seq = getSpecifiedSequence(af, subId);
406 Console.warn("Could not find sequence for argument "
407 + Arg.STRUCTURE.argString() + "=" + val);
408 // you probably want to continue here, not break
412 File structureFile = null;
413 if (subId.getContent() != null
414 && subId.getContent().length() != 0)
416 structureFile = new File(subId.getContent());
417 Console.debug("Using structure file (from argument) '"
418 + structureFile.getAbsolutePath() + "'");
423 * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
424 * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE,
425 * selectedSequence, true, Desktop.instance);
427 * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new
428 * SequenceI[] { 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 String paeLabel = subVals.get("label");
493 File paeFile = new File(subVals.getContent());
494 String paePath = null;
497 paePath = paeFile.getCanonicalPath();
498 } catch (IOException e)
500 paePath = paeFile.getAbsolutePath();
502 "Problem with the PAE file path: '" + paePath + "'");
504 String structid = null;
505 String structfile = null;
507 if (subVals.notSet())
509 ArgValue likelyStructure = avm
510 .getClosestPreviousArgValueOfArg(av, Arg.STRUCTURE);
511 if (likelyStructure != null)
513 SubVals sv = likelyStructure.getSubVals();
514 if (sv != null && sv.has(ArgValues.ID))
516 structid = sv.get(ArgValues.ID);
520 structfile = likelyStructure.getValue();
524 else if (subVals.has("structfile"))
526 structfile = subVals.get("structfile");
528 else if (subVals.has("structid"))
530 structid = subVals.get("structid");
532 if (structfile != null)
534 Console.info("##### Attaching paeFile '" + paePath + "' to "
535 + "structfile=" + subVals.get("structfile"));
536 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
537 paeFile, subVals.getIndex(), subVals.get("structfile"),
538 true, false, paeLabel);
540 else if (structid != null)
542 Console.info("##### Attaching paeFile '" + paePath + "' to "
543 + "structid=" + subVals.get("structid"));
544 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
545 paeFile, subVals.getIndex(), subVals.get("structid"),
546 true, true, paeLabel);
550 Console.debug("##### Attaching paeFile '" + paePath
551 + "' to sequence index " + subVals.getIndex());
552 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
553 paeFile, subVals.getIndex(), null, false, false,
555 // required to readjust the height and position of the pAE
558 for (AlignmentViewPanel ap : af.getAlignPanels())
560 ap.adjustAnnotationHeight();
566 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
569 AlignFrame af = afMap.get(id);
570 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
571 .findAnnotation(PDBChain.class.getName().toString()))
573 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
574 af.alignPanel.av.getGlobalColourScheme(), 0);
575 acg.setSeqAssociated(true);
576 af.changeColour(acg);
577 Console.info("Changed colour " + acg.toString());
581 return theseArgsWereParsed;
584 protected boolean processImages(String id)
586 ArgValuesMap avm = argParser.linkedArgs(id);
587 AlignFrame af = afMap.get(id);
591 Console.warn("Did not have an alignment window for id=" + id);
595 if (avm.containsArg(Arg.IMAGE))
597 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
599 String val = av.getValue();
600 SubVals subVal = new SubVals(val);
601 String type = "png"; // default
602 String fileName = subVal.getContent();
603 File file = new File(fileName);
604 if (subVal.has("type"))
606 type = subVal.get("type");
608 else if (fileName != null)
610 for (String ext : new String[] { "svg", "png", "html" })
612 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
618 // for moment we disable JSON export
619 Cache.setPropsAreReadOnly(true);
620 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
625 Console.debug("Outputting type '" + type + "' to " + fileName);
629 Console.debug("Outputting type '" + type + "' to " + fileName);
633 Console.debug("Outputting type '" + type + "' to " + fileName);
634 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
635 htmlSVG.exportHTML(fileName);
638 Console.warn(Arg.IMAGE.argString() + " type '" + type
639 + "' not known. Ignoring");
647 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
649 AlignmentI al = af.getCurrentView().getAlignment();
650 if (-1 < subId.getIndex()
651 && subId.getIndex() < al.getSequences().size())
653 return al.getSequenceAt(subId.getIndex());
655 else if (subId.has("seqid"))
657 return al.findName(subId.get("seqid"));