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.datamodel.AlignmentAnnotation;
18 import jalview.gui.AlignFrame;
19 import jalview.gui.Desktop;
20 import jalview.io.AppletFormatAdapter;
21 import jalview.io.DataSourceType;
22 import jalview.io.FileFormatException;
23 import jalview.io.FileFormatI;
24 import jalview.io.FileLoader;
25 import jalview.io.IdentifyFile;
26 import jalview.util.HttpUtils;
27 import jalview.util.MessageManager;
28 import jalview.util.Platform;
29 import jalview.ws.dbsources.EBIAlfaFold;
35 private static boolean headless;
37 private static ArgParser argParser;
39 private Map<String, AlignFrame> afMap;
41 public static void processArgs(ArgParser ap, boolean h)
45 if (argParser != null && argParser.linkedIds() != null)
47 for (String id : argParser.linkedIds())
49 Commands cmds = new Commands();
52 cmds.processUnlinked(id);
56 cmds.processLinked(id);
64 this(Desktop.instance);
67 public Commands(Desktop d)
70 afMap = new HashMap<String, AlignFrame>();
73 protected void processUnlinked(String id)
78 protected void processLinked(String id)
80 Map<Arg, ArgValues> m = argParser.linkedArgs(id);
83 // script to execute after all loading is completed one way or another
84 String groovyscript = m.get(Arg.GROOVY) == null ? null
85 : m.get(Arg.GROOVY).getValue();
86 String file = m.get(Arg.OPEN) == null ? null
87 : m.get(Arg.OPEN).getValue();
89 FileFormatI format = null;
90 DataSourceType protocol = null;
93 if (m.get(Arg.OPEN) != null)
99 OPEN: for (String openFile : m.get(Arg.OPEN).getValues())
101 if (openFile == null)
109 desktop.setProgressBar(
110 MessageManager.getString(
111 "status.processing_commandline_args"),
112 progress = System.currentTimeMillis());
116 if (!Platform.isJS())
118 * ignore in JavaScript -- can't just file existence - could load it?
123 if (!HttpUtils.startsWithHttpOrHttps(openFile))
125 if (!(new File(openFile)).exists())
127 Console.warn("Can't find file '" + openFile + "'");
133 DataSourceType protocol = AppletFormatAdapter
134 .checkProtocol(openFile);
136 FileFormatI format = null;
139 format = new IdentifyFile().identify(openFile, protocol);
140 } catch (FileFormatException e1)
142 Console.error("Unknown file format for '" + openFile + "'");
148 // get kind of temperature factor annotation
149 AlignmentAnnotation.TFType tempfacType = null;
150 if ((m.get(Arg.NOTEMPFAC) == null
151 || !m.get(Arg.NOTEMPFAC).getBoolean())
152 && m.get(Arg.TEMPFAC) != null)
156 tempfacType = AlignmentAnnotation.TFType
157 .valueOf(m.get(Arg.TEMPFAC).getValue()
158 .toUpperCase(Locale.ROOT));
159 Console.debug("Obtained Temperature Factor type of '"
160 + tempfacType + "'");
161 } catch (IllegalArgumentException e)
163 StringBuilder sb = new StringBuilder().append("Cannot set --")
164 .append(Arg.TEMPFAC.getName()).append(" to '")
166 .append("', ignoring. Valid values are: ");
167 Iterator<AlignmentAnnotation.TFType> it = Arrays
168 .stream(AlignmentAnnotation.TFType.values())
172 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
176 Console.warn(sb.toString());
181 "Opening '" + openFile + "' in new alignment frame");
182 FileLoader fileLoader = new FileLoader(!headless);
183 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, format,
187 if (m.get(Arg.WRAP) != null && m.get(Arg.WRAP).getBoolean())
189 af.getCurrentView().setWrapAlignment(true);
192 // change alignment frame title
193 if (m.get(Arg.TITLE) != null)
194 af.setTitle(m.get(Arg.TITLE).getValue());
196 // show secondary structure annotations?
197 if (m.get(Arg.SSANNOTATION) != null
198 && !m.get(Arg.SSANNOTATION).getBoolean())
200 // do this better (annotation types?)
201 AlignmentUtils.showOrHideSequenceAnnotations(
202 af.getCurrentView().getAlignment(),
203 Collections.singleton("Secondary Structure"), null,
207 // show temperature factor annotations?
208 if (m.get(Arg.NOTEMPFAC) != null
209 && m.get(Arg.NOTEMPFAC).getBoolean())
211 // do this better (annotation types?)
212 List<String> hideThese = new ArrayList<>();
213 hideThese.add("Temperature Factor");
214 hideThese.add(MessageManager
215 .getString("label.alphafold_reliability"));
216 AlignmentUtils.showOrHideSequenceAnnotations(
217 af.getCurrentView().getAlignment(), hideThese, null,
222 if (m.get(Arg.TEMPFAC_LABEL) != null)
224 AlignmentAnnotation aa = AlignmentUtils
225 .getFirstSequenceAnnotationOfType(
226 af.getCurrentView().getAlignment(),
227 AlignmentAnnotation.LINE_GRAPH);
230 aa.label = m.get(Arg.TEMPFAC_LABEL).getValue();
235 // store the AlignFrame for this id
241 "Opening '" + openFile + "' in existing alignment frame");
242 af.getCurrentView().addFile(new File(openFile), format);
246 .println("Command " + Arg.OPEN + " executed successfully!");
249 if (first) // first=true means nothing opened
253 Console.error("Could not open any files in headless mode");
259 Console.warn("No more files to open");
261 desktop.setProgressBar(null, progress);
266 // load a pAE file if given
267 if (m.get(Arg.PAEMATRIX) != null)
269 AlignFrame af = afMap.get(id);
272 for (String val : m.get(Arg.PAEMATRIX).getValues())
274 SubId subId = new SubId(val);
275 File paeFile = new File(subId.content);
276 EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
277 paeFile, subId.index,
278 "id".equals(subId.keyName) ? subId.keyValue : null);
279 // required to readjust the height and position of the pAE
281 for (AlignmentViewPanel ap : af.getAlignPanels())
283 ap.adjustAnnotationHeight();
291 * A helper class to parse a string of the possible forms "content"
292 * "[index]content", "[keyName=keyValue]content" and return the integer index,
293 * the strings keyName and keyValue, and the content after the square brackets
294 * (if present). Values not set will be -1 or null.
296 protected class SubId
298 protected int index = 0;
300 protected String keyName = null;
302 protected String keyValue = null;
304 protected String content = null;
306 protected SubId(String item)
308 if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
310 int openBracket = item.indexOf('[');
311 int closeBracket = item.indexOf(']');
312 String indexString = item.substring(openBracket + 1, closeBracket);
313 this.content = item.substring(closeBracket + 1);
314 int equals = indexString.indexOf('=');
317 this.keyName = indexString.substring(0, equals);
318 this.keyValue = indexString.substring(equals + 1);
325 this.index = Integer.parseInt(indexString);
326 } catch (NumberFormatException e)
328 Console.warn("Failed to obtain sequenced id or index from '"
329 + item + "'. Setting index=0 and using content='"