JAL-629 Add --seqid for non-subval use, make arg value naming more consistent in...
[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.net.URISyntaxException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.EnumSet;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Locale;
14 import java.util.Map;
15
16 import jalview.analysis.AlignmentUtils;
17 import jalview.bin.argparser.Arg;
18 import jalview.bin.argparser.ArgParser;
19 import jalview.bin.argparser.ArgParser.Position;
20 import jalview.bin.argparser.ArgValue;
21 import jalview.bin.argparser.ArgValues;
22 import jalview.bin.argparser.ArgValuesMap;
23 import jalview.bin.argparser.SubVals;
24 import jalview.datamodel.AlignmentI;
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.Desktop;
30 import jalview.gui.Preferences;
31 import jalview.gui.StructureChooser;
32 import jalview.gui.StructureViewer;
33 import jalview.gui.StructureViewer.ViewerType;
34 import jalview.io.AppletFormatAdapter;
35 import jalview.io.BackupFiles;
36 import jalview.io.BioJsHTMLOutput;
37 import jalview.io.DataSourceType;
38 import jalview.io.FileFormat;
39 import jalview.io.FileFormatException;
40 import jalview.io.FileFormatI;
41 import jalview.io.FileFormats;
42 import jalview.io.FileLoader;
43 import jalview.io.HtmlSvgOutput;
44 import jalview.io.IdentifyFile;
45 import jalview.io.NewickFile;
46 import jalview.structure.StructureImportSettings.TFType;
47 import jalview.structure.StructureSelectionManager;
48 import jalview.util.HttpUtils;
49 import jalview.util.MessageManager;
50 import jalview.util.Platform;
51
52 public class Commands
53 {
54   Desktop desktop;
55
56   private boolean headless;
57
58   private ArgParser argParser;
59
60   private Map<String, AlignFrame> afMap;
61
62   private boolean commandArgsProvided = false;
63
64   private boolean argsWereParsed = false;
65
66   public Commands(ArgParser argparser, boolean headless)
67   {
68     this(Desktop.instance, argparser, headless);
69   }
70
71   public Commands(Desktop d, ArgParser argparser, boolean h)
72   {
73     argParser = argparser;
74     headless = h;
75     desktop = d;
76     afMap = new HashMap<String, AlignFrame>();
77     if (argparser != null)
78     {
79       processArgs(argparser, headless);
80     }
81   }
82
83   private boolean processArgs(ArgParser argparser, boolean h)
84   {
85     argParser = argparser;
86     headless = h;
87     boolean theseArgsWereParsed = false;
88
89     if (argParser != null && argParser.getLinkedIds() != null)
90     {
91       for (String id : argParser.getLinkedIds())
92       {
93         ArgValuesMap avm = argParser.getLinkedArgs(id);
94         theseArgsWereParsed = true;
95         theseArgsWereParsed &= processLinked(id);
96         processGroovyScript(id);
97         boolean processLinkedOkay = theseArgsWereParsed;
98         theseArgsWereParsed &= processImages(id);
99         if (processLinkedOkay)
100           theseArgsWereParsed &= processOutput(id);
101
102         // close ap
103         if (avm.getBoolean(Arg.CLOSE))
104         {
105           AlignFrame af = afMap.get(id);
106           if (af != null)
107           {
108             af.closeMenuItem_actionPerformed(true);
109           }
110         }
111
112       }
113
114     }
115     if (argParser.getBoolean(Arg.QUIT))
116     {
117       Jalview.getInstance().quit();
118       return true;
119     }
120     // carry on with jalview.bin.Jalview
121     argsWereParsed = theseArgsWereParsed;
122     return argsWereParsed;
123   }
124
125   public boolean commandArgsProvided()
126   {
127     return commandArgsProvided;
128   }
129
130   public boolean argsWereParsed()
131   {
132     return argsWereParsed;
133   }
134
135   protected boolean processUnlinked(String id)
136   {
137     return processLinked(id);
138   }
139
140   protected boolean processLinked(String id)
141   {
142     boolean theseArgsWereParsed = false;
143     ArgValuesMap avm = argParser.getLinkedArgs(id);
144     if (avm == null)
145       return true;
146
147     /*
148      * // script to execute after all loading is completed one way or another String
149      * groovyscript = m.get(Arg.GROOVY) == null ? null :
150      * m.get(Arg.GROOVY).getValue(); String file = m.get(Arg.OPEN) == null ? null :
151      * m.get(Arg.OPEN).getValue(); String data = null; FileFormatI format = null;
152      * DataSourceType protocol = null;
153      */
154     if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN))
155     {
156       commandArgsProvided = true;
157       long progress = -1;
158
159       boolean first = true;
160       boolean progressBarSet = false;
161       AlignFrame af;
162       // Combine the APPEND and OPEN files into one list, along with whether it
163       // was APPEND or OPEN
164       List<ArgValue> openAvList = new ArrayList<>();
165       openAvList.addAll(avm.getArgValueList(Arg.OPEN));
166       openAvList.addAll(avm.getArgValueList(Arg.APPEND));
167       // sort avlist based on av.getArgIndex()
168       Collections.sort(openAvList);
169       for (ArgValue av : openAvList)
170       {
171         Arg a = av.getArg();
172         SubVals sv = av.getSubVals();
173         String openFile = av.getValue();
174         if (openFile == null)
175           continue;
176
177         theseArgsWereParsed = true;
178         if (first)
179         {
180           first = false;
181           if (!headless && desktop != null)
182           {
183             desktop.setProgressBar(
184                     MessageManager.getString(
185                             "status.processing_commandline_args"),
186                     progress = System.currentTimeMillis());
187             progressBarSet = true;
188           }
189         }
190
191         if (!Platform.isJS())
192         /**
193          * ignore in JavaScript -- can't just file existence - could load it?
194          * 
195          * @j2sIgnore
196          */
197         {
198           if (!HttpUtils.startsWithHttpOrHttps(openFile))
199           {
200             if (!(new File(openFile)).exists())
201             {
202               Console.warn("Can't find file '" + openFile + "'");
203             }
204           }
205         }
206
207         DataSourceType protocol = AppletFormatAdapter
208                 .checkProtocol(openFile);
209
210         FileFormatI format = null;
211         try
212         {
213           format = new IdentifyFile().identify(openFile, protocol);
214         } catch (FileFormatException e1)
215         {
216           Console.error("Unknown file format for '" + openFile + "'");
217         }
218
219         af = afMap.get(id);
220         // When to open a new AlignFrame
221         if (af == null || "true".equals(av.getSubVal("new"))
222                 || a == Arg.OPEN || format == FileFormat.Jalview)
223         {
224           if (a == Arg.OPEN)
225           {
226             Jalview.testoutput(argParser, Arg.OPEN, "examples/uniref50.fa",
227                     openFile);
228           }
229
230           Console.debug(
231                   "Opening '" + openFile + "' in new alignment frame");
232           FileLoader fileLoader = new FileLoader(!headless);
233
234           af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
235                   format);
236
237           // wrap alignment?
238           boolean wrap = ArgParser.getFromSubValArgOrPref(avm, Arg.WRAP, sv,
239                   null, "WRAP_ALIGNMENT", false);
240           af.getCurrentView().setWrapAlignment(wrap);
241
242           // colour alignment?
243           String colour = ArgParser.getFromSubValArgOrPref(avm, av,
244                   Arg.COLOUR, sv, null, "DEFAULT_COLOUR_PROT", "");
245           if ("" != colour)
246           {
247             af.changeColour_actionPerformed(colour);
248             Jalview.testoutput(argParser, Arg.COLOUR, "zappo", colour);
249           }
250
251           // Change alignment frame title
252           String title = ArgParser.getFromSubValArgOrPref(avm, av,
253                   Arg.TITLE, sv, null, null, null);
254           if (title != null)
255           {
256             af.setTitle(title);
257             Jalview.testoutput(argParser, Arg.TITLE, "test title", title);
258           }
259
260           // Add features
261           String featuresfile = ArgParser.getValueFromSubValOrArg(avm, av,
262                   Arg.FEATURES, sv);
263           if (featuresfile != null)
264           {
265             af.parseFeaturesFile(featuresfile,
266                     AppletFormatAdapter.checkProtocol(featuresfile));
267             Jalview.testoutput(argParser, Arg.FEATURES,
268                     "examples/testdata/plantfdx.features", featuresfile);
269           }
270
271           // Add annotations from file
272           String annotationsfile = ArgParser.getValueFromSubValOrArg(avm,
273                   av, Arg.ANNOTATIONS, sv);
274           if (annotationsfile != null)
275           {
276             af.loadJalviewDataFile(annotationsfile, null, null, null);
277             Jalview.testoutput(argParser, Arg.ANNOTATIONS,
278                     "examples/testdata/plantfdx.annotations",
279                     annotationsfile);
280           }
281
282           // Set or clear the sortbytree flag
283           boolean sortbytree = ArgParser.getBoolFromSubValOrArg(avm,
284                   Arg.SORTBYTREE, sv);
285           if (sortbytree)
286           {
287             af.getViewport().setSortByTree(true);
288             Jalview.testoutput(argParser, Arg.SORTBYTREE);
289           }
290
291           // Load tree from file
292           String treefile = ArgParser.getValueFromSubValOrArg(avm, av,
293                   Arg.TREE, sv);
294           if (treefile != null)
295           {
296             try
297             {
298               NewickFile nf = new NewickFile(treefile,
299                       AppletFormatAdapter.checkProtocol(treefile));
300               af.getViewport().setCurrentTree(
301                       af.showNewickTree(nf, treefile).getTree());
302               Jalview.testoutput(argParser, Arg.TREE,
303                       "examples/testdata/uniref50_test_tree", treefile);
304             } catch (IOException e)
305             {
306               Console.warn("Couldn't add tree " + treefile, e);
307             }
308           }
309
310           // Show secondary structure annotations?
311           boolean showSSAnnotations = ArgParser.getFromSubValArgOrPref(avm,
312                   Arg.SSANNOTATIONS, av.getSubVals(), null,
313                   "STRUCT_FROM_PDB", true);
314           af.setAnnotationsVisibility(showSSAnnotations, true, false);
315
316           // Show sequence annotations?
317           boolean showAnnotations = ArgParser.getFromSubValArgOrPref(avm,
318                   Arg.SHOWANNOTATIONS, av.getSubVals(), null,
319                   "SHOW_ANNOTATIONS", true);
320           af.setAnnotationsVisibility(showAnnotations, false, true);
321
322           // show temperature factor annotations?
323           if (avm.getBoolean(Arg.NOTEMPFAC))
324           {
325             // do this better (annotation types?)
326             List<String> hideThese = new ArrayList<>();
327             hideThese.add("Temperature Factor");
328             hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
329             AlignmentUtils.showOrHideSequenceAnnotations(
330                     af.getCurrentView().getAlignment(), hideThese, null,
331                     false, false);
332           }
333           else
334           /*
335            * comment out hacky approach up to here and add this line: if
336            * (showTemperatureFactor)
337            */
338           {
339             /*
340             if (avm.containsArg(Arg.TEMPFAC_LABEL))
341             {
342               AlignmentAnnotation aa = AlignmentUtils
343                       .getFirstSequenceAnnotationOfType(
344                               af.getCurrentView().getAlignment(),
345                               AlignmentAnnotation.LINE_GRAPH);
346               String label = avm.getValue(Arg.TEMPFAC_LABEL);
347               if (aa != null)
348               {
349                 aa.label = label;
350               }
351               else
352               {
353                 Console.info(
354                         "Could not find annotation to apply tempfac_label '"
355                                 + label);
356               }
357             }
358             */
359           }
360
361           // store the AlignFrame for this id
362           afMap.put(id, af);
363
364           // is it its own structure file?
365           if (format.isStructureFile())
366           {
367             StructureSelectionManager ssm = StructureSelectionManager
368                     .getStructureSelectionManager(Desktop.instance);
369             SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
370             ssm.computeMapping(false, new SequenceI[] { seq }, null,
371                     openFile, DataSourceType.FILE, null, null, null, false);
372           }
373         }
374         else
375         {
376           Console.debug(
377                   "Opening '" + openFile + "' in existing alignment frame");
378           DataSourceType dst = HttpUtils.startsWithHttpOrHttps(openFile)
379                   ? DataSourceType.URL
380                   : DataSourceType.FILE;
381           FileLoader fileLoader = new FileLoader(!headless);
382           fileLoader.LoadFile(af.getCurrentView(), openFile, dst, null,
383                   false);
384         }
385
386         Console.debug("Command " + Arg.APPEND + " executed successfully!");
387
388       }
389       if (first) // first=true means nothing opened
390       {
391         if (headless)
392         {
393           Jalview.exit("Could not open any files in headless mode", 1);
394         }
395         else
396         {
397           Console.warn("No more files to open");
398         }
399       }
400       if (progressBarSet && desktop != null)
401         desktop.setProgressBar(null, progress);
402
403     }
404
405     // open the structure (from same PDB file or given PDBfile)
406     if (!avm.getBoolean(Arg.NOSTRUCTURE))
407     {
408       AlignFrame af = afMap.get(id);
409       if (avm.containsArg(Arg.STRUCTURE))
410       {
411         commandArgsProvided = true;
412         for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
413         {
414           String val = av.getValue();
415           SubVals subVals = av.getSubVals();
416           SequenceI seq = getSpecifiedSequence(af, avm, av);
417           if (seq == null)
418           {
419             // Could not find sequence from subId, let's assume the first
420             // sequence in the alignframe
421             AlignmentI al = af.getCurrentView().getAlignment();
422             seq = al.getSequenceAt(0);
423           }
424
425           if (seq == null)
426           {
427             Console.warn("Could not find sequence for argument "
428                     + Arg.STRUCTURE.argString() + "=" + val);
429             // you probably want to continue here, not break
430             // break;
431             continue;
432           }
433           File structureFile = null;
434           if (subVals.getContent() != null
435                   && subVals.getContent().length() != 0)
436           {
437             structureFile = new File(subVals.getContent());
438             Console.debug("Using structure file (from argument) '"
439                     + structureFile.getAbsolutePath() + "'");
440           }
441           // TRY THIS
442           /*
443            * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
444            * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE,
445            * selectedSequence, true, Desktop.instance);
446            * 
447            * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new
448            * SequenceI[] { selectedSequence });
449            * 
450            */
451           /* THIS DOESN'T WORK */
452           else if (seq.getAllPDBEntries() != null
453                   && seq.getAllPDBEntries().size() > 0)
454           {
455             structureFile = new File(
456                     seq.getAllPDBEntries().elementAt(0).getFile());
457             Console.debug("Using structure file (from sequence) '"
458                     + structureFile.getAbsolutePath() + "'");
459           }
460
461           if (structureFile == null)
462           {
463             Console.warn("Not provided structure file with '" + val + "'");
464             continue;
465           }
466
467           if (!structureFile.exists())
468           {
469             Console.warn("Structure file '"
470                     + structureFile.getAbsoluteFile() + "' not found.");
471             continue;
472           }
473
474           Console.debug("Using structure file "
475                   + structureFile.getAbsolutePath());
476
477           // ##### Does this need to happen? Follow
478           // openStructureFileForSequence() below
479           /*
480           PDBEntry fileEntry = new AssociatePdbFileWithSeq()
481                   .associatePdbWithSeq(structureFile.getAbsolutePath(),
482                           DataSourceType.FILE, seq, true, Desktop.instance);
483                           */
484
485           // open structure view
486           AlignmentPanel ap = af.alignPanel;
487           if (headless)
488           {
489             Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
490                     StructureViewer.ViewerType.JMOL.toString());
491           }
492
493           String structureFilepath = structureFile.getAbsolutePath();
494
495           // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
496           String paeFilepath = subVals.getWithSubstitutions(argParser, id,
497                   "paematrix");
498           ArgValue paeAv = getArgAssociatedWithStructure(Arg.PAEMATRIX, avm,
499                   af, structureFilepath);
500           if (paeFilepath == null && paeAv != null)
501           {
502             SubVals sv = paeAv.getSubVals();
503             File paeFile = new File(sv.getContent());
504
505             try
506             {
507               paeFilepath = paeFile.getCanonicalPath();
508             } catch (IOException e)
509             {
510               paeFilepath = paeFile.getAbsolutePath();
511               Console.warn("Problem with the PAE file path: '"
512                       + paeFile.getPath() + "'");
513             }
514           }
515
516           // showing annotations from structure file or not
517           boolean ssFromStructure = ArgParser.getFromSubValArgOrPref(avm,
518                   Arg.SSANNOTATIONS, subVals, null, "STRUCT_FROM_PDB",
519                   true);
520
521           // get TEMPFAC type from subvals or Arg.TEMPFAC in case user Adds
522           // reference annotations
523           String tftString = subVals.get("tempfac");
524           TFType tft = avm.getBoolean(Arg.NOTEMPFAC) ? null
525                   : TFType.DEFAULT;
526           ArgValue tftAv = getArgAssociatedWithStructure(Arg.TEMPFAC, avm,
527                   af, structureFilepath);
528           if (tftString == null && tftAv != null)
529           {
530             tftString = tftAv.getSubVals().getContent();
531           }
532           if (tftString != null)
533           {
534             // get kind of temperature factor annotation
535             try
536             {
537               tft = TFType.valueOf(tftString.toUpperCase(Locale.ROOT));
538               Console.debug("Obtained Temperature Factor type of '" + tft
539                       + "' for structure '" + structureFilepath + "'");
540             } catch (IllegalArgumentException e)
541             {
542               // Just an error message!
543               StringBuilder sb = new StringBuilder().append("Cannot set ")
544                       .append(Arg.TEMPFAC.argString()).append(" to '")
545                       .append(tft)
546                       .append("', ignoring.  Valid values are: ");
547               Iterator<TFType> it = Arrays.stream(TFType.values())
548                       .iterator();
549               while (it.hasNext())
550               {
551                 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
552                 if (it.hasNext())
553                   sb.append(", ");
554               }
555               Console.warn(sb.toString());
556             }
557           }
558
559           String sViewer = ArgParser.getFromSubValArgOrPref(avm,
560                   Arg.STRUCTUREVIEWER, Position.AFTER, av, subVals, null,
561                   null, "jmol");
562           ViewerType viewerType = null;
563           if (!"none".equals(sViewer))
564           {
565             for (ViewerType v : EnumSet.allOf(ViewerType.class))
566             {
567               String name = v.name().toLowerCase(Locale.ROOT)
568                       .replaceAll(" ", "");
569               if (sViewer.equals(name))
570               {
571                 viewerType = v;
572                 break;
573               }
574             }
575           }
576
577           boolean addTempFac = tft != null
578                   || Cache.getDefault("ADD_TEMPFACT_ANN", false);
579
580           // TODO use ssFromStructure
581           StructureChooser.openStructureFileForSequence(null, null, ap, seq,
582                   false, structureFilepath, tft, paeFilepath, false,
583                   ssFromStructure, false, viewerType);
584         }
585       }
586     }
587
588     /*
589     boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
590     if (doShading)
591     {
592       AlignFrame af = afMap.get(id);
593       for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
594               .findAnnotation(PDBChain.class.getName().toString()))
595       {
596         AnnotationColourGradient acg = new AnnotationColourGradient(aa,
597                 af.alignPanel.av.getGlobalColourScheme(), 0);
598         acg.setSeqAssociated(true);
599         af.changeColour(acg);
600         Console.info("Changed colour " + acg.toString());
601       }
602     }
603     */
604
605     return theseArgsWereParsed;
606   }
607
608   protected void processGroovyScript(String id)
609   {
610     ArgValuesMap avm = argParser.getLinkedArgs(id);
611     AlignFrame af = afMap.get(id);
612
613     if (af == null)
614     {
615       Console.warn("Did not have an alignment window for id=" + id);
616       return;
617     }
618
619     if (avm.containsArg(Arg.GROOVY))
620     {
621       String groovyscript = avm.getValue(Arg.GROOVY);
622       if (groovyscript != null)
623       {
624         // Execute the groovy script after we've done all the rendering stuff
625         // and before any images or figures are generated.
626         Console.info("Executing script " + groovyscript);
627         Jalview.getInstance().executeGroovyScript(groovyscript, af);
628       }
629     }
630   }
631
632   protected boolean processImages(String id)
633   {
634     ArgValuesMap avm = argParser.getLinkedArgs(id);
635     AlignFrame af = afMap.get(id);
636
637     if (af == null)
638     {
639       Console.warn("Did not have an alignment window for id=" + id);
640       return false;
641     }
642
643     if (avm.containsArg(Arg.IMAGE))
644     {
645       for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
646       {
647         String val = av.getValue();
648         SubVals subVal = av.getSubVals();
649         String fileName = subVal.getContent();
650         File file = new File(fileName);
651         String name = af.getName();
652         String renderer = ArgParser.getValueFromSubValOrArg(avm, av,
653                 Arg.TEXTRENDERER, subVal);
654         if (renderer == null)
655           renderer = "text";
656         String type = "png"; // default
657         type = ArgParser.getValueFromSubValOrArg(avm, av, Arg.TYPE, subVal);
658         if (type == null && fileName != null)
659         {
660           for (String ext : new String[] { "svg", "png", "html", "eps" })
661           {
662             if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
663             {
664               type = ext;
665             }
666           }
667         }
668         // for moment we disable JSON export
669         Cache.setPropsAreReadOnly(true);
670         Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
671
672         switch (type)
673         {
674
675         case "svg":
676           Console.debug("Outputting type '" + type + "' to " + fileName);
677           af.createSVG(file, renderer);
678           break;
679
680         case "png":
681           Console.debug("Outputting type '" + type + "' to " + fileName);
682           af.createPNG(file);
683           break;
684
685         case "html":
686           Console.debug("Outputting type '" + type + "' to " + fileName);
687           HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
688           htmlSVG.exportHTML(fileName, renderer);
689           break;
690
691         case "biojs":
692           try
693           {
694             BioJsHTMLOutput.refreshVersionInfo(
695                     BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
696           } catch (URISyntaxException e)
697           {
698             e.printStackTrace();
699           }
700           BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
701           bjs.exportHTML(fileName);
702           Console.debug("Creating BioJS MSA Viwer HTML file: " + fileName);
703           break;
704
705         case "eps":
706           af.createEPS(file, name);
707           Console.debug("Creating EPS file: " + fileName);
708           break;
709
710         case "imagemap":
711           af.createImageMap(file, name);
712           Console.debug("Creating ImageMap file: " + fileName);
713           break;
714
715         default:
716           Console.warn(Arg.IMAGE.argString() + " type '" + type
717                   + "' not known. Ignoring");
718           break;
719         }
720       }
721     }
722     return true;
723   }
724
725   protected boolean processOutput(String id)
726   {
727     ArgValuesMap avm = argParser.getLinkedArgs(id);
728     AlignFrame af = afMap.get(id);
729
730     if (af == null)
731     {
732       Console.warn("Did not have an alignment window for id=" + id);
733       return false;
734     }
735
736     if (avm.containsArg(Arg.OUTPUT))
737     {
738       for (ArgValue av : avm.getArgValueList(Arg.OUTPUT))
739       {
740         String val = av.getValue();
741         SubVals subVals = av.getSubVals();
742         String fileName = subVals.getContent();
743         File file = new File(fileName);
744         boolean overwrite = ArgParser.getFromSubValArgOrPref(avm,
745                 Arg.OVERWRITE, subVals, null, "OVERWRITE_OUTPUT", false);
746         // backups. Use the Arg.BACKUPS or subval "backups" setting first,
747         // otherwise if headless assume false, if not headless use the user
748         // preference with default true.
749         boolean backups = ArgParser.getFromSubValArgOrPref(avm, Arg.BACKUPS,
750                 subVals, null,
751                 Platform.isHeadless() ? null : BackupFiles.ENABLED,
752                 !Platform.isHeadless());
753
754         // if backups is not true then --overwrite must be specified
755         if (file.exists() && !(overwrite || backups))
756         {
757           Console.error("Won't overwrite file '" + fileName + "' without "
758                   + Arg.OVERWRITE.argString() + " or "
759                   + Arg.BACKUPS.argString() + " set");
760           return false;
761         }
762
763         String name = af.getName();
764         String format = ArgParser.getValueFromSubValOrArg(avm, av,
765                 Arg.FORMAT, subVals);
766         FileFormats ffs = FileFormats.getInstance();
767         List<String> validFormats = ffs.getWritableFormats(false);
768
769         FileFormatI ff = null;
770         if (format == null && fileName != null)
771         {
772           FORMAT: for (String fname : validFormats)
773           {
774             FileFormatI tff = ffs.forName(fname);
775             String[] extensions = tff.getExtensions().split(",");
776             for (String ext : extensions)
777             {
778               if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
779               {
780                 ff = tff;
781                 format = ff.getName();
782                 break FORMAT;
783               }
784             }
785           }
786         }
787         if (ff == null && format != null)
788         {
789           ff = ffs.forName(format);
790         }
791         if (ff == null)
792         {
793           StringBuilder validSB = new StringBuilder();
794           for (String f : validFormats)
795           {
796             if (validSB.length() > 0)
797               validSB.append(", ");
798             validSB.append(f);
799             FileFormatI tff = ffs.forName(f);
800             validSB.append(" (");
801             validSB.append(tff.getExtensions());
802             validSB.append(")");
803           }
804
805           Jalview.exit("No valid format specified for "
806                   + Arg.OUTPUT.argString() + ". Valid formats are "
807                   + validSB.toString() + ".", 1);
808           // this return really shouldn't happen
809           return false;
810         }
811
812         String savedBackupsPreference = Cache
813                 .getDefault(BackupFiles.ENABLED, null);
814         Console.debug("Setting backups to " + backups);
815         Cache.applicationProperties.put(BackupFiles.ENABLED,
816                 Boolean.toString(backups));
817         af.saveAlignment(fileName, ff);
818         Console.debug("Returning backups to " + savedBackupsPreference);
819         if (savedBackupsPreference != null)
820           Cache.applicationProperties.put(BackupFiles.ENABLED,
821                   savedBackupsPreference);
822         if (af.isSaveAlignmentSuccessful())
823         {
824           Console.debug("Written alignment '" + name + "' in "
825                   + ff.getName() + " format to " + file);
826         }
827         else
828         {
829           Console.warn("Error writing file " + file + " in " + ff.getName()
830                   + " format!");
831         }
832
833       }
834     }
835     return true;
836   }
837
838   private SequenceI getSpecifiedSequence(AlignFrame af, ArgValuesMap avm,
839           ArgValue av)
840   {
841     SubVals subVals = av.getSubVals();
842     ArgValue idAv = avm.getClosestNextArgValueOfArg(av, Arg.SEQID);
843     SequenceI seq = null;
844     if (subVals == null && idAv == null)
845       return null;
846     AlignmentI al = af.getCurrentView().getAlignment();
847     if (al == null)
848       return null;
849     if (subVals != null)
850     {
851       if (subVals.has("seqid"))
852       {
853         seq = al.findName(subVals.get("seqid"));
854       }
855       else if (-1 < subVals.getIndex()
856               && subVals.getIndex() < al.getSequences().size())
857       {
858         seq = al.getSequenceAt(subVals.getIndex());
859       }
860     }
861     else if (idAv != null)
862     {
863       seq = al.findName(idAv.getValue());
864     }
865     return seq;
866   }
867
868   // returns the first Arg value intended for the structure structFilename
869   // (in the given AlignFrame from the ArgValuesMap)
870   private ArgValue getArgAssociatedWithStructure(Arg arg, ArgValuesMap avm,
871           AlignFrame af, String structFilename)
872   {
873     if (af != null)
874     {
875       for (ArgValue av : avm.getArgValueList(arg))
876       {
877         SubVals subVals = av.getSubVals();
878         String structid = subVals.get("structid");
879         String structfile = subVals.get("structfile");
880
881         // let's find a structure
882         if (structfile == null && structid == null)
883         {
884           ArgValue likelyStructure = avm.getClosestPreviousArgValueOfArg(av,
885                   Arg.STRUCTURE);
886           if (likelyStructure != null)
887           {
888             SubVals sv = likelyStructure.getSubVals();
889             if (sv != null && sv.has(ArgValues.ID))
890             {
891               structid = sv.get(ArgValues.ID);
892             }
893             else
894             {
895               structfile = likelyStructure.getValue();
896             }
897           }
898         }
899
900         if (structfile == null && structid != null)
901         {
902           StructureSelectionManager ssm = StructureSelectionManager
903                   .getStructureSelectionManager(Desktop.instance);
904           if (ssm != null)
905           {
906             structfile = ssm.findFileForPDBId(structid);
907           }
908         }
909         if (structfile != null && structfile.equals(structFilename))
910         {
911           return av;
912         }
913       }
914     }
915     return null;
916   }
917 }