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.ArgValues;
18 import jalview.bin.ArgParser.SubVal;
19 import jalview.datamodel.AlignmentAnnotation;
20 import jalview.datamodel.AlignmentI;
21 import jalview.datamodel.SequenceI;
22 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
23 import jalview.gui.AlignFrame;
24 import jalview.gui.AlignmentPanel;
25 import jalview.gui.Desktop;
26 import jalview.gui.StructureChooser;
27 import jalview.io.AppletFormatAdapter;
28 import jalview.io.DataSourceType;
29 import jalview.io.FileFormatException;
30 import jalview.io.FileFormatI;
31 import jalview.io.FileLoader;
32 import jalview.io.HtmlSvgOutput;
33 import jalview.io.IdentifyFile;
34 import jalview.schemes.AnnotationColourGradient;
35 import jalview.structure.StructureImportSettings;
36 import jalview.structure.StructureImportSettings.TFType;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.util.HttpUtils;
39 import jalview.util.MessageManager;
40 import jalview.util.Platform;
41 import jalview.ws.dbsources.EBIAlfaFold;
42 import mc_view.PDBChain;
48 private static boolean headless;
50 private static ArgParser argParser;
52 private Map<String, AlignFrame> afMap;
54 public static boolean processArgs(ArgParser ap, boolean h)
58 boolean argsWereParsed = false;
61 System.setProperty("java.awt.headless", "true");
64 if (argParser != null && argParser.linkedIds() != null)
66 for (String id : argParser.linkedIds())
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);
262 if (ArgParser.hasValue(m, Arg.COLOUR))
264 af.changeColour_actionPerformed(
265 ArgParser.getValue(m, Arg.COLOUR));
268 // change alignment frame title
269 if (ArgParser.getValue(m, Arg.TITLE) != null)
270 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
272 /* hacky approach to hiding the annotations */
273 // show secondary structure annotations?
274 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
276 // do this better (annotation types?)
277 AlignmentUtils.showOrHideSequenceAnnotations(
278 af.getCurrentView().getAlignment(),
279 Collections.singleton("Secondary Structure"), null,
283 // show temperature factor annotations?
284 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
286 // do this better (annotation types?)
287 List<String> hideThese = new ArrayList<>();
288 hideThese.add("Temperature Factor");
289 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
290 AlignmentUtils.showOrHideSequenceAnnotations(
291 af.getCurrentView().getAlignment(), hideThese, null,
295 /* comment out hacky approach up to here and add this line:
296 if (showTemperatureFactor)
299 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
301 AlignmentAnnotation aa = AlignmentUtils
302 .getFirstSequenceAnnotationOfType(
303 af.getCurrentView().getAlignment(),
304 AlignmentAnnotation.LINE_GRAPH);
305 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
313 "Could not find annotation to apply tempfac_label '"
319 // store the AlignFrame for this id
322 // is it its own structure file?
323 if (format.isStructureFile())
325 StructureSelectionManager ssm = StructureSelectionManager
326 .getStructureSelectionManager(Desktop.instance);
327 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
328 ssm.computeMapping(false, new SequenceI[] { seq }, null,
329 openFile, DataSourceType.FILE, null);
335 "Opening '" + openFile + "' in existing alignment frame");
336 af.getCurrentView().addFile(new File(openFile), format);
339 Console.debug("Command " + Arg.OPEN + " executed successfully!");
342 if (first) // first=true means nothing opened
346 Console.error("Could not open any files in headless mode");
351 Console.warn("No more files to open");
353 desktop.setProgressBar(null, progress);
359 // open the structure (from same PDB file or given PDBfile)
360 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
362 AlignFrame af = afMap.get(id);
363 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
365 for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
367 SubVal subId = new SubVal(val);
368 SequenceI seq = getSpecifiedSequence(af, subId);
371 Console.warn("Could not find sequence for argument --"
372 + Arg.STRUCTURE + "=" + val);
373 // you probably want to continue here, not break
377 File structureFile = null;
378 if (subId.content != null && subId.content.length() != 0)
380 structureFile = new File(subId.content);
381 Console.debug("Using structure file (from argument) '"
382 + structureFile.getAbsolutePath() + "'");
384 /* THIS DOESN'T WORK */
385 else if (seq.getAllPDBEntries() != null
386 && seq.getAllPDBEntries().size() > 0)
388 structureFile = new File(
389 seq.getAllPDBEntries().elementAt(0).getFile());
390 Console.debug("Using structure file (from sequence) '"
391 + structureFile.getAbsolutePath() + "'");
394 if (structureFile == null)
396 Console.warn("Not provided structure file with '" + val + "'");
400 if (!structureFile.exists())
402 Console.warn("Structure file '"
403 + structureFile.getAbsoluteFile() + "' not found.");
407 Console.debug("Using structure file "
408 + structureFile.getAbsolutePath());
410 // open structure view
411 AlignmentPanel ap = af.alignPanel;
412 StructureChooser.openStructureFileForSequence(ap, seq,
418 // load a pAE file if given
419 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
421 AlignFrame af = afMap.get(id);
424 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
426 SubVal subVal = ArgParser.getSubVal(val);
427 File paeFile = new File(subVal.content);
428 String paePath = null;
431 paePath = paeFile.getCanonicalPath();
432 } catch (IOException e)
434 paePath = paeFile.getAbsolutePath();
436 "Problem with the PAE file path: '" + paePath + "'");
438 String structId = "structid".equals(subVal.keyName)
443 // take structid from pdbfilename
445 if ("structfile".equals(subVal.keyName))
447 Console.info("***** Attaching paeFile '" + paePath + "' to "
448 + subVal.keyName + "=" + subVal.keyValue);
449 EBIAlfaFold.addAlphaFoldPAEToStructure(
450 af.getCurrentView().getAlignment(), paeFile,
451 subVal.index, subVal.keyValue, false);
453 else if ("structid".equals(subVal.keyName))
455 Console.info("***** Attaching paeFile '" + paePath + "' to "
456 + subVal.keyName + "=" + subVal.keyValue);
457 EBIAlfaFold.addAlphaFoldPAEToStructure(
458 af.getCurrentView().getAlignment(), paeFile,
459 subVal.index, subVal.keyValue, true);
463 Console.debug("***** Attaching paeFile '" + paePath
464 + "' to sequence index " + subVal.index);
465 EBIAlfaFold.addAlphaFoldPAEToSequence(
466 af.getCurrentView().getAlignment(), paeFile,
468 // required to readjust the height and position of the pAE
471 for (AlignmentViewPanel ap : af.getAlignPanels())
473 ap.adjustAnnotationHeight();
479 boolean doShading = ArgParser.getBoolean(m, Arg.TEMPFAC_SHADING);
482 AlignFrame af = afMap.get(id);
483 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
484 .findAnnotation(PDBChain.class.getName().toString()))
486 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
487 af.alignPanel.av.getGlobalColourScheme(), 0);
488 acg.setSeqAssociated(true);
489 af.changeColour(acg);
490 Console.info("Changed colour " + acg.toString());
495 protected void processImages(String id)
497 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
498 AlignFrame af = afMap.get(id);
502 Console.warn("Did not have an alignment window for id=" + id);
506 if (ArgParser.getValues(m, Arg.IMAGE) != null)
508 for (String val : ArgParser.getValues(m, Arg.IMAGE))
510 SubVal subVal = new SubVal(val);
511 String type = "png"; // default
512 String fileName = subVal.content;
513 File file = new File(fileName);
514 if ("type".equals(subVal.keyName))
516 type = subVal.keyValue;
518 else if (fileName != null)
520 for (String ext : new String[] { "svg", "png", "html" })
522 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
528 // for moment we disable JSON export
529 Cache.setPropsAreReadOnly(true);
530 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
535 Console.debug("Outputting type '" + type + "' to " + fileName);
539 Console.debug("Outputting type '" + type + "' to " + fileName);
543 Console.debug("Outputting type '" + type + "' to " + fileName);
544 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
545 htmlSVG.exportHTML(fileName);
548 Console.warn("--image type '" + type + "' not known. Ignoring");
555 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
557 AlignmentI al = af.getCurrentView().getAlignment();
558 if (-1 < subId.index && subId.index < al.getSequences().size())
560 return al.getSequenceAt(subId.index);
562 else if ("id".equals(subId.keyName))
564 return al.findName(subId.keyValue);