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 static boolean headless;
55 private static ArgParser argParser;
57 private Map<String, AlignFrame> afMap;
59 public static boolean processArgs(ArgParser ap, boolean h)
63 boolean argsWereParsed = false;
66 System.setProperty("java.awt.headless", "true");
69 if (argParser != null && argParser.linkedIds() != null)
71 for (String id : argParser.linkedIds())
73 Commands cmds = new Commands();
76 cmds.processUnlinked(id);
80 cmds.processLinked(id);
82 cmds.processImages(id);
83 argsWereParsed |= cmds.wereParsed();
87 if (argParser.getBool(Arg.QUIT))
89 Jalview.getInstance().quit();
92 // carry on with jalview.bin.Jalview
93 return argsWereParsed;
96 boolean argsWereParsed = false;
98 private boolean wereParsed()
100 return argsWereParsed;
105 this(Desktop.instance);
108 public Commands(Desktop d)
111 afMap = new HashMap<String, AlignFrame>();
114 protected void processUnlinked(String id)
116 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
121 protected void processLinked(String id)
123 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
126 // script to execute after all loading is completed one way or another
127 String groovyscript = m.get(Arg.GROOVY) == null ? null
128 : m.get(Arg.GROOVY).getValue();
129 String file = m.get(Arg.OPEN) == null ? null
130 : m.get(Arg.OPEN).getValue();
132 FileFormatI format = null;
133 DataSourceType protocol = null;
135 if (avm.hasValue(Arg.OPEN))
139 boolean first = true;
141 for (ArgValue av : avm.getArgValueList(Arg.OPEN))
143 String openFile = av.getValue();
144 if (openFile == null)
147 argsWereParsed = true;
151 if (!headless && desktop != null)
153 desktop.setProgressBar(
154 MessageManager.getString(
155 "status.processing_commandline_args"),
156 progress = System.currentTimeMillis());
160 if (!Platform.isJS())
162 * ignore in JavaScript -- can't just file existence - could load it?
167 if (!HttpUtils.startsWithHttpOrHttps(openFile))
169 if (!(new File(openFile)).exists())
171 Console.warn("Can't find file '" + openFile + "'");
176 DataSourceType protocol = AppletFormatAdapter
177 .checkProtocol(openFile);
179 FileFormatI format = null;
182 format = new IdentifyFile().identify(openFile, protocol);
183 } catch (FileFormatException e1)
185 Console.error("Unknown file format for '" + openFile + "'");
192 * this approach isn't working yet
193 // get default annotations before opening AlignFrame
194 if (m.get(Arg.SSANNOTATION) != null)
196 Console.debug("***** SSANNOTATION="
197 + m.get(Arg.SSANNOTATION).getBoolean());
199 if (m.get(Arg.NOTEMPFAC) != null)
202 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
204 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
205 ? m.get(Arg.SSANNOTATION).getBoolean()
207 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
208 ? !m.get(Arg.NOTEMPFAC).getBoolean()
210 Console.debug("***** tempfac=" + showTemperatureFactor
211 + ", showSS=" + showSecondaryStructure);
212 StructureSelectionManager ssm = StructureSelectionManager
213 .getStructureSelectionManager(Desktop.instance);
216 ssm.setAddTempFacAnnot(showTemperatureFactor);
217 ssm.setProcessSecondaryStructure(showSecondaryStructure);
221 // get kind of temperature factor annotation
222 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
223 if ((!avm.getBoolean(Arg.NOTEMPFAC)) && avm.hasValue(Arg.TEMPFAC))
227 tempfacType = StructureImportSettings.TFType
228 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
229 .toUpperCase(Locale.ROOT));
230 Console.debug("Obtained Temperature Factor type of '"
231 + tempfacType + "'");
232 } catch (IllegalArgumentException e)
234 // Just an error message!
235 StringBuilder sb = new StringBuilder().append("Cannot set --")
236 .append(Arg.TEMPFAC.getName()).append(" to '")
238 .append("', ignoring. Valid values are: ");
239 Iterator<StructureImportSettings.TFType> it = Arrays
240 .stream(StructureImportSettings.TFType.values())
244 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
248 Console.warn(sb.toString());
253 "Opening '" + openFile + "' in new alignment frame");
254 FileLoader fileLoader = new FileLoader(!headless);
256 StructureImportSettings.setTemperatureFactorType(tempfacType);
258 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
262 if (avm.getBoolean(Arg.WRAP))
264 af.getCurrentView().setWrapAlignment(true);
268 if (avm.hasValue(Arg.COLOUR))
270 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
273 // change alignment frame title
274 if (avm.hasValue(Arg.TITLE))
275 af.setTitle(avm.getValue(Arg.TITLE));
277 /* hacky approach to hiding the annotations */
278 // show secondary structure annotations?
279 if (avm.getBoolean(Arg.SSANNOTATION))
281 // do this better (annotation types?)
282 AlignmentUtils.showOrHideSequenceAnnotations(
283 af.getCurrentView().getAlignment(),
284 Collections.singleton("Secondary Structure"), null,
288 // show temperature factor annotations?
289 if (avm.getBoolean(Arg.NOTEMPFAC))
291 // do this better (annotation types?)
292 List<String> hideThese = new ArrayList<>();
293 hideThese.add("Temperature Factor");
294 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
295 AlignmentUtils.showOrHideSequenceAnnotations(
296 af.getCurrentView().getAlignment(), hideThese, null,
300 /* comment out hacky approach up to here and add this line:
301 if (showTemperatureFactor)
304 if (avm.hasValue(Arg.TEMPFAC_LABEL))
306 AlignmentAnnotation aa = AlignmentUtils
307 .getFirstSequenceAnnotationOfType(
308 af.getCurrentView().getAlignment(),
309 AlignmentAnnotation.LINE_GRAPH);
310 String label = avm.getValue(Arg.TEMPFAC_LABEL);
318 "Could not find annotation to apply tempfac_label '"
324 // store the AlignFrame for this id
327 // is it its own structure file?
328 if (format.isStructureFile())
330 StructureSelectionManager ssm = StructureSelectionManager
331 .getStructureSelectionManager(Desktop.instance);
332 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
333 ssm.computeMapping(false, new SequenceI[] { seq }, null,
334 openFile, DataSourceType.FILE, null);
340 "Opening '" + openFile + "' in existing alignment frame");
341 af.getCurrentView().addFile(new File(openFile), format);
344 Console.debug("Command " + Arg.OPEN + " executed successfully!");
347 if (first) // first=true means nothing opened
351 Console.error("Could not open any files in headless mode");
356 Console.warn("No more files to open");
358 desktop.setProgressBar(null, progress);
364 // open the structure (from same PDB file or given PDBfile)
365 if (!avm.getBoolean(Arg.NOSTRUCTURE))
367 AlignFrame af = afMap.get(id);
368 if (avm.hasValue(Arg.STRUCTURE))
370 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
372 String val = av.getValue();
373 SubVals subId = new SubVals(val);
374 SequenceI seq = getSpecifiedSequence(af, subId);
377 Console.warn("Could not find sequence for argument --"
378 + Arg.STRUCTURE + "=" + val);
379 // you probably want to continue here, not break
383 File structureFile = null;
384 if (subId.getContent() != null
385 && subId.getContent().length() != 0)
387 structureFile = new File(subId.getContent());
388 Console.debug("Using structure file (from argument) '"
389 + structureFile.getAbsolutePath() + "'");
394 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
395 .associatePdbWithSeq(selectedPdbFileName,
396 DataSourceType.FILE, selectedSequence, true,
399 sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
401 { selectedSequence });
405 /* THIS DOESN'T WORK */
406 else if (seq.getAllPDBEntries() != null
407 && seq.getAllPDBEntries().size() > 0)
409 structureFile = new File(
410 seq.getAllPDBEntries().elementAt(0).getFile());
411 Console.debug("Using structure file (from sequence) '"
412 + structureFile.getAbsolutePath() + "'");
415 if (structureFile == null)
417 Console.warn("Not provided structure file with '" + val + "'");
421 if (!structureFile.exists())
423 Console.warn("Structure file '"
424 + structureFile.getAbsoluteFile() + "' not found.");
428 Console.debug("Using structure file "
429 + structureFile.getAbsolutePath());
431 PDBEntry fileEntry = new AssociatePdbFileWithSeq()
432 .associatePdbWithSeq(structureFile.getAbsolutePath(),
433 DataSourceType.FILE, seq, true, Desktop.instance);
435 // open structure view
436 AlignmentPanel ap = af.alignPanel;
439 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
440 StructureViewer.ViewerType.JMOL.toString());
442 StructureChooser.openStructureFileForSequence(ap, seq,
448 // load a pAE file if given
449 if (avm.hasValue(Arg.PAEMATRIX))
451 AlignFrame af = afMap.get(id);
454 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
456 String val = av.getValue();
457 SubVals subVals = ArgParser.getSubVals(val);
458 File paeFile = new File(subVals.getContent());
459 String paePath = null;
462 paePath = paeFile.getCanonicalPath();
463 } catch (IOException e)
465 paePath = paeFile.getAbsolutePath();
467 "Problem with the PAE file path: '" + paePath + "'");
469 String structId = subVals.get("structid");
470 if (subVals.notSet())
472 // take structid from pdbfilename
474 if (subVals.has("structfile"))
476 Console.info("***** Attaching paeFile '" + paePath + "' to "
477 + "structfile=" + subVals.get("structfile"));
478 EBIAlfaFold.addAlphaFoldPAEToStructure(
479 af.getCurrentView().getAlignment(), paeFile,
480 subVals.getIndex(), subVals.get("structfile"), false);
482 else if (subVals.has("structid"))
484 Console.info("***** Attaching paeFile '" + paePath + "' to "
485 + "structid=" + subVals.get("structid"));
486 EBIAlfaFold.addAlphaFoldPAEToStructure(
487 af.getCurrentView().getAlignment(), paeFile,
488 subVals.getIndex(), subVals.get("structid"), true);
492 Console.debug("***** Attaching paeFile '" + paePath
493 + "' to sequence index " + subVals.getIndex());
494 EBIAlfaFold.addAlphaFoldPAEToSequence(
495 af.getCurrentView().getAlignment(), paeFile,
496 subVals.getIndex(), null);
497 // required to readjust the height and position of the pAE
500 for (AlignmentViewPanel ap : af.getAlignPanels())
502 ap.adjustAnnotationHeight();
508 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
511 AlignFrame af = afMap.get(id);
512 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
513 .findAnnotation(PDBChain.class.getName().toString()))
515 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
516 af.alignPanel.av.getGlobalColourScheme(), 0);
517 acg.setSeqAssociated(true);
518 af.changeColour(acg);
519 Console.info("Changed colour " + acg.toString());
524 protected void processImages(String id)
526 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
527 AlignFrame af = afMap.get(id);
531 Console.warn("Did not have an alignment window for id=" + id);
535 if (avm.hasValue(Arg.IMAGE))
537 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
539 String val = av.getValue();
540 SubVals subVal = new SubVals(val);
541 String type = "png"; // default
542 String fileName = subVal.getContent();
543 File file = new File(fileName);
544 if (subVal.has("type"))
546 type = subVal.get("type");
548 else if (fileName != null)
550 for (String ext : new String[] { "svg", "png", "html" })
552 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
558 // for moment we disable JSON export
559 Cache.setPropsAreReadOnly(true);
560 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
565 Console.debug("Outputting type '" + type + "' to " + fileName);
569 Console.debug("Outputting type '" + type + "' to " + fileName);
573 Console.debug("Outputting type '" + type + "' to " + fileName);
574 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
575 htmlSVG.exportHTML(fileName);
578 Console.warn("--image type '" + type + "' not known. Ignoring");
585 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
587 AlignmentI al = af.getCurrentView().getAlignment();
588 if (-1 < subId.getIndex()
589 && subId.getIndex() < al.getSequences().size())
591 return al.getSequenceAt(subId.getIndex());
593 else if (subId.has("seqid"))
595 return al.findName(subId.get("seqid"));