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.ArgParser;
18 import jalview.bin.argparser.ArgValue;
19 import jalview.bin.argparser.ArgValues;
20 import jalview.bin.argparser.ArgValuesMap;
21 import jalview.bin.argparser.SubVals;
22 import jalview.datamodel.AlignmentAnnotation;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.AlignmentPanel;
29 import jalview.gui.AssociatePdbFileWithSeq;
30 import jalview.gui.Desktop;
31 import jalview.gui.Preferences;
32 import jalview.gui.StructureChooser;
33 import jalview.gui.StructureViewer;
34 import jalview.io.AppletFormatAdapter;
35 import jalview.io.DataSourceType;
36 import jalview.io.FileFormatException;
37 import jalview.io.FileFormatI;
38 import jalview.io.FileLoader;
39 import jalview.io.HtmlSvgOutput;
40 import jalview.io.IdentifyFile;
41 import jalview.schemes.AnnotationColourGradient;
42 import jalview.structure.StructureImportSettings;
43 import jalview.structure.StructureImportSettings.TFType;
44 import jalview.structure.StructureSelectionManager;
45 import jalview.util.HttpUtils;
46 import jalview.util.MessageManager;
47 import jalview.util.Platform;
48 import jalview.ws.dbsources.EBIAlfaFold;
49 import mc_view.PDBChain;
55 private boolean headless;
57 private ArgParser argParser;
59 private Map<String, AlignFrame> afMap;
61 private boolean commandArgsProvided = false;
63 private boolean argsWereParsed = false;
65 public Commands(ArgParser argparser, boolean headless)
67 this(Desktop.instance, argparser, headless);
70 public Commands(Desktop d, ArgParser argparser, boolean h)
72 argParser = argparser;
75 afMap = new HashMap<String, AlignFrame>();
76 if (argparser != null)
78 processArgs(argparser, headless);
82 private boolean processArgs(ArgParser argparser, boolean h)
84 argParser = argparser;
86 boolean theseArgsWereParsed = false;
88 if (argParser != null && argParser.linkedIds() != null)
90 for (String id : argParser.linkedIds())
92 ArgValuesMap avm = argParser.linkedArgs(id);
93 theseArgsWereParsed = true;
96 theseArgsWereParsed &= processUnlinked(id);
100 theseArgsWereParsed &= processLinked(id);
102 theseArgsWereParsed &= processImages(id);
105 if (avm.getBoolean(Arg.CLOSE))
107 AlignFrame af = afMap.get(id);
110 af.closeMenuItem_actionPerformed(true);
117 if (argParser.getBool(Arg.QUIT))
119 Jalview.getInstance().quit();
122 // carry on with jalview.bin.Jalview
123 argsWereParsed = theseArgsWereParsed;
124 return argsWereParsed;
127 public boolean commandArgsProvided()
129 return commandArgsProvided;
132 public boolean argsWereParsed()
134 return argsWereParsed;
137 protected boolean processUnlinked(String id)
139 return processLinked(id);
142 protected boolean processLinked(String id)
144 boolean theseArgsWereParsed = false;
145 ArgValuesMap avm = argParser.linkedArgs(id);
150 // script to execute after all loading is completed one way or another
151 String groovyscript = m.get(Arg.GROOVY) == null ? null
152 : m.get(Arg.GROOVY).getValue();
153 String file = m.get(Arg.OPEN) == null ? null
154 : m.get(Arg.OPEN).getValue();
156 FileFormatI format = null;
157 DataSourceType protocol = null;
159 if (avm.containsArg(Arg.OPEN))
161 commandArgsProvided = true;
164 boolean first = true;
165 boolean progressBarSet = false;
167 for (ArgValue av : avm.getArgValueList(Arg.OPEN))
169 String openFile = av.getValue();
170 if (openFile == null)
173 theseArgsWereParsed = true;
177 if (!headless && desktop != null)
179 desktop.setProgressBar(
180 MessageManager.getString(
181 "status.processing_commandline_args"),
182 progress = System.currentTimeMillis());
183 progressBarSet = true;
187 if (!Platform.isJS())
189 * ignore in JavaScript -- can't just file existence - could load it?
194 if (!HttpUtils.startsWithHttpOrHttps(openFile))
196 if (!(new File(openFile)).exists())
198 Console.warn("Can't find file '" + openFile + "'");
203 DataSourceType protocol = AppletFormatAdapter
204 .checkProtocol(openFile);
206 FileFormatI format = null;
209 format = new IdentifyFile().identify(openFile, protocol);
210 } catch (FileFormatException e1)
212 Console.error("Unknown file format for '" + openFile + "'");
219 * this approach isn't working yet
220 // get default annotations before opening AlignFrame
221 if (m.get(Arg.SSANNOTATION) != null)
223 Console.debug("***** SSANNOTATION="
224 + m.get(Arg.SSANNOTATION).getBoolean());
226 if (m.get(Arg.NOTEMPFAC) != null)
229 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
231 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
232 ? m.get(Arg.SSANNOTATION).getBoolean()
234 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
235 ? !m.get(Arg.NOTEMPFAC).getBoolean()
237 Console.debug("***** tempfac=" + showTemperatureFactor
238 + ", showSS=" + showSecondaryStructure);
239 StructureSelectionManager ssm = StructureSelectionManager
240 .getStructureSelectionManager(Desktop.instance);
243 ssm.setAddTempFacAnnot(showTemperatureFactor);
244 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.getName()).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,
328 /* comment out hacky approach up to here and add this line:
329 if (showTemperatureFactor)
332 if (avm.containsArg(Arg.TEMPFAC_LABEL))
334 AlignmentAnnotation aa = AlignmentUtils
335 .getFirstSequenceAnnotationOfType(
336 af.getCurrentView().getAlignment(),
337 AlignmentAnnotation.LINE_GRAPH);
338 String label = avm.getValue(Arg.TEMPFAC_LABEL);
346 "Could not find annotation to apply tempfac_label '"
352 // store the AlignFrame for this id
355 // is it its own structure file?
356 if (format.isStructureFile())
358 StructureSelectionManager ssm = StructureSelectionManager
359 .getStructureSelectionManager(Desktop.instance);
360 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
361 ssm.computeMapping(false, new SequenceI[] { seq }, null,
362 openFile, DataSourceType.FILE, null, null, null);
368 "Opening '" + openFile + "' in existing alignment frame");
369 af.getCurrentView().addFile(new File(openFile), format, false);
372 Console.debug("Command " + Arg.OPEN + " executed successfully!");
375 if (first) // first=true means nothing opened
379 Jalview.exit("Could not open any files in headless mode", 1);
383 Console.warn("No more files to open");
386 if (progressBarSet && desktop != null)
387 desktop.setProgressBar(null, progress);
391 // open the structure (from same PDB file or given PDBfile)
392 if (!avm.getBoolean(Arg.NOSTRUCTURE))
394 AlignFrame af = afMap.get(id);
395 if (avm.containsArg(Arg.STRUCTURE))
397 commandArgsProvided = true;
398 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
400 String val = av.getValue();
401 SubVals subId = new SubVals(val);
402 SequenceI seq = getSpecifiedSequence(af, subId);
405 Console.warn("Could not find sequence for argument --"
406 + Arg.STRUCTURE + "=" + val);
407 // you probably want to continue here, not break
411 File structureFile = null;
412 if (subId.getContent() != null
413 && subId.getContent().length() != 0)
415 structureFile = new File(subId.getContent());
416 Console.debug("Using structure file (from argument) '"
417 + structureFile.getAbsolutePath() + "'");
422 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
423 .associatePdbWithSeq(selectedPdbFileName,
424 DataSourceType.FILE, selectedSequence, true,
427 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
429 { selectedSequence });
433 /* THIS DOESN'T WORK */
434 else if (seq.getAllPDBEntries() != null
435 && seq.getAllPDBEntries().size() > 0)
437 structureFile = new File(
438 seq.getAllPDBEntries().elementAt(0).getFile());
439 Console.debug("Using structure file (from sequence) '"
440 + structureFile.getAbsolutePath() + "'");
443 if (structureFile == null)
445 Console.warn("Not provided structure file with '" + val + "'");
449 if (!structureFile.exists())
451 Console.warn("Structure file '"
452 + structureFile.getAbsoluteFile() + "' not found.");
456 Console.debug("Using structure file "
457 + structureFile.getAbsolutePath());
459 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
460 .associatePdbWithSeq(structureFile.getAbsolutePath(),
461 DataSourceType.FILE, seq, true, Desktop.instance);
463 // open structure view
464 AlignmentPanel ap = af.alignPanel;
467 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
468 StructureViewer.ViewerType.JMOL.toString());
471 // get tft, paeFilename, label?
473 ArgValue tftAv = avm.getArgValuesReferringTo("structid", structId,
476 StructureChooser.openStructureFileForSequence(null, null, ap, seq,
477 false, structureFile.getAbsolutePath(), null, null); // tft,
483 // load a pAE file if given
484 if (avm.containsArg(Arg.PAEMATRIX))
486 AlignFrame af = afMap.get(id);
489 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
491 String val = av.getValue();
492 SubVals subVals = ArgParser.getSubVals(val);
493 String paeLabel = subVals.get("label");
494 File paeFile = new File(subVals.getContent());
495 String paePath = null;
498 paePath = paeFile.getCanonicalPath();
499 } catch (IOException e)
501 paePath = paeFile.getAbsolutePath();
503 "Problem with the PAE file path: '" + paePath + "'");
505 String structid = null;
506 String structfile = null;
508 if (subVals.notSet())
510 ArgValue likelyStructure = avm
511 .getClosestPreviousArgValueOfArg(av, Arg.STRUCTURE);
512 if (likelyStructure != null)
514 SubVals sv = likelyStructure.getSubVals();
515 if (sv != null && sv.has(ArgValues.ID))
517 structid = sv.get(ArgValues.ID);
521 structfile = likelyStructure.getValue();
525 else if (subVals.has("structfile"))
527 structfile = subVals.get("structfile");
529 else if (subVals.has("structid"))
531 structid = subVals.get("structid");
533 if (structfile != null)
535 Console.info("***** Attaching paeFile '" + paePath + "' to "
536 + "structfile=" + subVals.get("structfile"));
537 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
538 paeFile, subVals.getIndex(), subVals.get("structfile"),
539 true, false, paeLabel);
541 else if (structid != null)
543 Console.info("***** Attaching paeFile '" + paePath + "' to "
544 + "structid=" + subVals.get("structid"));
545 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
546 paeFile, subVals.getIndex(), subVals.get("structid"),
547 true, true, paeLabel);
551 Console.debug("***** Attaching paeFile '" + paePath
552 + "' to sequence index " + subVals.getIndex());
553 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
554 paeFile, subVals.getIndex(), null, false, false,
556 // required to readjust the height and position of the pAE
559 for (AlignmentViewPanel ap : af.getAlignPanels())
561 ap.adjustAnnotationHeight();
567 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
570 AlignFrame af = afMap.get(id);
571 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
572 .findAnnotation(PDBChain.class.getName().toString()))
574 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
575 af.alignPanel.av.getGlobalColourScheme(), 0);
576 acg.setSeqAssociated(true);
577 af.changeColour(acg);
578 Console.info("Changed colour " + acg.toString());
582 return theseArgsWereParsed;
585 protected boolean processImages(String id)
587 ArgValuesMap avm = argParser.linkedArgs(id);
588 AlignFrame af = afMap.get(id);
592 Console.warn("Did not have an alignment window for id=" + id);
596 if (avm.containsArg(Arg.IMAGE))
598 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
600 String val = av.getValue();
601 SubVals subVal = new SubVals(val);
602 String type = "png"; // default
603 String fileName = subVal.getContent();
604 File file = new File(fileName);
605 if (subVal.has("type"))
607 type = subVal.get("type");
609 else if (fileName != null)
611 for (String ext : new String[] { "svg", "png", "html" })
613 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
619 // for moment we disable JSON export
620 Cache.setPropsAreReadOnly(true);
621 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
626 Console.debug("Outputting type '" + type + "' to " + fileName);
630 Console.debug("Outputting type '" + type + "' to " + fileName);
634 Console.debug("Outputting type '" + type + "' to " + fileName);
635 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
636 htmlSVG.exportHTML(fileName);
639 Console.warn("--image type '" + type + "' 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"));