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.ArgValues;
19 import jalview.bin.ArgParser.SubVals;
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceI;
23 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.AlignmentPanel;
26 import jalview.gui.Desktop;
27 import jalview.gui.StructureChooser;
28 import jalview.io.AppletFormatAdapter;
29 import jalview.io.DataSourceType;
30 import jalview.io.FileFormatException;
31 import jalview.io.FileFormatI;
32 import jalview.io.FileLoader;
33 import jalview.io.HtmlSvgOutput;
34 import jalview.io.IdentifyFile;
35 import jalview.schemes.AnnotationColourGradient;
36 import jalview.structure.StructureImportSettings;
37 import jalview.structure.StructureImportSettings.TFType;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.util.HttpUtils;
40 import jalview.util.MessageManager;
41 import jalview.util.Platform;
42 import jalview.ws.dbsources.EBIAlfaFold;
43 import mc_view.PDBChain;
49 private static boolean headless;
51 private static ArgParser argParser;
53 private Map<String, AlignFrame> afMap;
55 public static boolean processArgs(ArgParser ap, boolean h)
59 boolean argsWereParsed = false;
62 System.setProperty("java.awt.headless", "true");
65 if (argParser != null && argParser.linkedIds() != null)
67 for (String id : argParser.linkedIds())
69 Commands cmds = new Commands();
72 cmds.processUnlinked(id);
76 cmds.processLinked(id);
78 cmds.processImages(id);
79 argsWereParsed |= cmds.wereParsed();
83 if (argParser.getBool(Arg.QUIT))
85 Jalview.getInstance().quit();
88 // carry on with jalview.bin.Jalview
89 return argsWereParsed;
92 boolean argsWereParsed = false;
94 private boolean wereParsed()
96 return argsWereParsed;
101 this(Desktop.instance);
104 public Commands(Desktop d)
107 afMap = new HashMap<String, AlignFrame>();
110 protected void processUnlinked(String id)
112 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
117 protected void processLinked(String id)
119 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
122 // script to execute after all loading is completed one way or another
123 String groovyscript = m.get(Arg.GROOVY) == null ? null
124 : m.get(Arg.GROOVY).getValue();
125 String file = m.get(Arg.OPEN) == null ? null
126 : m.get(Arg.OPEN).getValue();
128 FileFormatI format = null;
129 DataSourceType protocol = null;
131 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
135 boolean first = true;
137 for (ArgValue av : ArgParser.getArgValueList(m, Arg.OPEN))
139 String openFile = av.getValue();
140 if (openFile == null)
143 argsWereParsed = true;
149 desktop.setProgressBar(
150 MessageManager.getString(
151 "status.processing_commandline_args"),
152 progress = System.currentTimeMillis());
156 if (!Platform.isJS())
158 * ignore in JavaScript -- can't just file existence - could load it?
163 if (!HttpUtils.startsWithHttpOrHttps(openFile))
165 if (!(new File(openFile)).exists())
167 Console.warn("Can't find file '" + openFile + "'");
172 DataSourceType protocol = AppletFormatAdapter
173 .checkProtocol(openFile);
175 FileFormatI format = null;
178 format = new IdentifyFile().identify(openFile, protocol);
179 } catch (FileFormatException e1)
181 Console.error("Unknown file format for '" + openFile + "'");
188 * this approach isn't working yet
189 // get default annotations before opening AlignFrame
190 if (m.get(Arg.SSANNOTATION) != null)
192 Console.debug("***** SSANNOTATION="
193 + m.get(Arg.SSANNOTATION).getBoolean());
195 if (m.get(Arg.NOTEMPFAC) != null)
198 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
200 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
201 ? m.get(Arg.SSANNOTATION).getBoolean()
203 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
204 ? !m.get(Arg.NOTEMPFAC).getBoolean()
206 Console.debug("***** tempfac=" + showTemperatureFactor
207 + ", showSS=" + showSecondaryStructure);
208 StructureSelectionManager ssm = StructureSelectionManager
209 .getStructureSelectionManager(Desktop.instance);
212 ssm.setAddTempFacAnnot(showTemperatureFactor);
213 ssm.setProcessSecondaryStructure(showSecondaryStructure);
217 // get kind of temperature factor annotation
218 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
219 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
220 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
224 tempfacType = StructureImportSettings.TFType
225 .valueOf(ArgParser.getArgValue(m, Arg.TEMPFAC)
226 .getValue().toUpperCase(Locale.ROOT));
227 Console.debug("Obtained Temperature Factor type of '"
228 + tempfacType + "'");
229 } catch (IllegalArgumentException e)
231 // Just an error message!
232 StringBuilder sb = new StringBuilder().append("Cannot set --")
233 .append(Arg.TEMPFAC.getName()).append(" to '")
235 .append("', ignoring. Valid values are: ");
236 Iterator<StructureImportSettings.TFType> it = Arrays
237 .stream(StructureImportSettings.TFType.values())
241 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
245 Console.warn(sb.toString());
250 "Opening '" + openFile + "' in new alignment frame");
251 FileLoader fileLoader = new FileLoader(!headless);
253 StructureImportSettings.setTemperatureFactorType(tempfacType);
255 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
259 if (ArgParser.getBoolean(m, Arg.WRAP))
261 af.getCurrentView().setWrapAlignment(true);
265 if (ArgParser.hasValue(m, Arg.COLOUR))
267 af.changeColour_actionPerformed(
268 ArgParser.getValue(m, Arg.COLOUR));
271 // change alignment frame title
272 if (ArgParser.getValue(m, Arg.TITLE) != null)
273 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
275 /* hacky approach to hiding the annotations */
276 // show secondary structure annotations?
277 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
279 // do this better (annotation types?)
280 AlignmentUtils.showOrHideSequenceAnnotations(
281 af.getCurrentView().getAlignment(),
282 Collections.singleton("Secondary Structure"), null,
286 // show temperature factor annotations?
287 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
289 // do this better (annotation types?)
290 List<String> hideThese = new ArrayList<>();
291 hideThese.add("Temperature Factor");
292 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
293 AlignmentUtils.showOrHideSequenceAnnotations(
294 af.getCurrentView().getAlignment(), hideThese, null,
298 /* comment out hacky approach up to here and add this line:
299 if (showTemperatureFactor)
302 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
304 AlignmentAnnotation aa = AlignmentUtils
305 .getFirstSequenceAnnotationOfType(
306 af.getCurrentView().getAlignment(),
307 AlignmentAnnotation.LINE_GRAPH);
308 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
316 "Could not find annotation to apply tempfac_label '"
322 // store the AlignFrame for this id
325 // is it its own structure file?
326 if (format.isStructureFile())
328 StructureSelectionManager ssm = StructureSelectionManager
329 .getStructureSelectionManager(Desktop.instance);
330 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
331 ssm.computeMapping(false, new SequenceI[] { seq }, null,
332 openFile, DataSourceType.FILE, null);
338 "Opening '" + openFile + "' in existing alignment frame");
339 af.getCurrentView().addFile(new File(openFile), format);
342 Console.debug("Command " + Arg.OPEN + " executed successfully!");
345 if (first) // first=true means nothing opened
349 Console.error("Could not open any files in headless mode");
354 Console.warn("No more files to open");
356 desktop.setProgressBar(null, progress);
362 // open the structure (from same PDB file or given PDBfile)
363 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
365 AlignFrame af = afMap.get(id);
366 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
368 for (ArgValue av : ArgParser.getArgValueList(m, Arg.STRUCTURE))
370 String val = av.getValue();
371 SubVals subId = new SubVals(val);
372 SequenceI seq = getSpecifiedSequence(af, subId);
375 Console.warn("Could not find sequence for argument --"
376 + Arg.STRUCTURE + "=" + val);
377 // you probably want to continue here, not break
381 File structureFile = null;
382 if (subId.getContent() != null
383 && subId.getContent().length() != 0)
385 structureFile = new File(subId.getContent());
386 Console.debug("Using structure file (from argument) '"
387 + structureFile.getAbsolutePath() + "'");
389 /* THIS DOESN'T WORK */
390 else if (seq.getAllPDBEntries() != null
391 && seq.getAllPDBEntries().size() > 0)
393 structureFile = new File(
394 seq.getAllPDBEntries().elementAt(0).getFile());
395 Console.debug("Using structure file (from sequence) '"
396 + structureFile.getAbsolutePath() + "'");
399 if (structureFile == null)
401 Console.warn("Not provided structure file with '" + val + "'");
405 if (!structureFile.exists())
407 Console.warn("Structure file '"
408 + structureFile.getAbsoluteFile() + "' not found.");
412 Console.debug("Using structure file "
413 + structureFile.getAbsolutePath());
415 // open structure view
416 AlignmentPanel ap = af.alignPanel;
417 StructureChooser.openStructureFileForSequence(ap, seq,
423 // load a pAE file if given
424 if (ArgParser.getArgValueList(m, Arg.PAEMATRIX) != null)
426 AlignFrame af = afMap.get(id);
429 for (ArgValue av : ArgParser.getArgValueList(m, Arg.PAEMATRIX))
431 String val = av.getValue();
432 SubVals subVals = ArgParser.getSubVals(val);
433 File paeFile = new File(subVals.getContent());
434 String paePath = null;
437 paePath = paeFile.getCanonicalPath();
438 } catch (IOException e)
440 paePath = paeFile.getAbsolutePath();
442 "Problem with the PAE file path: '" + paePath + "'");
444 String structId = subVals.get("structid");
445 if (subVals.notSet())
447 // take structid from pdbfilename
449 if (subVals.has("structfile"))
451 Console.info("***** Attaching paeFile '" + paePath + "' to "
452 + "structfile=" + subVals.get("structfile"));
453 EBIAlfaFold.addAlphaFoldPAEToStructure(
454 af.getCurrentView().getAlignment(), paeFile,
455 subVals.getIndex(), subVals.get("structfile"), false);
457 else if (subVals.has("structid"))
459 Console.info("***** Attaching paeFile '" + paePath + "' to "
460 + "structid=" + subVals.get("structid"));
461 EBIAlfaFold.addAlphaFoldPAEToStructure(
462 af.getCurrentView().getAlignment(), paeFile,
463 subVals.getIndex(), subVals.get("structid"), true);
467 Console.debug("***** Attaching paeFile '" + paePath
468 + "' to sequence index " + subVals.getIndex());
469 EBIAlfaFold.addAlphaFoldPAEToSequence(
470 af.getCurrentView().getAlignment(), paeFile,
471 subVals.getIndex(), null);
472 // required to readjust the height and position of the pAE
475 for (AlignmentViewPanel ap : af.getAlignPanels())
477 ap.adjustAnnotationHeight();
483 boolean doShading = ArgParser.getBoolean(m, Arg.TEMPFAC_SHADING);
486 AlignFrame af = afMap.get(id);
487 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
488 .findAnnotation(PDBChain.class.getName().toString()))
490 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
491 af.alignPanel.av.getGlobalColourScheme(), 0);
492 acg.setSeqAssociated(true);
493 af.changeColour(acg);
494 Console.info("Changed colour " + acg.toString());
499 protected void processImages(String id)
501 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
502 AlignFrame af = afMap.get(id);
506 Console.warn("Did not have an alignment window for id=" + id);
510 if (ArgParser.getArgValueList(m, Arg.IMAGE) != null)
512 for (ArgValue av : ArgParser.getArgValueList(m, Arg.IMAGE))
514 String val = av.getValue();
515 SubVals subVal = new SubVals(val);
516 String type = "png"; // default
517 String fileName = subVal.getContent();
518 File file = new File(fileName);
519 if (subVal.has("type"))
521 type = subVal.get("type");
523 else if (fileName != null)
525 for (String ext : new String[] { "svg", "png", "html" })
527 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
533 // for moment we disable JSON export
534 Cache.setPropsAreReadOnly(true);
535 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
540 Console.debug("Outputting type '" + type + "' to " + fileName);
544 Console.debug("Outputting type '" + type + "' to " + fileName);
548 Console.debug("Outputting type '" + type + "' to " + fileName);
549 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
550 htmlSVG.exportHTML(fileName);
553 Console.warn("--image type '" + type + "' not known. Ignoring");
560 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
562 AlignmentI al = af.getCurrentView().getAlignment();
563 if (-1 < subId.getIndex()
564 && subId.getIndex() < al.getSequences().size())
566 return al.getSequenceAt(subId.getIndex());
568 else if (subId.has("seqid"))
570 return al.findName(subId.get("seqid"));