4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import java.util.Locale;
13 import jalview.analysis.AlignmentUtils;
14 import jalview.api.AlignmentViewPanel;
15 import jalview.bin.ArgParser.Arg;
16 import jalview.bin.ArgParser.ArgValues;
17 import jalview.bin.ArgParser.SubVal;
18 import jalview.datamodel.AlignmentAnnotation;
19 import jalview.datamodel.AlignmentI;
20 import jalview.datamodel.SequenceI;
21 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
22 import jalview.gui.AlignFrame;
23 import jalview.gui.AlignmentPanel;
24 import jalview.gui.Desktop;
25 import jalview.gui.StructureChooser;
26 import jalview.io.AppletFormatAdapter;
27 import jalview.io.DataSourceType;
28 import jalview.io.FileFormatException;
29 import jalview.io.FileFormatI;
30 import jalview.io.FileLoader;
31 import jalview.io.HtmlSvgOutput;
32 import jalview.io.IdentifyFile;
33 import jalview.schemes.AnnotationColourGradient;
34 import jalview.structure.StructureImportSettings;
35 import jalview.structure.StructureImportSettings.TFType;
36 import jalview.structure.StructureSelectionManager;
37 import jalview.util.HttpUtils;
38 import jalview.util.MessageManager;
39 import jalview.util.Platform;
40 import jalview.ws.dbsources.EBIAlfaFold;
41 import mc_view.PDBChain;
47 private static boolean headless;
49 private static ArgParser argParser;
51 private Map<String, AlignFrame> afMap;
53 public static boolean processArgs(ArgParser ap, boolean h)
57 boolean argsWereParsed = false;
60 System.setProperty("java.awt.headless", "true");
63 if (argParser != null && argParser.linkedIds() != null)
65 for (String id : argParser.linkedIds())
67 Console.debug("##### id=" + id);
68 Commands cmds = new Commands();
71 cmds.processUnlinked(id);
75 cmds.processLinked(id);
77 cmds.processImages(id);
78 argsWereParsed |= cmds.wereParsed();
82 if (argParser.getBool(Arg.QUIT))
84 Jalview.getInstance().quit();
87 // carry on with jalview.bin.Jalview
88 return argsWereParsed;
91 boolean argsWereParsed = false;
93 private boolean wereParsed()
95 return argsWereParsed;
100 this(Desktop.instance);
103 public Commands(Desktop d)
106 afMap = new HashMap<String, AlignFrame>();
109 protected void processUnlinked(String id)
111 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
116 protected void processLinked(String id)
118 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
121 // script to execute after all loading is completed one way or another
122 String groovyscript = m.get(Arg.GROOVY) == null ? null
123 : m.get(Arg.GROOVY).getValue();
124 String file = m.get(Arg.OPEN) == null ? null
125 : m.get(Arg.OPEN).getValue();
127 FileFormatI format = null;
128 DataSourceType protocol = null;
130 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
134 boolean first = true;
136 for (String openFile : ArgParser.getValues(m, Arg.OPEN))
138 if (openFile == null)
141 argsWereParsed = true;
147 desktop.setProgressBar(
148 MessageManager.getString(
149 "status.processing_commandline_args"),
150 progress = System.currentTimeMillis());
154 if (!Platform.isJS())
156 * ignore in JavaScript -- can't just file existence - could load it?
161 if (!HttpUtils.startsWithHttpOrHttps(openFile))
163 if (!(new File(openFile)).exists())
165 Console.warn("Can't find file '" + openFile + "'");
170 DataSourceType protocol = AppletFormatAdapter
171 .checkProtocol(openFile);
173 FileFormatI format = null;
176 format = new IdentifyFile().identify(openFile, protocol);
177 } catch (FileFormatException e1)
179 Console.error("Unknown file format for '" + openFile + "'");
186 * this approach isn't working yet
187 // get default annotations before opening AlignFrame
188 if (m.get(Arg.SSANNOTATION) != null)
190 Console.debug("***** SSANNOTATION="
191 + m.get(Arg.SSANNOTATION).getBoolean());
193 if (m.get(Arg.NOTEMPFAC) != null)
196 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
198 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
199 ? m.get(Arg.SSANNOTATION).getBoolean()
201 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
202 ? !m.get(Arg.NOTEMPFAC).getBoolean()
204 Console.debug("***** tempfac=" + showTemperatureFactor
205 + ", showSS=" + showSecondaryStructure);
206 StructureSelectionManager ssm = StructureSelectionManager
207 .getStructureSelectionManager(Desktop.instance);
210 ssm.setAddTempFacAnnot(showTemperatureFactor);
211 ssm.setProcessSecondaryStructure(showSecondaryStructure);
215 // get kind of temperature factor annotation
216 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
217 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
218 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
222 tempfacType = StructureImportSettings.TFType.valueOf(ArgParser
223 .getValue(m, Arg.TEMPFAC).toUpperCase(Locale.ROOT));
224 Console.debug("Obtained Temperature Factor type of '"
225 + tempfacType + "'");
226 } catch (IllegalArgumentException e)
228 // Just an error message!
229 StringBuilder sb = new StringBuilder().append("Cannot set --")
230 .append(Arg.TEMPFAC.getName()).append(" to '")
232 .append("', ignoring. Valid values are: ");
233 Iterator<StructureImportSettings.TFType> it = Arrays
234 .stream(StructureImportSettings.TFType.values())
238 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
242 Console.warn(sb.toString());
247 "Opening '" + openFile + "' in new alignment frame");
248 FileLoader fileLoader = new FileLoader(!headless);
250 StructureImportSettings.setTemperatureFactorType(tempfacType);
252 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
256 if (ArgParser.getBoolean(m, Arg.WRAP))
258 af.getCurrentView().setWrapAlignment(true);
261 // change alignment frame title
262 if (ArgParser.getValue(m, Arg.TITLE) != null)
263 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
265 /* hacky approach to hiding the annotations */
266 // show secondary structure annotations?
267 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
269 // do this better (annotation types?)
270 AlignmentUtils.showOrHideSequenceAnnotations(
271 af.getCurrentView().getAlignment(),
272 Collections.singleton("Secondary Structure"), null,
276 // show temperature factor annotations?
277 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
279 // do this better (annotation types?)
280 List<String> hideThese = new ArrayList<>();
281 hideThese.add("Temperature Factor");
282 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
283 AlignmentUtils.showOrHideSequenceAnnotations(
284 af.getCurrentView().getAlignment(), hideThese, null,
288 /* comment out hacky approach up to here and add this line:
289 if (showTemperatureFactor)
292 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
294 AlignmentAnnotation aa = AlignmentUtils
295 .getFirstSequenceAnnotationOfType(
296 af.getCurrentView().getAlignment(),
297 AlignmentAnnotation.LINE_GRAPH);
298 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
306 "Could not find annotation to apply tempfac_label '"
312 // store the AlignFrame for this id
315 // is it its own structure file?
316 if (format.isStructureFile())
318 StructureSelectionManager ssm = StructureSelectionManager
319 .getStructureSelectionManager(Desktop.instance);
320 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
321 ssm.computeMapping(false, new SequenceI[] { seq }, null,
322 openFile, DataSourceType.FILE, null);
328 "Opening '" + openFile + "' in existing alignment frame");
329 af.getCurrentView().addFile(new File(openFile), format);
333 .println("Command " + Arg.OPEN + " executed successfully!");
336 if (first) // first=true means nothing opened
340 Console.error("Could not open any files in headless mode");
345 Console.warn("No more files to open");
347 desktop.setProgressBar(null, progress);
353 // load a pAE file if given
354 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
356 AlignFrame af = afMap.get(id);
359 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
361 SubVal subVal = ArgParser.getSubVal(val);
362 File paeFile = new File(subVal.content);
363 String structId = "structid".equals(subVal.keyName)
368 // take structid from pdbfilename
370 if ("structfile".equals(subVal.keyName))
372 EBIAlfaFold.addAlphaFoldPAEToStructure(
373 af.getCurrentView().getAlignment(), paeFile,
374 subVal.index, subVal.keyValue, false);
376 else if ("structid".equals(subVal.keyName))
378 EBIAlfaFold.addAlphaFoldPAEToStructure(
379 af.getCurrentView().getAlignment(), paeFile,
380 subVal.index, subVal.keyValue, true);
384 EBIAlfaFold.addAlphaFoldPAEToSequence(
385 af.getCurrentView().getAlignment(), paeFile,
387 "seqid".equals(subVal.keyName) ? subVal.keyValue
389 // required to readjust the height and position of the pAE
392 for (AlignmentViewPanel ap : af.getAlignPanels())
394 ap.adjustAnnotationHeight();
400 // open the structure (from same PDB file or given PDBfile)
401 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
403 AlignFrame af = afMap.get(id);
404 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
406 for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
408 SubVal subId = new SubVal(val);
409 SequenceI seq = getSpecifiedSequence(af, subId);
412 Console.warn("Could not find sequence for argument --"
413 + Arg.STRUCTURE + "=" + val);
414 // you probably want to continue here, not break
418 File structureFile = null;
419 if (subId.content != null && subId.content.length() != 0)
421 structureFile = new File(subId.content);
422 Console.debug("Using structure file (from argument) '"
423 + structureFile.getAbsolutePath() + "'");
425 /* THIS DOESN'T WORK */
426 else if (seq.getAllPDBEntries() != null
427 && seq.getAllPDBEntries().size() > 0)
429 structureFile = new File(
430 seq.getAllPDBEntries().elementAt(0).getFile());
431 Console.debug("Using structure file (from sequence) '"
432 + structureFile.getAbsolutePath() + "'");
435 if (structureFile == null)
437 Console.warn("Not provided structure file with '" + val + "'");
441 if (!structureFile.exists())
443 Console.warn("Structure file '"
444 + structureFile.getAbsoluteFile() + "' not found.");
448 Console.debug("Using structure file "
449 + structureFile.getAbsolutePath());
451 // open structure view
452 AlignmentPanel ap = af.alignPanel;
453 StructureChooser.openStructureFileForSequence(ap, seq,
459 boolean doShading = ArgParser.getBoolean(m, Arg.TEMPFAC_SHADING);
462 AlignFrame af = afMap.get(id);
463 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
464 .findAnnotation(PDBChain.class.getName().toString()))
466 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
467 af.alignPanel.av.getGlobalColourScheme(), 0);
468 acg.setSeqAssociated(true);
469 af.changeColour(acg);
470 Console.info("Changed colour " + acg.toString());
475 protected void processImages(String id)
477 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
478 AlignFrame af = afMap.get(id);
482 Console.warn("Did not have an alignment window for id=" + id);
486 if (ArgParser.getValues(m, Arg.IMAGE) != null)
488 for (String val : ArgParser.getValues(m, Arg.IMAGE))
490 SubVal subVal = new SubVal(val);
491 String type = "png"; // default
492 String fileName = subVal.content;
493 File file = new File(fileName);
494 if ("type".equals(subVal.keyName))
496 type = subVal.keyValue;
498 else if (fileName != null)
500 for (String ext : new String[] { "svg", "png", "html" })
502 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
508 // for moment we disable JSON export
509 Cache.setPropsAreReadOnly(true);
510 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
515 Console.debug("Outputting type '" + type + "' to " + fileName);
519 Console.debug("Outputting type '" + type + "' to " + fileName);
523 Console.debug("Outputting type '" + type + "' to " + fileName);
524 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
525 htmlSVG.exportHTML(fileName);
528 Console.warn("--image type '" + type + "' not known. Ignoring");
535 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
537 AlignmentI al = af.getCurrentView().getAlignment();
538 if (-1 < subId.index && subId.index < al.getSequences().size())
540 return al.getSequenceAt(subId.index);
542 else if ("id".equals(subId.keyName))
544 return al.findName(subId.keyValue);