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.structure.StructureImportSettings;
34 import jalview.structure.StructureImportSettings.TFType;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.HttpUtils;
37 import jalview.util.MessageManager;
38 import jalview.util.Platform;
39 import jalview.ws.dbsources.EBIAlfaFold;
45 private static boolean headless;
47 private static ArgParser argParser;
49 private Map<String, AlignFrame> afMap;
51 public static boolean processArgs(ArgParser ap, boolean h)
55 boolean argsWereParsed = false;
58 System.setProperty("java.awt.headless", "true");
61 if (argParser != null && argParser.linkedIds() != null)
63 for (String id : argParser.linkedIds())
65 Console.debug("##### id=" + id);
66 Commands cmds = new Commands();
69 cmds.processUnlinked(id);
73 cmds.processLinked(id);
75 cmds.processImages(id);
76 argsWereParsed |= cmds.wereParsed();
80 if (argParser.getBool(Arg.QUIT))
82 Jalview.getInstance().quit();
85 // carry on with jalview.bin.Jalview
86 return argsWereParsed;
89 boolean argsWereParsed = false;
91 private boolean wereParsed()
93 return argsWereParsed;
98 this(Desktop.instance);
101 public Commands(Desktop d)
104 afMap = new HashMap<String, AlignFrame>();
107 protected void processUnlinked(String id)
109 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
114 protected void processLinked(String id)
116 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
119 // script to execute after all loading is completed one way or another
120 String groovyscript = m.get(Arg.GROOVY) == null ? null
121 : m.get(Arg.GROOVY).getValue();
122 String file = m.get(Arg.OPEN) == null ? null
123 : m.get(Arg.OPEN).getValue();
125 FileFormatI format = null;
126 DataSourceType protocol = null;
128 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
132 boolean first = true;
134 for (String openFile : ArgParser.getValues(m, Arg.OPEN))
136 if (openFile == null)
139 argsWereParsed = true;
145 desktop.setProgressBar(
146 MessageManager.getString(
147 "status.processing_commandline_args"),
148 progress = System.currentTimeMillis());
152 if (!Platform.isJS())
154 * ignore in JavaScript -- can't just file existence - could load it?
159 if (!HttpUtils.startsWithHttpOrHttps(openFile))
161 if (!(new File(openFile)).exists())
163 Console.warn("Can't find file '" + openFile + "'");
168 DataSourceType protocol = AppletFormatAdapter
169 .checkProtocol(openFile);
171 FileFormatI format = null;
174 format = new IdentifyFile().identify(openFile, protocol);
175 } catch (FileFormatException e1)
177 Console.error("Unknown file format for '" + openFile + "'");
184 * this approach isn't working yet
185 // get default annotations before opening AlignFrame
186 if (m.get(Arg.SSANNOTATION) != null)
188 Console.debug("***** SSANNOTATION="
189 + m.get(Arg.SSANNOTATION).getBoolean());
191 if (m.get(Arg.NOTEMPFAC) != null)
194 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
196 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
197 ? m.get(Arg.SSANNOTATION).getBoolean()
199 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
200 ? !m.get(Arg.NOTEMPFAC).getBoolean()
202 Console.debug("***** tempfac=" + showTemperatureFactor
203 + ", showSS=" + showSecondaryStructure);
204 StructureSelectionManager ssm = StructureSelectionManager
205 .getStructureSelectionManager(Desktop.instance);
208 ssm.setAddTempFacAnnot(showTemperatureFactor);
209 ssm.setProcessSecondaryStructure(showSecondaryStructure);
213 // get kind of temperature factor annotation
214 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
215 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
216 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
220 tempfacType = StructureImportSettings.TFType.valueOf(ArgParser
221 .getValue(m, Arg.TEMPFAC).toUpperCase(Locale.ROOT));
222 Console.debug("Obtained Temperature Factor type of '"
223 + tempfacType + "'");
224 } catch (IllegalArgumentException e)
226 // Just an error message!
227 StringBuilder sb = new StringBuilder().append("Cannot set --")
228 .append(Arg.TEMPFAC.getName()).append(" to '")
230 .append("', ignoring. Valid values are: ");
231 Iterator<StructureImportSettings.TFType> it = Arrays
232 .stream(StructureImportSettings.TFType.values())
236 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
240 Console.warn(sb.toString());
245 "Opening '" + openFile + "' in new alignment frame");
246 FileLoader fileLoader = new FileLoader(!headless);
248 StructureImportSettings.setTemperatureFactorType(tempfacType);
250 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
254 if (ArgParser.getBoolean(m, Arg.WRAP))
256 af.getCurrentView().setWrapAlignment(true);
259 // change alignment frame title
260 if (ArgParser.getValue(m, Arg.TITLE) != null)
261 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
263 /* hacky approach to hiding the annotations */
264 // show secondary structure annotations?
265 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
267 // do this better (annotation types?)
268 AlignmentUtils.showOrHideSequenceAnnotations(
269 af.getCurrentView().getAlignment(),
270 Collections.singleton("Secondary Structure"), null,
274 // show temperature factor annotations?
275 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
277 // do this better (annotation types?)
278 List<String> hideThese = new ArrayList<>();
279 hideThese.add("Temperature Factor");
280 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
281 AlignmentUtils.showOrHideSequenceAnnotations(
282 af.getCurrentView().getAlignment(), hideThese, null,
286 /* comment out hacky approach up to here and add this line:
287 if (showTemperatureFactor)
290 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
292 AlignmentAnnotation aa = AlignmentUtils
293 .getFirstSequenceAnnotationOfType(
294 af.getCurrentView().getAlignment(),
295 AlignmentAnnotation.LINE_GRAPH);
296 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
304 "Could not find annotation to apply tempfac_label '"
310 // store the AlignFrame for this id
313 // is it its own structure file?
314 if (format.isStructureFile())
316 StructureSelectionManager ssm = StructureSelectionManager
317 .getStructureSelectionManager(Desktop.instance);
318 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
319 ssm.computeMapping(false, new SequenceI[] { seq }, null,
320 openFile, DataSourceType.FILE, null);
326 "Opening '" + openFile + "' in existing alignment frame");
327 af.getCurrentView().addFile(new File(openFile), format);
331 .println("Command " + Arg.OPEN + " executed successfully!");
334 if (first) // first=true means nothing opened
338 Console.error("Could not open any files in headless mode");
342 // @Ben assumed this was the GUI version of above, rather then the
343 // first==false state
346 Console.warn("No more files to open");
348 desktop.setProgressBar(null, progress);
354 // load a pAE file if given
355 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
357 AlignFrame af = afMap.get(id);
360 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
362 SubVal subVal = ArgParser.getSubVal(val);
363 File paeFile = new File(subVal.content);
364 String structId = "structid".equals(subVal.keyName)
369 // take structid from pdbfilename
371 if ("structfile".equals(subVal.keyName))
373 EBIAlfaFold.addAlphaFoldPAEToStructure(
374 af.getCurrentView().getAlignment(), paeFile,
375 subVal.index, subVal.keyValue, false);
377 else if ("structid".equals(subVal.keyName))
379 EBIAlfaFold.addAlphaFoldPAEToStructure(
380 af.getCurrentView().getAlignment(), paeFile,
381 subVal.index, subVal.keyValue, true);
385 EBIAlfaFold.addAlphaFoldPAEToSequence(
386 af.getCurrentView().getAlignment(), paeFile,
388 "seqid".equals(subVal.keyName) ? subVal.keyValue
390 // required to readjust the height and position of the pAE
393 for (AlignmentViewPanel ap : af.getAlignPanels())
395 ap.adjustAnnotationHeight();
401 // open the structure (from same PDB file or given PDBfile)
402 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
404 AlignFrame af = afMap.get(id);
405 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
407 for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
409 SubVal subId = new SubVal(val);
410 SequenceI seq = getSpecifiedSequence(af, subId);
413 Console.warn("Could not find sequence for argument --"
414 + Arg.STRUCTURE + "=" + val);
415 // you probably want to continue here, not break
419 File structureFile = null;
420 if (subId.content != null && subId.content.length() != 0)
422 structureFile = new File(subId.content);
423 Console.debug("Using structure file (from argument) '"
424 + structureFile.getAbsolutePath() + "'");
426 /* THIS DOESN'T WORK */
427 else if (seq.getAllPDBEntries() != null
428 && seq.getAllPDBEntries().size() > 0)
430 structureFile = new File(
431 seq.getAllPDBEntries().elementAt(0).getFile());
432 Console.debug("Using structure file (from sequence) '"
433 + structureFile.getAbsolutePath() + "'");
436 if (structureFile == null)
438 Console.warn("Not provided structure file with '" + val + "'");
442 if (!structureFile.exists())
444 Console.warn("Structure file '"
445 + structureFile.getAbsoluteFile() + "' not found.");
449 Console.debug("Using structure file "
450 + structureFile.getAbsolutePath());
452 // open structure view
453 AlignmentPanel ap = af.alignPanel;
454 StructureChooser.openStructureFileForSequence(ap, seq,
461 protected void processImages(String id)
463 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
464 AlignFrame af = afMap.get(id);
468 Console.warn("Did not have an alignment window for id=" + id);
472 if (ArgParser.getValues(m, Arg.IMAGE) != null)
474 for (String val : ArgParser.getValues(m, Arg.IMAGE))
476 SubVal subVal = new SubVal(val);
477 String type = "png"; // default
478 String fileName = subVal.content;
479 File file = new File(fileName);
480 if ("type".equals(subVal.keyName))
482 type = subVal.keyValue;
484 else if (fileName != null)
486 for (String ext : new String[] { "svg", "png", "html" })
488 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
494 // for moment we disable JSON export
495 Cache.setPropsAreReadOnly(true);
496 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
501 Console.debug("Outputting type '" + type + "' to " + fileName);
505 Console.debug("Outputting type '" + type + "' to " + fileName);
509 Console.debug("Outputting type '" + type + "' to " + fileName);
510 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
511 htmlSVG.exportHTML(fileName);
514 Console.warn("--image type '" + type + "' not known. Ignoring");
521 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
523 AlignmentI al = af.getCurrentView().getAlignment();
524 if (-1 < subId.index && subId.index < al.getSequences().size())
526 return al.getSequenceAt(subId.index);
528 else if ("id".equals(subId.keyName))
530 return al.findName(subId.keyValue);