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.util.HttpUtils;
36 import jalview.util.MessageManager;
37 import jalview.util.Platform;
38 import jalview.ws.dbsources.EBIAlfaFold;
44 private static boolean headless;
46 private static ArgParser argParser;
48 private Map<String, AlignFrame> afMap;
50 public static void processArgs(ArgParser ap, boolean h)
54 if (argParser != null && argParser.linkedIds() != null)
56 for (String id : argParser.linkedIds())
58 Commands cmds = new Commands();
61 cmds.processUnlinked(id);
65 cmds.processLinked(id);
67 cmds.processImages(id);
70 if (argParser.getBool(Arg.QUIT))
72 Jalview.getInstance().quit();
79 this(Desktop.instance);
82 public Commands(Desktop d)
85 afMap = new HashMap<String, AlignFrame>();
88 protected void processUnlinked(String id)
93 protected void processLinked(String id)
95 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
98 // script to execute after all loading is completed one way or another
99 String groovyscript = m.get(Arg.GROOVY) == null ? null
100 : m.get(Arg.GROOVY).getValue();
101 String file = m.get(Arg.OPEN) == null ? null
102 : m.get(Arg.OPEN).getValue();
104 FileFormatI format = null;
105 DataSourceType protocol = null;
108 if (ArgParser.getArgValues(m, Arg.OPEN) != null)
112 boolean first = true;
114 OPEN: for (String openFile : ArgParser.getValues(m, Arg.OPEN))
116 if (openFile == null)
124 desktop.setProgressBar(
125 MessageManager.getString(
126 "status.processing_commandline_args"),
127 progress = System.currentTimeMillis());
131 if (!Platform.isJS())
133 * ignore in JavaScript -- can't just file existence - could load it?
138 if (!HttpUtils.startsWithHttpOrHttps(openFile))
140 if (!(new File(openFile)).exists())
142 Console.warn("Can't find file '" + openFile + "'");
148 DataSourceType protocol = AppletFormatAdapter
149 .checkProtocol(openFile);
151 FileFormatI format = null;
154 format = new IdentifyFile().identify(openFile, protocol);
155 } catch (FileFormatException e1)
157 Console.error("Unknown file format for '" + openFile + "'");
164 * this approach isn't working yet
165 // get default annotations before opening AlignFrame
166 if (m.get(Arg.SSANNOTATION) != null)
168 Console.debug("***** SSANNOTATION="
169 + m.get(Arg.SSANNOTATION).getBoolean());
171 if (m.get(Arg.NOTEMPFAC) != null)
174 "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
176 boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
177 ? m.get(Arg.SSANNOTATION).getBoolean()
179 boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
180 ? !m.get(Arg.NOTEMPFAC).getBoolean()
182 Console.debug("***** tempfac=" + showTemperatureFactor
183 + ", showSS=" + showSecondaryStructure);
184 StructureSelectionManager ssm = StructureSelectionManager
185 .getStructureSelectionManager(Desktop.instance);
188 ssm.setAddTempFacAnnot(showTemperatureFactor);
189 ssm.setProcessSecondaryStructure(showSecondaryStructure);
193 // get kind of temperature factor annotation
194 StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
195 if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC))
196 && ArgParser.getArgValues(m, Arg.TEMPFAC) != null)
200 tempfacType = StructureImportSettings.TFType.valueOf(ArgParser
201 .getValue(m, Arg.TEMPFAC).toUpperCase(Locale.ROOT));
202 Console.debug("Obtained Temperature Factor type of '"
203 + tempfacType + "'");
204 } catch (IllegalArgumentException e)
206 // Just an error message!
207 StringBuilder sb = new StringBuilder().append("Cannot set --")
208 .append(Arg.TEMPFAC.getName()).append(" to '")
210 .append("', ignoring. Valid values are: ");
211 Iterator<StructureImportSettings.TFType> it = Arrays
212 .stream(StructureImportSettings.TFType.values())
216 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
220 Console.warn(sb.toString());
225 "Opening '" + openFile + "' in new alignment frame");
226 FileLoader fileLoader = new FileLoader(!headless);
228 StructureImportSettings.setTemperatureFactorType(tempfacType);
230 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
234 if (ArgParser.getBoolean(m, Arg.WRAP))
236 af.getCurrentView().setWrapAlignment(true);
239 // change alignment frame title
240 if (ArgParser.getValue(m, Arg.TITLE) != null)
241 af.setTitle(ArgParser.getValue(m, Arg.TITLE));
243 /* hacky approach to hiding the annotations */
244 // show secondary structure annotations?
245 if (ArgParser.getBoolean(m, Arg.SSANNOTATION))
247 // do this better (annotation types?)
248 AlignmentUtils.showOrHideSequenceAnnotations(
249 af.getCurrentView().getAlignment(),
250 Collections.singleton("Secondary Structure"), null,
254 // show temperature factor annotations?
255 if (ArgParser.getBoolean(m, Arg.NOTEMPFAC))
257 // do this better (annotation types?)
258 List<String> hideThese = new ArrayList<>();
259 hideThese.add("Temperature Factor");
260 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
261 AlignmentUtils.showOrHideSequenceAnnotations(
262 af.getCurrentView().getAlignment(), hideThese, null,
266 /* comment out hacky approach up to here and add this line:
267 if (showTemperatureFactor)
270 if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null)
272 AlignmentAnnotation aa = AlignmentUtils
273 .getFirstSequenceAnnotationOfType(
274 af.getCurrentView().getAlignment(),
275 AlignmentAnnotation.LINE_GRAPH);
276 String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL);
284 "Could not find annotation to apply tempfac_label '"
290 // store the AlignFrame for this id
296 "Opening '" + openFile + "' in existing alignment frame");
297 af.getCurrentView().addFile(new File(openFile), format);
301 .println("Command " + Arg.OPEN + " executed successfully!");
304 if (first) // first=true means nothing opened
308 Console.error("Could not open any files in headless mode");
314 Console.warn("No more files to open");
316 desktop.setProgressBar(null, progress);
321 // load a pAE file if given
322 if (ArgParser.getValues(m, Arg.PAEMATRIX) != null)
324 AlignFrame af = afMap.get(id);
327 for (String val : ArgParser.getValues(m, Arg.PAEMATRIX))
329 SubVal subVal = ArgParser.getSubVal(val);
330 File paeFile = new File(subVal.content);
331 if ("structid".equals(subVal.index))
333 EBIAlfaFold.addAlphaFoldPAEToStructure(
334 af.getCurrentView().getAlignment(), paeFile,
335 subVal.index, subVal.keyValue);
339 EBIAlfaFold.addAlphaFoldPAEToSequence(af.getCurrentView().getAlignment(),
340 paeFile, subVal.index,
341 "id".equals(subVal.keyName) ? subVal.keyValue : null);
342 // required to readjust the height and position of the pAE
345 for (AlignmentViewPanel ap : af.getAlignPanels())
347 ap.adjustAnnotationHeight();
353 // open the structure (from same PDB file or given PDBfile)
354 if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE))
356 AlignFrame af = afMap.get(id);
357 if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null)
359 STRUCTURE: for (String val : ArgParser.getValues(m, Arg.STRUCTURE))
361 SubVal subId = new SubVal(val);
362 SequenceI seq = getSpecifiedSequence(af, subId);
365 Console.warn("Could not find sequence for argument --"
366 + Arg.STRUCTURE + "=" + val);
369 File structureFile = null;
370 if (subId.content != null && subId.content.length() != 0)
372 structureFile = new File(subId.content);
373 Console.debug("Using structure file (from argument) '"
374 + structureFile.getAbsolutePath() + "'");
376 /* THIS DOESN'T WORK */
377 else if (seq.getAllPDBEntries() != null
378 && seq.getAllPDBEntries().size() > 0)
380 structureFile = new File(
381 seq.getAllPDBEntries().elementAt(0).getFile());
382 Console.debug("Using structure file (from sequence) '"
383 + structureFile.getAbsolutePath() + "'");
386 if (structureFile == null)
388 Console.warn("Not provided structure file with '" + val + "'");
392 if (!structureFile.exists())
394 Console.warn("Structure file '"
395 + structureFile.getAbsoluteFile() + "' not found.");
399 Console.debug("Using structure file "
400 + structureFile.getAbsolutePath());
402 // open structure view
403 AlignmentPanel ap = af.alignPanel;
404 StructureChooser.openStructureFileForSequence(ap, seq,
411 protected void processImages(String id)
413 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
414 AlignFrame af = afMap.get(id);
418 Console.warn("Did not have an alignment window for id=" + id);
422 if (ArgParser.getValues(m, Arg.IMAGE) != null)
424 for (String val : ArgParser.getValues(m, Arg.IMAGE))
426 SubVal subVal = new SubVal(val);
427 String type = "png"; // default
428 String fileName = subVal.content;
429 File file = new File(fileName);
430 if ("type".equals(subVal.keyName))
432 type = subVal.keyValue;
434 else if (fileName != null)
436 for (String ext : new String[] { "svg", "png", "html" })
438 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
447 Console.debug("Outputting type '" + type + "' to " + fileName);
451 Console.debug("Outputting type '" + type + "' to " + fileName);
455 Console.debug("Outputting type '" + type + "' to " + fileName);
456 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
457 htmlSVG.exportHTML(fileName);
460 Console.warn("--image type '" + type + "' not known. Ignoring");
467 private SequenceI getSpecifiedSequence(AlignFrame af, SubVal subId)
469 AlignmentI al = af.getCurrentView().getAlignment();
470 if (-1 < subId.index && subId.index < al.getSequences().size())
472 return al.getSequenceAt(subId.index);
474 else if ("id".equals(subId.keyName))
476 return al.findName(subId.keyValue);