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.bin.argparser.Arg;
18 import jalview.bin.argparser.ArgParser;
19 import jalview.bin.argparser.ArgValue;
20 import jalview.bin.argparser.ArgValues;
21 import jalview.bin.argparser.ArgValuesMap;
22 import jalview.bin.argparser.SubVals;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
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.Desktop;
30 import jalview.gui.Preferences;
31 import jalview.gui.StructureChooser;
32 import jalview.gui.StructureViewer;
33 import jalview.io.AppletFormatAdapter;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormat;
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.TFType;
43 import jalview.structure.StructureSelectionManager;
44 import jalview.util.HttpUtils;
45 import jalview.util.MessageManager;
46 import jalview.util.Platform;
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 String
149 * groovyscript = m.get(Arg.GROOVY) == null ? null :
150 * m.get(Arg.GROOVY).getValue(); String file = m.get(Arg.OPEN) == null ? null :
151 * m.get(Arg.OPEN).getValue(); String data = null; FileFormatI format = null;
152 * DataSourceType protocol = null;
154 if (avm.containsArg(Arg.OPEN) || avm.containsArg(Arg.OPENNEW))
156 commandArgsProvided = true;
159 boolean first = true;
160 boolean progressBarSet = false;
162 // Combine the OPEN and OPENNEW files into one list, along with whether it
163 // was OPEN or OPENNEW
164 List<Entry<Arg, ArgValue>> openAvList = new ArrayList<>();
165 avm.getArgValueList(Arg.OPEN).stream()
166 .forEachOrdered(av -> openAvList.add(
167 new AbstractMap.SimpleEntry<Arg, ArgValue>(Arg.OPEN,
169 avm.getArgValueList(Arg.OPENNEW).stream()
170 .forEachOrdered(av -> openAvList
171 .add(new AbstractMap.SimpleEntry<Arg, ArgValue>(
173 for (Entry<Arg, ArgValue> aav : openAvList)
175 Arg a = aav.getKey();
176 ArgValue av = aav.getValue();
177 String openFile = av.getValue();
178 if (openFile == null)
181 theseArgsWereParsed = true;
185 if (!headless && desktop != null)
187 desktop.setProgressBar(
188 MessageManager.getString(
189 "status.processing_commandline_args"),
190 progress = System.currentTimeMillis());
191 progressBarSet = true;
195 if (!Platform.isJS())
197 * ignore in JavaScript -- can't just file existence - could load it?
202 if (!HttpUtils.startsWithHttpOrHttps(openFile))
204 if (!(new File(openFile)).exists())
206 Console.warn("Can't find file '" + openFile + "'");
211 DataSourceType protocol = AppletFormatAdapter
212 .checkProtocol(openFile);
214 FileFormatI format = null;
217 format = new IdentifyFile().identify(openFile, protocol);
218 } catch (FileFormatException e1)
220 Console.error("Unknown file format for '" + openFile + "'");
224 if (af == null || "true".equals(av.getSubVal("new"))
225 || a == Arg.OPENNEW || format == FileFormat.Jalview)
228 * this approach isn't working yet // get default annotations before opening
229 * AlignFrame if (m.get(Arg.SSANNOTATION) != null) {
230 * Console.debug("##### SSANNOTATION=" + m.get(Arg.SSANNOTATION).getBoolean());
231 * } if (m.get(Arg.NOTEMPFAC) != null) { Console.debug( "##### NOTEMPFAC=" +
232 * m.get(Arg.NOTEMPFAC).getBoolean()); } boolean showSecondaryStructure =
233 * (m.get(Arg.SSANNOTATION) != null) ? m.get(Arg.SSANNOTATION).getBoolean() :
234 * false; boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null) ?
235 * !m.get(Arg.NOTEMPFAC).getBoolean() : false; Console.debug("##### tempfac=" +
236 * showTemperatureFactor + ", showSS=" + showSecondaryStructure);
237 * StructureSelectionManager ssm = StructureSelectionManager
238 * .getStructureSelectionManager(Desktop.instance); if (ssm != null) {
239 * ssm.setAddTempFacAnnot(showTemperatureFactor);
240 * ssm.setProcessSecondaryStructure(showSecondaryStructure); }
244 "Opening '" + openFile + "' in new alignment frame");
245 FileLoader fileLoader = new FileLoader(!headless);
247 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
251 if (avm.getBoolean(Arg.WRAP))
253 af.getCurrentView().setWrapAlignment(true);
257 if (avm.containsArg(Arg.COLOUR))
259 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
262 // change alignment frame title
263 if (avm.containsArg(Arg.TITLE))
264 af.setTitle(avm.getValue(Arg.TITLE));
266 /* hacky approach to hiding the annotations */
267 // show secondary structure annotations?
268 if (avm.getBoolean(Arg.SSANNOTATION))
270 // do this better (annotation types?)
271 AlignmentUtils.showOrHideSequenceAnnotations(
272 af.getCurrentView().getAlignment(),
273 Collections.singleton("Secondary Structure"), null,
277 // show temperature factor annotations?
278 if (avm.getBoolean(Arg.NOTEMPFAC))
280 // do this better (annotation types?)
281 List<String> hideThese = new ArrayList<>();
282 hideThese.add("Temperature Factor");
283 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
284 AlignmentUtils.showOrHideSequenceAnnotations(
285 af.getCurrentView().getAlignment(), hideThese, null,
290 * comment out hacky approach up to here and add this line: if
291 * (showTemperatureFactor)
294 if (avm.containsArg(Arg.TEMPFAC_LABEL))
296 AlignmentAnnotation aa = AlignmentUtils
297 .getFirstSequenceAnnotationOfType(
298 af.getCurrentView().getAlignment(),
299 AlignmentAnnotation.LINE_GRAPH);
300 String label = avm.getValue(Arg.TEMPFAC_LABEL);
308 "Could not find annotation to apply tempfac_label '"
314 // store the AlignFrame for this id
317 // is it its own structure file?
318 if (format.isStructureFile())
320 StructureSelectionManager ssm = StructureSelectionManager
321 .getStructureSelectionManager(Desktop.instance);
322 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
323 ssm.computeMapping(false, new SequenceI[] { seq }, null,
324 openFile, DataSourceType.FILE, null, null, null, false);
330 "Opening '" + openFile + "' in existing alignment frame");
331 af.getCurrentView().addFile(new File(openFile), format, false);
334 Console.debug("Command " + Arg.OPEN + " executed successfully!");
337 if (first) // first=true means nothing opened
341 Jalview.exit("Could not open any files in headless mode", 1);
345 Console.warn("No more files to open");
348 if (progressBarSet && desktop != null)
349 desktop.setProgressBar(null, progress);
353 // open the structure (from same PDB file or given PDBfile)
354 if (!avm.getBoolean(Arg.NOSTRUCTURE))
356 AlignFrame af = afMap.get(id);
357 if (avm.containsArg(Arg.STRUCTURE))
359 commandArgsProvided = true;
360 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
362 String val = av.getValue();
363 SubVals subVals = av.getSubVals();
364 SequenceI seq = getSpecifiedSequence(af, subVals);
367 // Could not find sequence from subId, let's assume the first
368 // sequence in the alignframe
369 AlignmentI al = af.getCurrentView().getAlignment();
370 seq = al.getSequenceAt(0);
375 Console.warn("Could not find sequence for argument "
376 + Arg.STRUCTURE.argString() + "=" + val);
377 // you probably want to continue here, not break
381 File structureFile = null;
382 if (subVals.getContent() != null
383 && subVals.getContent().length() != 0)
385 structureFile = new File(subVals.getContent());
386 Console.debug("Using structure file (from argument) '"
387 + structureFile.getAbsolutePath() + "'");
391 * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
392 * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE,
393 * selectedSequence, true, Desktop.instance);
395 * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new
396 * SequenceI[] { selectedSequence });
399 /* THIS DOESN'T WORK */
400 else if (seq.getAllPDBEntries() != null
401 && seq.getAllPDBEntries().size() > 0)
403 structureFile = new File(
404 seq.getAllPDBEntries().elementAt(0).getFile());
405 Console.debug("Using structure file (from sequence) '"
406 + structureFile.getAbsolutePath() + "'");
409 if (structureFile == null)
411 Console.warn("Not provided structure file with '" + val + "'");
415 if (!structureFile.exists())
417 Console.warn("Structure file '"
418 + structureFile.getAbsoluteFile() + "' not found.");
422 Console.debug("Using structure file "
423 + structureFile.getAbsolutePath());
425 // ##### Does this need to happen? Follow
426 // openStructureFileForSequence() below
428 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
429 .associatePdbWithSeq(structureFile.getAbsolutePath(),
430 DataSourceType.FILE, seq, true, Desktop.instance);
433 // open structure view
434 AlignmentPanel ap = af.alignPanel;
437 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
438 StructureViewer.ViewerType.JMOL.toString());
441 String structureFilepath = structureFile.getAbsolutePath();
443 // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
444 String paeFilepath = subVals.getWithSubstitutions(argParser, id,
446 String paeLabel = subVals.get("paelabel");
447 ArgValue paeAv = getArgAssociatedWithStructure(Arg.PAEMATRIX, avm,
448 af, structureFilepath);
449 if (paeFilepath == null && paeAv != null)
451 SubVals sv = paeAv.getSubVals();
452 File paeFile = new File(sv.getContent());
454 paeLabel = sv.get("label");
457 paeFilepath = paeFile.getCanonicalPath();
458 } catch (IOException e)
460 paeFilepath = paeFile.getAbsolutePath();
461 Console.warn("Problem with the PAE file path: '"
462 + paeFile.getPath() + "'");
466 // get TEMPFAC type from subvals or Arg.TEMPFAC
467 String tftString = subVals.get("tempfac");
468 TFType tft = avm.getBoolean(Arg.NOTEMPFAC) ? null
470 ArgValue tftAv = getArgAssociatedWithStructure(Arg.TEMPFAC, avm,
471 af, structureFilepath);
472 if (tftString == null && tftAv != null)
474 tftString = tftAv.getSubVals().getContent();
476 if (tftString != null)
478 // get kind of temperature factor annotation
481 tft = TFType.valueOf(tftString.toUpperCase(Locale.ROOT));
482 Console.debug("Obtained Temperature Factor type of '" + tft
483 + "' for structure '" + structureFilepath + "'");
484 } catch (IllegalArgumentException e)
486 // Just an error message!
487 StringBuilder sb = new StringBuilder().append("Cannot set ")
488 .append(Arg.TEMPFAC.argString()).append(" to '")
490 .append("', ignoring. Valid values are: ");
491 Iterator<TFType> it = Arrays.stream(TFType.values())
495 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
499 Console.warn(sb.toString());
503 // TODO pass PAE label
504 StructureChooser.openStructureFileForSequence(null, null, ap, seq,
505 false, structureFilepath, tft, paeFilepath, false);
510 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
513 AlignFrame af = afMap.get(id);
514 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
515 .findAnnotation(PDBChain.class.getName().toString()))
517 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
518 af.alignPanel.av.getGlobalColourScheme(), 0);
519 acg.setSeqAssociated(true);
520 af.changeColour(acg);
521 Console.info("Changed colour " + acg.toString());
525 return theseArgsWereParsed;
528 protected boolean processImages(String id)
530 ArgValuesMap avm = argParser.linkedArgs(id);
531 AlignFrame af = afMap.get(id);
535 Console.warn("Did not have an alignment window for id=" + id);
539 if (avm.containsArg(Arg.IMAGE))
541 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
543 String val = av.getValue();
544 SubVals subVal = av.getSubVals();
545 String type = "png"; // default
546 String fileName = subVal.getContent();
547 File file = new File(fileName);
548 if (subVal.has("type"))
550 type = subVal.get("type");
552 else if (fileName != null)
554 for (String ext : new String[] { "svg", "png", "html" })
556 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
562 // for moment we disable JSON export
563 Cache.setPropsAreReadOnly(true);
564 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
569 Console.debug("Outputting type '" + type + "' to " + fileName);
573 Console.debug("Outputting type '" + type + "' to " + fileName);
577 Console.debug("Outputting type '" + type + "' to " + fileName);
578 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
579 htmlSVG.exportHTML(fileName);
582 Console.warn(Arg.IMAGE.argString() + " type '" + type
583 + "' not known. Ignoring");
591 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
595 AlignmentI al = af.getCurrentView().getAlignment();
596 if (subId.has("seqid"))
598 return al.findName(subId.get("seqid"));
600 else if (-1 < subId.getIndex()
601 && subId.getIndex() < al.getSequences().size())
603 return al.getSequenceAt(subId.getIndex());
608 // returns the first Arg value intended for the structure structFilename
609 // (in the given AlignFrame from the ArgValuesMap)
610 private ArgValue getArgAssociatedWithStructure(Arg arg, ArgValuesMap avm,
611 AlignFrame af, String structFilename)
615 for (ArgValue av : avm.getArgValueList(arg))
617 SubVals subVals = av.getSubVals();
618 String structid = subVals.get("structid");
619 String structfile = subVals.get("structfile");
621 // let's find a structure
622 if (structfile == null && structid == null)
624 ArgValue likelyStructure = avm.getClosestPreviousArgValueOfArg(av,
626 if (likelyStructure != null)
628 SubVals sv = likelyStructure.getSubVals();
629 if (sv != null && sv.has(ArgValues.ID))
631 structid = sv.get(ArgValues.ID);
635 structfile = likelyStructure.getValue();
637 "##### Comparing closest previous structure argument '"
643 if (structfile == null && structid != null)
645 StructureSelectionManager ssm = StructureSelectionManager
646 .getStructureSelectionManager(Desktop.instance);
649 structfile = ssm.findFileForPDBId(structid);
652 if (structfile != null && structfile.equals(structFilename))