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.ArgValue;
18 import jalview.bin.ArgParser.ArgValuesMap;
19 import jalview.bin.ArgParser.SubVals;
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceI;
23 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.AlignmentPanel;
26 import jalview.gui.Desktop;
27 import jalview.gui.StructureChooser;
28 import jalview.io.AppletFormatAdapter;
29 import jalview.io.DataSourceType;
30 import jalview.io.FileFormatException;
31 import jalview.io.FileFormatI;
32 import jalview.io.FileLoader;
33 import jalview.io.HtmlSvgOutput;
34 import jalview.io.IdentifyFile;
35 import jalview.schemes.AnnotationColourGradient;
36 import jalview.structure.StructureImportSettings;
37 import jalview.structure.StructureImportSettings.TFType;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.util.HttpUtils;
40 import jalview.util.MessageManager;
41 import jalview.util.Platform;
42 import jalview.ws.dbsources.EBIAlfaFold;
43 import mc_view.PDBChain;
49 private static boolean headless;
51 private static ArgParser argParser;
53 private Map<String, AlignFrame> afMap;
55 public static boolean processArgs(ArgParser ap, boolean h)
59 boolean argsWereParsed = false;
62 System.setProperty("java.awt.headless", "true");
65 if (argParser != null && argParser.linkedIds() != null)
67 for (String id : argParser.linkedIds())
69 Commands cmds = new Commands();
72 cmds.processUnlinked(id);
76 cmds.processLinked(id);
78 cmds.processImages(id);
79 argsWereParsed |= cmds.wereParsed();
83 if (argParser.getBool(Arg.QUIT))
85 Jalview.getInstance().quit();
88 // carry on with jalview.bin.Jalview
89 return argsWereParsed;
92 boolean argsWereParsed = false;
94 private boolean wereParsed()
96 return argsWereParsed;
101 this(Desktop.instance);
104 public Commands(Desktop d)
107 afMap = new HashMap<String, AlignFrame>();
110 protected void processUnlinked(String id)
112 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
117 protected void processLinked(String id)
119 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
122 // script to execute after all loading is completed one way or another
123 String groovyscript = m.get(Arg.GROOVY) == null ? null
124 : m.get(Arg.GROOVY).getValue();
125 String file = m.get(Arg.OPEN) == null ? null
126 : m.get(Arg.OPEN).getValue();
128 FileFormatI format = null;
129 DataSourceType protocol = null;
131 if (avm.hasValue(Arg.OPEN))
135 boolean first = true;
137 for (ArgValue av : avm.getArgValueList(Arg.OPEN))
139 String openFile = av.getValue();
140 if (openFile == null)
143 argsWereParsed = true;
149 desktop.setProgressBar(
150 MessageManager.getString(
151 "status.processing_commandline_args"),
152 progress = System.currentTimeMillis());
156 if (!Platform.isJS())
158 * ignore in JavaScript -- can't just file existence - could load it?
163 if (!HttpUtils.startsWithHttpOrHttps(openFile))
165 if (!(new File(openFile)).exists())
167 Console.warn("Can't find file '" + openFile + "'");
172 DataSourceType protocol = AppletFormatAdapter
173 .checkProtocol(openFile);
175 FileFormatI format = null;
178 format = new IdentifyFile().identify(openFile, protocol);
179 } catch (FileFormatException e1)
181 Console.error("Unknown file format for '" + openFile + "'");
188 * this approach isn't working yet
189 // get default annotations before opening AlignFrame
190 if (m.get(Arg.SSANNOTATION) != null)
192 Console.debug("***** SSANNOTATION="
193 + m.get(Arg.SSANNOTATION).getBoolean());
195 if (m.get(Arg.NOTEMPFAC) != null)
198 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
200 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
201 ? m.get(Arg.SSANNOTATION).getBoolean()
203 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
204 ? !m.get(Arg.NOTEMPFAC).getBoolean()
206 Console.debug("***** tempfac=" + showTemperatureFactor
207 + ", showSS=" + showSecondaryStructure);
208 StructureSelectionManager ssm = StructureSelectionManager
209 .getStructureSelectionManager(Desktop.instance);
212 ssm.setAddTempFacAnnot(showTemperatureFactor);
213 ssm.setProcessSecondaryStructure(showSecondaryStructure);
217 // get kind of temperature factor annotation
218 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
219 if ((!avm.getBoolean(Arg.NOTEMPFAC)) && avm.hasValue(Arg.TEMPFAC))
223 tempfacType = StructureImportSettings.TFType
224 .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
225 .toUpperCase(Locale.ROOT));
226 Console.debug("Obtained Temperature Factor type of '"
227 + tempfacType + "'");
228 } catch (IllegalArgumentException e)
230 // Just an error message!
231 StringBuilder sb = new StringBuilder().append("Cannot set --")
232 .append(Arg.TEMPFAC.getName()).append(" to '")
234 .append("', ignoring. Valid values are: ");
235 Iterator<StructureImportSettings.TFType> it = Arrays
236 .stream(StructureImportSettings.TFType.values())
240 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
244 Console.warn(sb.toString());
249 "Opening '" + openFile + "' in new alignment frame");
250 FileLoader fileLoader = new FileLoader(!headless);
252 StructureImportSettings.setTemperatureFactorType(tempfacType);
254 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
258 if (avm.getBoolean(Arg.WRAP))
260 af.getCurrentView().setWrapAlignment(true);
264 if (avm.hasValue(Arg.COLOUR))
266 af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
269 // change alignment frame title
270 if (avm.hasValue(Arg.TITLE))
271 af.setTitle(avm.getValue(Arg.TITLE));
273 /* hacky approach to hiding the annotations */
274 // show secondary structure annotations?
275 if (avm.getBoolean(Arg.SSANNOTATION))
277 // do this better (annotation types?)
278 AlignmentUtils.showOrHideSequenceAnnotations(
279 af.getCurrentView().getAlignment(),
280 Collections.singleton("Secondary Structure"), null,
284 // show temperature factor annotations?
285 if (avm.getBoolean(Arg.NOTEMPFAC))
287 // do this better (annotation types?)
288 List<String> hideThese = new ArrayList<>();
289 hideThese.add("Temperature Factor");
290 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
291 AlignmentUtils.showOrHideSequenceAnnotations(
292 af.getCurrentView().getAlignment(), hideThese, null,
296 /* comment out hacky approach up to here and add this line:
297 if (showTemperatureFactor)
300 if (avm.hasValue(Arg.TEMPFAC_LABEL))
302 AlignmentAnnotation aa = AlignmentUtils
303 .getFirstSequenceAnnotationOfType(
304 af.getCurrentView().getAlignment(),
305 AlignmentAnnotation.LINE_GRAPH);
306 String label = avm.getValue(Arg.TEMPFAC_LABEL);
314 "Could not find annotation to apply tempfac_label '"
320 // store the AlignFrame for this id
323 // is it its own structure file?
324 if (format.isStructureFile())
326 StructureSelectionManager ssm = StructureSelectionManager
327 .getStructureSelectionManager(Desktop.instance);
328 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
329 ssm.computeMapping(false, new SequenceI[] { seq }, null,
330 openFile, DataSourceType.FILE, null);
336 "Opening '" + openFile + "' in existing alignment frame");
337 af.getCurrentView().addFile(new File(openFile), format);
340 Console.debug("Command " + Arg.OPEN + " executed successfully!");
343 if (first) // first=true means nothing opened
347 Console.error("Could not open any files in headless mode");
352 Console.warn("No more files to open");
354 desktop.setProgressBar(null, progress);
360 // open the structure (from same PDB file or given PDBfile)
361 if (!avm.getBoolean(Arg.NOSTRUCTURE))
363 AlignFrame af = afMap.get(id);
364 if (avm.hasValue(Arg.STRUCTURE))
366 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
368 String val = av.getValue();
369 SubVals subId = new SubVals(val);
370 SequenceI seq = getSpecifiedSequence(af, subId);
373 Console.warn("Could not find sequence for argument --"
374 + Arg.STRUCTURE + "=" + val);
375 // you probably want to continue here, not break
379 File structureFile = null;
380 if (subId.getContent() != null
381 && subId.getContent().length() != 0)
383 structureFile = new File(subId.getContent());
384 Console.debug("Using structure file (from argument) '"
385 + structureFile.getAbsolutePath() + "'");
387 /* THIS DOESN'T WORK */
388 else if (seq.getAllPDBEntries() != null
389 && seq.getAllPDBEntries().size() > 0)
391 structureFile = new File(
392 seq.getAllPDBEntries().elementAt(0).getFile());
393 Console.debug("Using structure file (from sequence) '"
394 + structureFile.getAbsolutePath() + "'");
397 if (structureFile == null)
399 Console.warn("Not provided structure file with '" + val + "'");
403 if (!structureFile.exists())
405 Console.warn("Structure file '"
406 + structureFile.getAbsoluteFile() + "' not found.");
410 Console.debug("Using structure file "
411 + structureFile.getAbsolutePath());
413 // open structure view
414 AlignmentPanel ap = af.alignPanel;
415 StructureChooser.openStructureFileForSequence(ap, seq,
421 // load a pAE file if given
422 if (avm.hasValue(Arg.PAEMATRIX))
424 AlignFrame af = afMap.get(id);
427 for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
429 String val = av.getValue();
430 SubVals subVals = ArgParser.getSubVals(val);
431 File paeFile = new File(subVals.getContent());
432 String paePath = null;
435 paePath = paeFile.getCanonicalPath();
436 } catch (IOException e)
438 paePath = paeFile.getAbsolutePath();
440 "Problem with the PAE file path: '" + paePath + "'");
442 String structId = subVals.get("structid");
443 if (subVals.notSet())
445 // take structid from pdbfilename
447 if (subVals.has("structfile"))
449 Console.info("***** Attaching paeFile '" + paePath + "' to "
450 + "structfile=" + subVals.get("structfile"));
451 EBIAlfaFold.addAlphaFoldPAEToStructure(
452 af.getCurrentView().getAlignment(), paeFile,
453 subVals.getIndex(), subVals.get("structfile"), false);
455 else if (subVals.has("structid"))
457 Console.info("***** Attaching paeFile '" + paePath + "' to "
458 + "structid=" + subVals.get("structid"));
459 EBIAlfaFold.addAlphaFoldPAEToStructure(
460 af.getCurrentView().getAlignment(), paeFile,
461 subVals.getIndex(), subVals.get("structid"), true);
465 Console.debug("***** Attaching paeFile '" + paePath
466 + "' to sequence index " + subVals.getIndex());
467 EBIAlfaFold.addAlphaFoldPAEToSequence(
468 af.getCurrentView().getAlignment(), paeFile,
469 subVals.getIndex(), null);
470 // required to readjust the height and position of the pAE
473 for (AlignmentViewPanel ap : af.getAlignPanels())
475 ap.adjustAnnotationHeight();
481 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
484 AlignFrame af = afMap.get(id);
485 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
486 .findAnnotation(PDBChain.class.getName().toString()))
488 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
489 af.alignPanel.av.getGlobalColourScheme(), 0);
490 acg.setSeqAssociated(true);
491 af.changeColour(acg);
492 Console.info("Changed colour " + acg.toString());
497 protected void processImages(String id)
499 ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id));
500 AlignFrame af = afMap.get(id);
504 Console.warn("Did not have an alignment window for id=" + id);
508 if (avm.hasValue(Arg.IMAGE))
510 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
512 String val = av.getValue();
513 SubVals subVal = new SubVals(val);
514 String type = "png"; // default
515 String fileName = subVal.getContent();
516 File file = new File(fileName);
517 if (subVal.has("type"))
519 type = subVal.get("type");
521 else if (fileName != null)
523 for (String ext : new String[] { "svg", "png", "html" })
525 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
531 // for moment we disable JSON export
532 Cache.setPropsAreReadOnly(true);
533 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
538 Console.debug("Outputting type '" + type + "' to " + fileName);
542 Console.debug("Outputting type '" + type + "' to " + fileName);
546 Console.debug("Outputting type '" + type + "' to " + fileName);
547 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
548 htmlSVG.exportHTML(fileName);
551 Console.warn("--image type '" + type + "' not known. Ignoring");
558 private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
560 AlignmentI al = af.getCurrentView().getAlignment();
561 if (-1 < subId.getIndex()
562 && subId.getIndex() < al.getSequences().size())
564 return al.getSequenceAt(subId.getIndex());
566 else if (subId.has("seqid"))
568 return al.findName(subId.get("seqid"));