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 void processArgs(ArgParser ap, boolean h)
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);
79 if (argParser.getBool(Arg.QUIT))
81 Jalview.getInstance().quit();
82 // Desktop.instance.quit();
88 this(Desktop.instance);
91 public Commands(Desktop d)
94 afMap = new HashMap<String, AlignFrame>();
97 protected void processUnlinked(String id)
99 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
104 protected void processLinked(String id)
106 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
109 // script to execute after all loading is completed one way or another
110 String groovyscript = m.get(Arg.GROOVY) == null ? null
111 : m.get(Arg.GROOVY).getValue();
112 String file = m.get(Arg.OPEN) == null ? null
113 : m.get(Arg.OPEN).getValue();
115 FileFormatI format = null;
116 DataSourceType protocol = null;
118 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
122 boolean first = true;
124 for (String openFile : ArgParser.getValues(m, Arg.OPEN))
126 if (openFile == null)
129 argsWereParsed = true;
135 desktop.setProgressBar(
136 MessageManager.getString(
137 "status.processing_commandline_args"),
138 progress = System.currentTimeMillis());
142 if (!Platform.isJS())
144 * ignore in JavaScript -- can't just file existence - could load it?
149 if (!HttpUtils.startsWithHttpOrHttps(openFile))
151 if (!(new File(openFile)).exists())
153 Console.warn("Can't find file '" + openFile + "'");
158 DataSourceType protocol = AppletFormatAdapter
159 .checkProtocol(openFile);
161 FileFormatI format = null;
164 format = new IdentifyFile().identify(openFile, protocol);
165 } catch (FileFormatException e1)
167 Console.error("Unknown file format for '" + openFile + "'");
174 * this approach isn't working yet
175 // get default annotations before opening AlignFrame
176 if (m.get(Arg.SSANNOTATION) != null)
178 Console.debug("***** SSANNOTATION="
179 + m.get(Arg.SSANNOTATION).getBoolean());
181 if (m.get(Arg.NOTEMPFAC) != null)
184 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
186 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
187 ? m.get(Arg.SSANNOTATION).getBoolean()
189 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
190 ? !m.get(Arg.NOTEMPFAC).getBoolean()
192 Console.debug("***** tempfac=" + showTemperatureFactor
193 + ", showSS=" + showSecondaryStructure);
194 StructureSelectionManager ssm = StructureSelectionManager
195 .getStructureSelectionManager(Desktop.instance);
198 ssm.setAddTempFacAnnot(showTemperatureFactor);
199 ssm.setProcessSecondaryStructure(showSecondaryStructure);
203 // get kind of temperature factor annotation
204 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
205 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
206 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
210 tempfacType = StructureImportSettings.TFType.valueOf(ArgParser
211 .getValue(m, Arg.TEMPFAC).toUpperCase(Locale.ROOT));
212 Console.debug("Obtained Temperature Factor type of '"
213 + tempfacType + "'");
214 } catch (IllegalArgumentException e)
216 // Just an error message!
217 StringBuilder sb = new StringBuilder().append("Cannot set --")
218 .append(Arg.TEMPFAC.getName()).append(" to '")
220 .append("', ignoring. Valid values are: ");
221 Iterator<StructureImportSettings.TFType> it = Arrays
222 .stream(StructureImportSettings.TFType.values())
226 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
230 Console.warn(sb.toString());
235 "Opening '" + openFile + "' in new alignment frame");
236 FileLoader fileLoader = new FileLoader(!headless);
238 StructureImportSettings.setTemperatureFactorType(tempfacType);
240 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
244 if (ArgParser.getBoolean(m, Arg.WRAP))
246 af.getCurrentView().setWrapAlignment(true);
249 // change alignment frame title
250 if (ArgParser.getValue(m, Arg.TITLE) != null)
251 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
253 /* hacky approach to hiding the annotations */
254 // show secondary structure annotations?
255 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
257 // do this better (annotation types?)
258 AlignmentUtils.showOrHideSequenceAnnotations(
259 af.getCurrentView().getAlignment(),
260 Collections.singleton("Secondary Structure"), null,
264 // show temperature factor annotations?
265 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
267 // do this better (annotation types?)
268 List<String> hideThese = new ArrayList<>();
269 hideThese.add("Temperature Factor");
270 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
271 AlignmentUtils.showOrHideSequenceAnnotations(
272 af.getCurrentView().getAlignment(), hideThese, null,
276 /* comment out hacky approach up to here and add this line:
277 if (showTemperatureFactor)
280 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
282 AlignmentAnnotation aa = AlignmentUtils
283 .getFirstSequenceAnnotationOfType(
284 af.getCurrentView().getAlignment(),
285 AlignmentAnnotation.LINE_GRAPH);
286 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
294 "Could not find annotation to apply tempfac_label '"
300 // store the AlignFrame for this id
303 // is it its own structure file?
304 if (format.isStructureFile())
306 StructureSelectionManager ssm = StructureSelectionManager
307 .getStructureSelectionManager(Desktop.instance);
308 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
309 ssm.computeMapping(false, new SequenceI[] { seq }, null,
310 openFile, DataSourceType.FILE, null);
316 "Opening '" + openFile + "' in existing alignment frame");
317 af.getCurrentView().addFile(new File(openFile), format);
321 .println("Command " + Arg.OPEN + " executed successfully!");
324 if (first) // first=true means nothing opened
328 Console.error("Could not open any files in headless mode");
334 Console.warn("No more files to open");
336 desktop.setProgressBar(null, progress);
341 // load a pAE file if given
342 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
344 AlignFrame af = afMap.get(id);
347 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
349 SubVal subVal = ArgParser.getSubVal(val);
350 File paeFile = new File(subVal.content);
351 String structId = "structid".equals(subVal.keyName)
356 // take structid from pdbfilename
358 if ("structfile".equals(subVal.keyName))
360 EBIAlfaFold.addAlphaFoldPAEToStructure(
361 af.getCurrentView().getAlignment(), paeFile,
362 subVal.index, subVal.keyValue, false);
364 else if ("structid".equals(subVal.keyName))
366 EBIAlfaFold.addAlphaFoldPAEToStructure(
367 af.getCurrentView().getAlignment(), paeFile,
368 subVal.index, subVal.keyValue, true);
372 EBIAlfaFold.addAlphaFoldPAEToSequence(
373 af.getCurrentView().getAlignment(), paeFile,
375 "seqid".equals(subVal.keyName) ? subVal.keyValue
377 // required to readjust the height and position of the pAE
380 for (AlignmentViewPanel ap : af.getAlignPanels())
382 ap.adjustAnnotationHeight();
388 // open the structure (from same PDB file or given PDBfile)
389 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
391 AlignFrame af = afMap.get(id);
392 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
394 for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
396 SubVal subId = new SubVal(val);
397 SequenceI seq = getSpecifiedSequence(af, subId);
400 Console.warn("Could not find sequence for argument --"
401 + Arg.STRUCTURE + "=" + val);
404 File structureFile = null;
405 if (subId.content != null && subId.content.length() != 0)
407 structureFile = new File(subId.content);
408 Console.debug("Using structure file (from argument) '"
409 + structureFile.getAbsolutePath() + "'");
411 /* THIS DOESN'T WORK */
412 else if (seq.getAllPDBEntries() != null
413 && seq.getAllPDBEntries().size() > 0)
415 structureFile = new File(
416 seq.getAllPDBEntries().elementAt(0).getFile());
417 Console.debug("Using structure file (from sequence) '"
418 + structureFile.getAbsolutePath() + "'");
421 if (structureFile == null)
423 Console.warn("Not provided structure file with '" + val + "'");
427 if (!structureFile.exists())
429 Console.warn("Structure file '"
430 + structureFile.getAbsoluteFile() + "' not found.");
434 Console.debug("Using structure file "
435 + structureFile.getAbsolutePath());
437 // open structure view
438 AlignmentPanel ap = af.alignPanel;
439 StructureChooser.openStructureFileForSequence(ap, seq,
446 protected void processImages(String id)
448 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
449 AlignFrame af = afMap.get(id);
453 Console.warn("Did not have an alignment window for id=" + id);
457 if (ArgParser.getValues(m, Arg.IMAGE) != null)
459 for (String val : ArgParser.getValues(m, Arg.IMAGE))
461 SubVal subVal = new SubVal(val);
462 String type = "png"; // default
463 String fileName = subVal.content;
464 File file = new File(fileName);
465 if ("type".equals(subVal.keyName))
467 type = subVal.keyValue;
469 else if (fileName != null)
471 for (String ext : new String[] { "svg", "png", "html" })
473 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
482 Console.debug("Outputting type '" + type + "' to " + fileName);
486 Console.debug("Outputting type '" + type + "' to " + fileName);
490 Console.debug("Outputting type '" + type + "' to " + fileName);
491 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
492 htmlSVG.exportHTML(fileName);
495 Console.warn("--image type '" + type + "' not known. Ignoring");
502 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
504 AlignmentI al = af.getCurrentView().getAlignment();
505 if (-1 < subId.index && subId.index < al.getSequences().size())
507 return al.getSequenceAt(subId.index);
509 else if ("id".equals(subId.keyName))
511 return al.findName(subId.keyValue);