JAL-629 improvements to argparser toString. Improvements to cli paeFile structure...
[jalview.git] / src / jalview / bin / Commands.java
1 package jalview.bin;
2
3 import java.io.File;
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;
12 import java.util.Map;
13
14 import jalview.analysis.AlignmentUtils;
15 import jalview.api.AlignmentViewPanel;
16 import jalview.bin.argparser.Arg;
17 import jalview.bin.argparser.ArgParser;
18 import jalview.bin.argparser.ArgValue;
19 import jalview.bin.argparser.ArgValues;
20 import jalview.bin.argparser.ArgValuesMap;
21 import jalview.bin.argparser.SubVals;
22 import jalview.datamodel.AlignmentAnnotation;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.AlignmentPanel;
29 import jalview.gui.AssociatePdbFileWithSeq;
30 import jalview.gui.Desktop;
31 import jalview.gui.Preferences;
32 import jalview.gui.StructureChooser;
33 import jalview.gui.StructureViewer;
34 import jalview.io.AppletFormatAdapter;
35 import jalview.io.DataSourceType;
36 import jalview.io.FileFormatException;
37 import jalview.io.FileFormatI;
38 import jalview.io.FileLoader;
39 import jalview.io.HtmlSvgOutput;
40 import jalview.io.IdentifyFile;
41 import jalview.schemes.AnnotationColourGradient;
42 import jalview.structure.StructureImportSettings;
43 import jalview.structure.StructureImportSettings.TFType;
44 import jalview.structure.StructureSelectionManager;
45 import jalview.util.HttpUtils;
46 import jalview.util.MessageManager;
47 import jalview.util.Platform;
48 import jalview.ws.dbsources.EBIAlfaFold;
49 import mc_view.PDBChain;
50
51 public class Commands
52 {
53   Desktop desktop;
54
55   private boolean headless;
56
57   private ArgParser argParser;
58
59   private Map<String, AlignFrame> afMap;
60
61   private boolean commandArgsProvided = false;
62
63   private boolean argsWereParsed = false;
64
65   public Commands(ArgParser argparser, boolean headless)
66   {
67     this(Desktop.instance, argparser, headless);
68   }
69
70   public Commands(Desktop d, ArgParser argparser, boolean h)
71   {
72     argParser = argparser;
73     headless = h;
74     desktop = d;
75     afMap = new HashMap<String, AlignFrame>();
76     if (argparser != null)
77     {
78       processArgs(argparser, headless);
79     }
80   }
81
82   private boolean processArgs(ArgParser argparser, boolean h)
83   {
84     argParser = argparser;
85     headless = h;
86     boolean theseArgsWereParsed = false;
87
88     if (argParser != null && argParser.linkedIds() != null)
89     {
90       for (String id : argParser.linkedIds())
91       {
92         ArgValuesMap avm = argParser.linkedArgs(id);
93         theseArgsWereParsed = true;
94         if (id == null)
95         {
96           theseArgsWereParsed &= processUnlinked(id);
97         }
98         else
99         {
100           theseArgsWereParsed &= processLinked(id);
101         }
102         theseArgsWereParsed &= processImages(id);
103
104         // close ap
105         if (avm.getBoolean(Arg.CLOSE))
106         {
107           AlignFrame af = afMap.get(id);
108           if (af != null)
109           {
110             af.closeMenuItem_actionPerformed(true);
111           }
112         }
113
114       }
115
116     }
117     if (argParser.getBool(Arg.QUIT))
118     {
119       Jalview.getInstance().quit();
120       return true;
121     }
122     // carry on with jalview.bin.Jalview
123     argsWereParsed = theseArgsWereParsed;
124     return argsWereParsed;
125   }
126
127   public boolean commandArgsProvided()
128   {
129     return commandArgsProvided;
130   }
131
132   public boolean argsWereParsed()
133   {
134     return argsWereParsed;
135   }
136
137   protected boolean processUnlinked(String id)
138   {
139     return processLinked(id);
140   }
141
142   protected boolean processLinked(String id)
143   {
144     boolean theseArgsWereParsed = false;
145     ArgValuesMap avm = argParser.linkedArgs(id);
146     if (avm == null)
147       return true;
148
149     /*
150     // script to execute after all loading is completed one way or another
151     String groovyscript = m.get(Arg.GROOVY) == null ? null
152             : m.get(Arg.GROOVY).getValue();
153     String file = m.get(Arg.OPEN) == null ? null
154             : m.get(Arg.OPEN).getValue();
155     String data = null;
156     FileFormatI format = null;
157     DataSourceType protocol = null;
158     */
159     if (avm.containsArg(Arg.OPEN))
160     {
161       commandArgsProvided = true;
162       long progress = -1;
163
164       boolean first = true;
165       boolean progressBarSet = false;
166       AlignFrame af;
167       for (ArgValue av : avm.getArgValueList(Arg.OPEN))
168       {
169         String openFile = av.getValue();
170         if (openFile == null)
171           continue;
172
173         theseArgsWereParsed = true;
174         if (first)
175         {
176           first = false;
177           if (!headless && desktop != null)
178           {
179             desktop.setProgressBar(
180                     MessageManager.getString(
181                             "status.processing_commandline_args"),
182                     progress = System.currentTimeMillis());
183             progressBarSet = true;
184           }
185         }
186
187         if (!Platform.isJS())
188         /**
189          * ignore in JavaScript -- can't just file existence - could load it?
190          * 
191          * @j2sIgnore
192          */
193         {
194           if (!HttpUtils.startsWithHttpOrHttps(openFile))
195           {
196             if (!(new File(openFile)).exists())
197             {
198               Console.warn("Can't find file '" + openFile + "'");
199             }
200           }
201         }
202
203         DataSourceType protocol = AppletFormatAdapter
204                 .checkProtocol(openFile);
205
206         FileFormatI format = null;
207         try
208         {
209           format = new IdentifyFile().identify(openFile, protocol);
210         } catch (FileFormatException e1)
211         {
212           Console.error("Unknown file format for '" + openFile + "'");
213         }
214
215         af = afMap.get(id);
216         if (af == null)
217         {
218           /*
219            * this approach isn't working yet
220           // get default annotations before opening AlignFrame
221           if (m.get(Arg.SSANNOTATION) != null)
222           {
223             Console.debug("***** SSANNOTATION="
224                     + m.get(Arg.SSANNOTATION).getBoolean());
225           }
226           if (m.get(Arg.NOTEMPFAC) != null)
227           {
228             Console.debug(
229                     "***** NOTEMPFAC=" + m.get(Arg.NOTEMPFAC).getBoolean());
230           }
231           boolean showSecondaryStructure = (m.get(Arg.SSANNOTATION) != null)
232                   ? m.get(Arg.SSANNOTATION).getBoolean()
233                   : false;
234           boolean showTemperatureFactor = (m.get(Arg.NOTEMPFAC) != null)
235                   ? !m.get(Arg.NOTEMPFAC).getBoolean()
236                   : false;
237           Console.debug("***** tempfac=" + showTemperatureFactor
238                   + ", showSS=" + showSecondaryStructure);
239           StructureSelectionManager ssm = StructureSelectionManager
240                   .getStructureSelectionManager(Desktop.instance);
241           if (ssm != null)
242           {
243             ssm.setAddTempFacAnnot(showTemperatureFactor);
244             ssm.setProcessSecondaryStructure(showSecondaryStructure);
245           }
246            */
247
248           // get kind of temperature factor annotation
249           StructureImportSettings.TFType tempfacType = TFType.DEFAULT;
250           if ((!avm.getBoolean(Arg.NOTEMPFAC))
251                   && avm.containsArg(Arg.TEMPFAC))
252           {
253             try
254             {
255               tempfacType = StructureImportSettings.TFType
256                       .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue()
257                               .toUpperCase(Locale.ROOT));
258               Console.debug("Obtained Temperature Factor type of '"
259                       + tempfacType + "'");
260             } catch (IllegalArgumentException e)
261             {
262               // Just an error message!
263               StringBuilder sb = new StringBuilder().append("Cannot set --")
264                       .append(Arg.TEMPFAC.getName()).append(" to '")
265                       .append(tempfacType)
266                       .append("', ignoring.  Valid values are: ");
267               Iterator<StructureImportSettings.TFType> it = Arrays
268                       .stream(StructureImportSettings.TFType.values())
269                       .iterator();
270               while (it.hasNext())
271               {
272                 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
273                 if (it.hasNext())
274                   sb.append(", ");
275               }
276               Console.warn(sb.toString());
277             }
278           }
279
280           Console.debug(
281                   "Opening '" + openFile + "' in new alignment frame");
282           FileLoader fileLoader = new FileLoader(!headless);
283
284           StructureImportSettings.setTemperatureFactorType(tempfacType);
285
286           af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
287                   format);
288
289           // wrap alignment?
290           if (avm.getBoolean(Arg.WRAP))
291           {
292             af.getCurrentView().setWrapAlignment(true);
293           }
294
295           // colour aligment?
296           if (avm.containsArg(Arg.COLOUR))
297           {
298             af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR));
299           }
300
301           // change alignment frame title
302           if (avm.containsArg(Arg.TITLE))
303             af.setTitle(avm.getValue(Arg.TITLE));
304
305           /* hacky approach to hiding the annotations */
306           // show secondary structure annotations?
307           if (avm.getBoolean(Arg.SSANNOTATION))
308           {
309             // do this better (annotation types?)
310             AlignmentUtils.showOrHideSequenceAnnotations(
311                     af.getCurrentView().getAlignment(),
312                     Collections.singleton("Secondary Structure"), null,
313                     false, false);
314           }
315
316           // show temperature factor annotations?
317           if (avm.getBoolean(Arg.NOTEMPFAC))
318           {
319             // do this better (annotation types?)
320             List<String> hideThese = new ArrayList<>();
321             hideThese.add("Temperature Factor");
322             hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
323             AlignmentUtils.showOrHideSequenceAnnotations(
324                     af.getCurrentView().getAlignment(), hideThese, null,
325                     false, false);
326           }
327           else
328           /* comment out hacky approach up to here and add this line:
329            if (showTemperatureFactor)
330              */
331           {
332             if (avm.containsArg(Arg.TEMPFAC_LABEL))
333             {
334               AlignmentAnnotation aa = AlignmentUtils
335                       .getFirstSequenceAnnotationOfType(
336                               af.getCurrentView().getAlignment(),
337                               AlignmentAnnotation.LINE_GRAPH);
338               String label = avm.getValue(Arg.TEMPFAC_LABEL);
339               if (aa != null)
340               {
341                 aa.label = label;
342               }
343               else
344               {
345                 Console.info(
346                         "Could not find annotation to apply tempfac_label '"
347                                 + label);
348               }
349             }
350           }
351
352           // store the AlignFrame for this id
353           afMap.put(id, af);
354
355           // is it its own structure file?
356           if (format.isStructureFile())
357           {
358             StructureSelectionManager ssm = StructureSelectionManager
359                     .getStructureSelectionManager(Desktop.instance);
360             SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
361             ssm.computeMapping(false, new SequenceI[] { seq }, null,
362                     openFile, DataSourceType.FILE, null, null, null);
363           }
364         }
365         else
366         {
367           Console.debug(
368                   "Opening '" + openFile + "' in existing alignment frame");
369           af.getCurrentView().addFile(new File(openFile), format, false);
370         }
371
372         Console.debug("Command " + Arg.OPEN + " executed successfully!");
373
374       }
375       if (first) // first=true means nothing opened
376       {
377         if (headless)
378         {
379           Jalview.exit("Could not open any files in headless mode", 1);
380         }
381         else
382         {
383           Console.warn("No more files to open");
384         }
385       }
386       if (progressBarSet && desktop != null)
387         desktop.setProgressBar(null, progress);
388
389     }
390
391     // open the structure (from same PDB file or given PDBfile)
392     if (!avm.getBoolean(Arg.NOSTRUCTURE))
393     {
394       AlignFrame af = afMap.get(id);
395       if (avm.containsArg(Arg.STRUCTURE))
396       {
397         commandArgsProvided = true;
398         for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
399         {
400           String val = av.getValue();
401           SubVals subId = new SubVals(val);
402           SequenceI seq = getSpecifiedSequence(af, subId);
403           if (seq == null)
404           {
405             Console.warn("Could not find sequence for argument --"
406                     + Arg.STRUCTURE + "=" + val);
407             // you probably want to continue here, not break
408             // break;
409             continue;
410           }
411           File structureFile = null;
412           if (subId.getContent() != null
413                   && subId.getContent().length() != 0)
414           {
415             structureFile = new File(subId.getContent());
416             Console.debug("Using structure file (from argument) '"
417                     + structureFile.getAbsolutePath() + "'");
418           }
419
420           // TRY THIS
421           /*
422            PDBEntry fileEntry = new AssociatePdbFileWithSeq()
423                   .associatePdbWithSeq(selectedPdbFileName,
424                           DataSourceType.FILE, selectedSequence, true,
425                           Desktop.instance);
426                           
427            sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
428                   ap, new SequenceI[]
429                   { selectedSequence });
430           
431            */
432
433           /* THIS DOESN'T WORK */
434           else if (seq.getAllPDBEntries() != null
435                   && seq.getAllPDBEntries().size() > 0)
436           {
437             structureFile = new File(
438                     seq.getAllPDBEntries().elementAt(0).getFile());
439             Console.debug("Using structure file (from sequence) '"
440                     + structureFile.getAbsolutePath() + "'");
441           }
442
443           if (structureFile == null)
444           {
445             Console.warn("Not provided structure file with '" + val + "'");
446             continue;
447           }
448
449           if (!structureFile.exists())
450           {
451             Console.warn("Structure file '"
452                     + structureFile.getAbsoluteFile() + "' not found.");
453             continue;
454           }
455
456           Console.debug("Using structure file "
457                   + structureFile.getAbsolutePath());
458
459           PDBEntry fileEntry = new AssociatePdbFileWithSeq()
460                   .associatePdbWithSeq(structureFile.getAbsolutePath(),
461                           DataSourceType.FILE, seq, true, Desktop.instance);
462
463           // open structure view
464           AlignmentPanel ap = af.alignPanel;
465           if (headless)
466           {
467             Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
468                     StructureViewer.ViewerType.JMOL.toString());
469           }
470
471           // get tft, paeFilename, label?
472           /*
473           ArgValue tftAv = avm.getArgValuesReferringTo("structid", structId,
474                   Arg.TEMPFAC);
475            */
476           StructureChooser.openStructureFileForSequence(null, null, ap, seq,
477                   false, structureFile.getAbsolutePath(), null, null); // tft,
478                                                                        // paeFilename);
479         }
480       }
481     }
482
483     // load a pAE file if given
484     if (avm.containsArg(Arg.PAEMATRIX))
485     {
486       AlignFrame af = afMap.get(id);
487       if (af != null)
488       {
489         for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX))
490         {
491           String val = av.getValue();
492           SubVals subVals = ArgParser.getSubVals(val);
493           String paeLabel = subVals.get("label");
494           File paeFile = new File(subVals.getContent());
495           String paePath = null;
496           try
497           {
498             paePath = paeFile.getCanonicalPath();
499           } catch (IOException e)
500           {
501             paePath = paeFile.getAbsolutePath();
502             Console.warn(
503                     "Problem with the PAE file path: '" + paePath + "'");
504           }
505           String structid = null;
506           String structfile = null;
507           String seqid = null;
508           if (subVals.notSet())
509           {
510             ArgValue likelyStructure = avm
511                     .getClosestPreviousArgValueOfArg(av, Arg.STRUCTURE);
512             if (likelyStructure != null)
513             {
514               SubVals sv = likelyStructure.getSubVals();
515               if (sv != null && sv.has(ArgValues.ID))
516               {
517                 structid = sv.get(ArgValues.ID);
518               }
519               else
520               {
521                 structfile = likelyStructure.getValue();
522               }
523             }
524           }
525           else if (subVals.has("structfile"))
526           {
527             structfile = subVals.get("structfile");
528           }
529           else if (subVals.has("structid"))
530           {
531             structid = subVals.get("structid");
532           }
533           if (structfile != null)
534           {
535             Console.info("***** Attaching paeFile '" + paePath + "' to "
536                     + "structfile=" + subVals.get("structfile"));
537             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
538                     paeFile, subVals.getIndex(), subVals.get("structfile"),
539                     true, false, paeLabel);
540           }
541           else if (structid != null)
542           {
543             Console.info("***** Attaching paeFile '" + paePath + "' to "
544                     + "structid=" + subVals.get("structid"));
545             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
546                     paeFile, subVals.getIndex(), subVals.get("structid"),
547                     true, true, paeLabel);
548           }
549           else
550           {
551             Console.debug("***** Attaching paeFile '" + paePath
552                     + "' to sequence index " + subVals.getIndex());
553             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
554                     paeFile, subVals.getIndex(), null, false, false,
555                     paeLabel);
556             // required to readjust the height and position of the pAE
557             // annotation
558           }
559           for (AlignmentViewPanel ap : af.getAlignPanels())
560           {
561             ap.adjustAnnotationHeight();
562           }
563         }
564       }
565     }
566
567     boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
568     if (doShading)
569     {
570       AlignFrame af = afMap.get(id);
571       for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
572               .findAnnotation(PDBChain.class.getName().toString()))
573       {
574         AnnotationColourGradient acg = new AnnotationColourGradient(aa,
575                 af.alignPanel.av.getGlobalColourScheme(), 0);
576         acg.setSeqAssociated(true);
577         af.changeColour(acg);
578         Console.info("Changed colour " + acg.toString());
579       }
580     }
581
582     return theseArgsWereParsed;
583   }
584
585   protected boolean processImages(String id)
586   {
587     ArgValuesMap avm = argParser.linkedArgs(id);
588     AlignFrame af = afMap.get(id);
589
590     if (af == null)
591     {
592       Console.warn("Did not have an alignment window for id=" + id);
593       return false;
594     }
595
596     if (avm.containsArg(Arg.IMAGE))
597     {
598       for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
599       {
600         String val = av.getValue();
601         SubVals subVal = new SubVals(val);
602         String type = "png"; // default
603         String fileName = subVal.getContent();
604         File file = new File(fileName);
605         if (subVal.has("type"))
606         {
607           type = subVal.get("type");
608         }
609         else if (fileName != null)
610         {
611           for (String ext : new String[] { "svg", "png", "html" })
612           {
613             if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
614             {
615               type = ext;
616             }
617           }
618         }
619         // for moment we disable JSON export
620         Cache.setPropsAreReadOnly(true);
621         Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
622
623         switch (type)
624         {
625         case "svg":
626           Console.debug("Outputting type '" + type + "' to " + fileName);
627           af.createSVG(file);
628           break;
629         case "png":
630           Console.debug("Outputting type '" + type + "' to " + fileName);
631           af.createPNG(file);
632           break;
633         case "html":
634           Console.debug("Outputting type '" + type + "' to " + fileName);
635           HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
636           htmlSVG.exportHTML(fileName);
637           break;
638         default:
639           Console.warn("--image type '" + type + "' not known. Ignoring");
640           break;
641         }
642       }
643     }
644     return true;
645   }
646
647   private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId)
648   {
649     AlignmentI al = af.getCurrentView().getAlignment();
650     if (-1 < subId.getIndex()
651             && subId.getIndex() < al.getSequences().size())
652     {
653       return al.getSequenceAt(subId.getIndex());
654     }
655     else if (subId.has("seqid"))
656     {
657       return al.findName(subId.get("seqid"));
658     }
659     return null;
660   }
661 }