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");
344 // @Ben assumed this was the GUI version of above, rather then the
345 // first==false state
348 Console.warn("No more files to open");
350 desktop.setProgressBar(null, progress);
356 // load a pAE file if given
357 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
359 AlignFrame af = afMap.get(id);
362 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
364 SubVal subVal = ArgParser.getSubVal(val);
365 File paeFile = new File(subVal.content);
366 String structId = "structid".equals(subVal.keyName)
371 // take structid from pdbfilename
373 if ("structfile".equals(subVal.keyName))
375 EBIAlfaFold.addAlphaFoldPAEToStructure(
376 af.getCurrentView().getAlignment(), paeFile,
377 subVal.index, subVal.keyValue, false);
379 else if ("structid".equals(subVal.keyName))
381 EBIAlfaFold.addAlphaFoldPAEToStructure(
382 af.getCurrentView().getAlignment(), paeFile,
383 subVal.index, subVal.keyValue, true);
387 EBIAlfaFold.addAlphaFoldPAEToSequence(
388 af.getCurrentView().getAlignment(), paeFile,
390 "seqid".equals(subVal.keyName) ? subVal.keyValue
392 // required to readjust the height and position of the pAE
395 for (AlignmentViewPanel ap : af.getAlignPanels())
397 ap.adjustAnnotationHeight();
403 // open the structure (from same PDB file or given PDBfile)
404 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
406 AlignFrame af = afMap.get(id);
407 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
409 for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
411 SubVal subId = new SubVal(val);
412 SequenceI seq = getSpecifiedSequence(af, subId);
415 Console.warn("Could not find sequence for argument --"
416 + Arg.STRUCTURE + "=" + val);
417 // you probably want to continue here, not break
421 File structureFile = null;
422 if (subId.content != null && subId.content.length() != 0)
424 structureFile = new File(subId.content);
425 Console.debug("Using structure file (from argument) '"
426 + structureFile.getAbsolutePath() + "'");
428 /* THIS DOESN'T WORK */
429 else if (seq.getAllPDBEntries() != null
430 && seq.getAllPDBEntries().size() > 0)
432 structureFile = new File(
433 seq.getAllPDBEntries().elementAt(0).getFile());
434 Console.debug("Using structure file (from sequence) '"
435 + structureFile.getAbsolutePath() + "'");
438 if (structureFile == null)
440 Console.warn("Not provided structure file with '" + val + "'");
444 if (!structureFile.exists())
446 Console.warn("Structure file '"
447 + structureFile.getAbsoluteFile() + "' not found.");
451 Console.debug("Using structure file "
452 + structureFile.getAbsolutePath());
454 // open structure view
455 AlignmentPanel ap = af.alignPanel;
456 StructureChooser.openStructureFileForSequence(ap, seq,
461 String shading = ArgParser.getValue(m, Arg.TEMPFAC_SHADING);
465 "Annotation colour scheme : " + shading + " (ignored value)");
466 AlignFrame af = afMap.get(id);
467 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
468 .findAnnotation(PDBChain.class.getName().toString()))
470 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
471 af.alignPanel.av.getGlobalColourScheme(), 0);
472 acg.setSeqAssociated(true);
473 af.changeColour(acg);
474 Console.info("Changed colour " + acg.toString());
480 protected void processImages(String id)
482 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
483 AlignFrame af = afMap.get(id);
487 Console.warn("Did not have an alignment window for id=" + id);
491 if (ArgParser.getValues(m, Arg.IMAGE) != null)
493 for (String val : ArgParser.getValues(m, Arg.IMAGE))
495 SubVal subVal = new SubVal(val);
496 String type = "png"; // default
497 String fileName = subVal.content;
498 File file = new File(fileName);
499 if ("type".equals(subVal.keyName))
501 type = subVal.keyValue;
503 else if (fileName != null)
505 for (String ext : new String[] { "svg", "png", "html" })
507 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
513 // for moment we disable JSON export
514 Cache.setPropsAreReadOnly(true);
515 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
520 Console.debug("Outputting type '" + type + "' to " + fileName);
524 Console.debug("Outputting type '" + type + "' to " + fileName);
528 Console.debug("Outputting type '" + type + "' to " + fileName);
529 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
530 htmlSVG.exportHTML(fileName);
533 Console.warn("--image type '" + type + "' not known. Ignoring");
540 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
542 AlignmentI al = af.getCurrentView().getAlignment();
543 if (-1 < subId.index && subId.index < al.getSequences().size())
545 return al.getSequenceAt(subId.index);
547 else if ("id".equals(subId.keyName))
549 return al.findName(subId.keyValue);