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;
16 import jalview.analysis.AlignmentUtils;
17 import jalview.api.structures.JalviewStructureDisplayI;
18 import jalview.bin.argparser.Arg;
19 import jalview.bin.argparser.ArgParser;
20 import jalview.bin.argparser.ArgParser.Position;
21 import jalview.bin.argparser.ArgValue;
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.AppJmol;
30 import jalview.gui.Desktop;
31 import jalview.gui.Preferences;
32 import jalview.gui.StructureChooser;
33 import jalview.gui.StructureViewer;
34 import jalview.gui.StructureViewer.ViewerType;
35 import jalview.io.AppletFormatAdapter;
36 import jalview.io.BackupFiles;
37 import jalview.io.BioJsHTMLOutput;
38 import jalview.io.DataSourceType;
39 import jalview.io.FileFormat;
40 import jalview.io.FileFormatException;
41 import jalview.io.FileFormatI;
42 import jalview.io.FileFormats;
43 import jalview.io.FileLoader;
44 import jalview.io.HtmlSvgOutput;
45 import jalview.io.IdentifyFile;
46 import jalview.io.NewickFile;
47 import jalview.io.exceptions.ImageOutputException;
48 import jalview.schemes.ColourSchemeI;
49 import jalview.schemes.ColourSchemeProperty;
50 import jalview.structure.StructureImportSettings.TFType;
51 import jalview.structure.StructureSelectionManager;
52 import jalview.util.FileUtils;
53 import jalview.util.HttpUtils;
54 import jalview.util.ImageMaker;
55 import jalview.util.ImageMaker.TYPE;
56 import jalview.util.MessageManager;
57 import jalview.util.Platform;
58 import jalview.util.imagemaker.BitmapImageSizing;
64 private boolean headless;
66 private ArgParser argParser;
68 private Map<String, AlignFrame> afMap;
70 private boolean commandArgsProvided = false;
72 private boolean argsWereParsed = false;
74 public Commands(ArgParser argparser, boolean headless)
76 this(Desktop.instance, argparser, headless);
79 public Commands(Desktop d, ArgParser argparser, boolean h)
81 argParser = argparser;
84 afMap = new HashMap<>();
85 if (argparser != null)
87 processArgs(argparser, headless);
91 private boolean processArgs(ArgParser argparser, boolean h)
93 argParser = argparser;
95 boolean theseArgsWereParsed = false;
97 if (argParser != null && argParser.getLinkedIds() != null)
99 for (String id : argParser.getLinkedIds())
101 ArgValuesMap avm = argParser.getLinkedArgs(id);
102 theseArgsWereParsed = true;
103 theseArgsWereParsed &= processLinked(id);
104 processGroovyScript(id);
105 boolean processLinkedOkay = theseArgsWereParsed;
107 // wait around until alignFrame isn't busy
108 AlignFrame af = afMap.get(id);
109 while (af != null && af.getViewport().isCalcInProgress())
114 } catch (Exception q)
120 theseArgsWereParsed &= processImages(id);
121 if (processLinkedOkay)
122 theseArgsWereParsed &= processOutput(id);
125 if (avm.getBoolean(Arg.CLOSE))
130 af.closeMenuItem_actionPerformed(true);
137 if (argParser.getBoolean(Arg.QUIT))
139 Jalview.getInstance().quit();
142 // carry on with jalview.bin.Jalview
143 argsWereParsed = theseArgsWereParsed;
144 return argsWereParsed;
147 public boolean commandArgsProvided()
149 return commandArgsProvided;
152 public boolean argsWereParsed()
154 return argsWereParsed;
157 protected boolean processUnlinked(String id)
159 return processLinked(id);
162 protected boolean processLinked(String id)
164 boolean theseArgsWereParsed = false;
165 ArgValuesMap avm = argParser.getLinkedArgs(id);
169 // set wrap scope here so it can be applied after structures are opened
170 boolean wrap = false;
172 if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN))
174 commandArgsProvided = true;
177 boolean first = true;
178 boolean progressBarSet = false;
180 // Combine the APPEND and OPEN files into one list, along with whether it
181 // was APPEND or OPEN
182 List<ArgValue> openAvList = new ArrayList<>();
183 openAvList.addAll(avm.getArgValueList(Arg.OPEN));
184 openAvList.addAll(avm.getArgValueList(Arg.APPEND));
185 // sort avlist based on av.getArgIndex()
186 Collections.sort(openAvList);
187 for (ArgValue av : openAvList)
190 SubVals sv = av.getSubVals();
191 String openFile = av.getValue();
192 if (openFile == null)
195 theseArgsWereParsed = true;
199 if (!headless && desktop != null)
201 desktop.setProgressBar(
202 MessageManager.getString(
203 "status.processing_commandline_args"),
204 progress = System.currentTimeMillis());
205 progressBarSet = true;
209 if (!Platform.isJS())
211 * ignore in JavaScript -- can't just file existence - could load it?
216 if (!HttpUtils.startsWithHttpOrHttps(openFile))
218 if (!(new File(openFile)).exists())
220 Console.warn("Can't find file '" + openFile + "'");
225 DataSourceType protocol = AppletFormatAdapter
226 .checkProtocol(openFile);
228 FileFormatI format = null;
231 format = new IdentifyFile().identify(openFile, protocol);
232 } catch (FileFormatException e1)
234 Console.error("Unknown file format for '" + openFile + "'");
238 // When to open a new AlignFrame
239 if (af == null || "true".equals(av.getSubVal("new"))
240 || a == Arg.OPEN || format == FileFormat.Jalview)
244 Jalview.testoutput(argParser, Arg.OPEN, "examples/uniref50.fa",
249 "Opening '" + openFile + "' in new alignment frame");
250 FileLoader fileLoader = new FileLoader(!headless);
252 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
256 String colour = ArgParser.getFromSubValArgOrPref(avm, av,
257 Arg.COLOUR, sv, null, "DEFAULT_COLOUR_PROT", "");
260 ColourSchemeI cs = ColourSchemeProperty.getColourScheme(
261 af.getViewport(), af.getViewport().getAlignment(),
264 if (cs == null && !"None".equals(colour))
267 "Couldn't parse '" + colour + "' as a colourscheme.");
273 Jalview.testoutput(argParser, Arg.COLOUR, "zappo", colour);
276 // Change alignment frame title
277 String title = ArgParser.getFromSubValArgOrPref(avm, av,
278 Arg.TITLE, sv, null, null, null);
282 Jalview.testoutput(argParser, Arg.TITLE, "test title", title);
286 String featuresfile = ArgParser.getValueFromSubValOrArg(avm, av,
288 if (featuresfile != null)
290 af.parseFeaturesFile(featuresfile,
291 AppletFormatAdapter.checkProtocol(featuresfile));
292 Jalview.testoutput(argParser, Arg.FEATURES,
293 "examples/testdata/plantfdx.features", featuresfile);
296 // Add annotations from file
297 String annotationsfile = ArgParser.getValueFromSubValOrArg(avm,
298 av, Arg.ANNOTATIONS, sv);
299 if (annotationsfile != null)
301 af.loadJalviewDataFile(annotationsfile, null, null, null);
302 Jalview.testoutput(argParser, Arg.ANNOTATIONS,
303 "examples/testdata/plantfdx.annotations",
307 // Set or clear the sortbytree flag
308 boolean sortbytree = ArgParser.getBoolFromSubValOrArg(avm,
312 af.getViewport().setSortByTree(true);
313 Jalview.testoutput(argParser, Arg.SORTBYTREE);
316 // Load tree from file
317 String treefile = ArgParser.getValueFromSubValOrArg(avm, av,
319 if (treefile != null)
323 NewickFile nf = new NewickFile(treefile,
324 AppletFormatAdapter.checkProtocol(treefile));
325 af.getViewport().setCurrentTree(
326 af.showNewickTree(nf, treefile).getTree());
327 Jalview.testoutput(argParser, Arg.TREE,
328 "examples/testdata/uniref50_test_tree", treefile);
329 } catch (IOException e)
331 Console.warn("Couldn't add tree " + treefile, e);
335 // Show secondary structure annotations?
336 boolean showSSAnnotations = ArgParser.getFromSubValArgOrPref(avm,
337 Arg.SHOWSSANNOTATIONS, av.getSubVals(), null,
338 "STRUCT_FROM_PDB", true);
339 af.setAnnotationsVisibility(showSSAnnotations, true, false);
341 // Show sequence annotations?
342 boolean showAnnotations = ArgParser.getFromSubValArgOrPref(avm,
343 Arg.SHOWANNOTATIONS, av.getSubVals(), null,
344 "SHOW_ANNOTATIONS", true);
345 af.setAnnotationsVisibility(showAnnotations, false, true);
347 // show temperature factor annotations?
348 if (avm.getBoolean(Arg.NOTEMPFAC))
350 // do this better (annotation types?)
351 List<String> hideThese = new ArrayList<>();
352 hideThese.add("Temperature Factor");
353 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
354 AlignmentUtils.showOrHideSequenceAnnotations(
355 af.getCurrentView().getAlignment(), hideThese, null,
359 // wrap alignment? do this last for formatting reasons
360 wrap = ArgParser.getFromSubValArgOrPref(avm, Arg.WRAP, sv, null,
361 "WRAP_ALIGNMENT", false);
362 // af.setWrapFormat(wrap) is applied after structures are opened for
363 // annotation reasons
365 // store the AlignFrame for this id
368 // is it its own structure file?
369 if (format.isStructureFile())
371 StructureSelectionManager ssm = StructureSelectionManager
372 .getStructureSelectionManager(Desktop.instance);
373 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
374 ssm.computeMapping(false, new SequenceI[] { seq }, null,
375 openFile, DataSourceType.FILE, null, null, null, false);
381 "Opening '" + openFile + "' in existing alignment frame");
382 DataSourceType dst = HttpUtils.startsWithHttpOrHttps(openFile)
384 : DataSourceType.FILE;
385 FileLoader fileLoader = new FileLoader(!headless);
386 fileLoader.LoadFile(af.getCurrentView(), openFile, dst, null,
390 Console.debug("Command " + Arg.APPEND + " executed successfully!");
393 if (first) // first=true means nothing opened
397 Jalview.exit("Could not open any files in headless mode", 1);
401 Console.warn("No more files to open");
404 if (progressBarSet && desktop != null)
405 desktop.setProgressBar(null, progress);
409 // open the structure (from same PDB file or given PDBfile)
410 if (!avm.getBoolean(Arg.NOSTRUCTURE))
412 AlignFrame af = afMap.get(id);
413 if (avm.containsArg(Arg.STRUCTURE))
415 commandArgsProvided = true;
416 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
418 String val = av.getValue();
419 SubVals subVals = av.getSubVals();
420 SequenceI seq = getSpecifiedSequence(af, avm, av);
423 // Could not find sequence from subId, let's assume the first
424 // sequence in the alignframe
425 AlignmentI al = af.getCurrentView().getAlignment();
426 seq = al.getSequenceAt(0);
431 Console.warn("Could not find sequence for argument "
432 + Arg.STRUCTURE.argString() + "=" + val);
433 // you probably want to continue here, not break
437 File structureFile = null;
438 if (subVals.getContent() != null
439 && subVals.getContent().length() != 0)
441 structureFile = new File(subVals.getContent());
442 Console.debug("Using structure file (from argument) '"
443 + structureFile.getAbsolutePath() + "'");
447 * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
448 * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE,
449 * selectedSequence, true, Desktop.instance);
451 * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new
452 * SequenceI[] { selectedSequence });
455 /* THIS DOESN'T WORK */
456 else if (seq.getAllPDBEntries() != null
457 && seq.getAllPDBEntries().size() > 0)
459 structureFile = new File(
460 seq.getAllPDBEntries().elementAt(0).getFile());
461 Console.debug("Using structure file (from sequence) '"
462 + structureFile.getAbsolutePath() + "'");
465 if (structureFile == null)
467 Console.warn("Not provided structure file with '" + val + "'");
471 if (!structureFile.exists())
473 Console.warn("Structure file '"
474 + structureFile.getAbsoluteFile() + "' not found.");
478 Console.debug("Using structure file "
479 + structureFile.getAbsolutePath());
481 // open structure view
482 AlignmentPanel ap = af.alignPanel;
485 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
486 StructureViewer.ViewerType.JMOL.toString());
489 String structureFilepath = structureFile.getAbsolutePath();
491 // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
492 String paeFilepath = ArgParser
493 .getFromSubValArgOrPrefWithSubstitutions(argParser, avm,
494 Arg.PAEMATRIX, Position.AFTER, av, subVals, null,
496 if (paeFilepath != null)
498 File paeFile = new File(paeFilepath);
502 paeFilepath = paeFile.getCanonicalPath();
503 } catch (IOException e)
505 paeFilepath = paeFile.getAbsolutePath();
506 Console.warn("Problem with the PAE file path: '"
507 + paeFile.getPath() + "'");
511 // showing annotations from structure file or not
512 boolean ssFromStructure = ArgParser.getFromSubValArgOrPref(avm,
513 Arg.SHOWSSANNOTATIONS, subVals, null, "STRUCT_FROM_PDB",
516 // get TEMPFAC type from subvals or Arg.TEMPFAC in case user Adds
517 // reference annotations
518 String tftString = ArgParser
519 .getFromSubValArgOrPrefWithSubstitutions(argParser, avm,
520 Arg.TEMPFAC, Position.AFTER, av, subVals, null,
522 boolean notempfac = ArgParser.getFromSubValArgOrPref(avm,
523 Arg.NOTEMPFAC, subVals, null, "ADD_TEMPFACT_ANN", false,
525 TFType tft = notempfac ? null : TFType.DEFAULT;
526 if (tftString != null && !notempfac)
528 // get kind of temperature factor annotation
531 tft = TFType.valueOf(tftString.toUpperCase(Locale.ROOT));
532 Console.debug("Obtained Temperature Factor type of '" + tft
533 + "' for structure '" + structureFilepath + "'");
534 } catch (IllegalArgumentException e)
536 // Just an error message!
537 StringBuilder sb = new StringBuilder().append("Cannot set ")
538 .append(Arg.TEMPFAC.argString()).append(" to '")
540 .append("', ignoring. Valid values are: ");
541 Iterator<TFType> it = Arrays.stream(TFType.values())
545 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
549 Console.warn(sb.toString());
553 String sViewer = ArgParser.getFromSubValArgOrPref(avm,
554 Arg.STRUCTUREVIEWER, Position.AFTER, av, subVals, null,
556 ViewerType viewerType = null;
557 if (!"none".equals(sViewer))
559 for (ViewerType v : EnumSet.allOf(ViewerType.class))
561 String name = v.name().toLowerCase(Locale.ROOT)
562 .replaceAll(" ", "");
563 if (sViewer.equals(name))
571 // TODO use ssFromStructure
572 StructureViewer sv = StructureChooser
573 .openStructureFileForSequence(null, null, ap, seq, false,
574 structureFilepath, tft, paeFilepath, false,
575 ssFromStructure, false, viewerType);
579 Console.error("Failed to import and open structure view.");
585 while (sv.isBusy() && tries > 0)
592 "Waiting for viewer for " + structureFilepath);
595 if (tries == 0 && sv.isBusy())
598 "Gave up waiting for structure viewer to load. Something may have gone wrong.");
600 } catch (Exception x)
602 Console.warn("Exception whilst waiting for structure viewer "
603 + structureFilepath, x);
606 "Successfully opened viewer for " + structureFilepath);
607 String structureImageFilename = ArgParser.getValueFromSubValOrArg(
608 avm, av, Arg.STRUCTUREIMAGE, subVals);
609 if (sv != null && structureImageFilename != null)
611 ArgValue siAv = avm.getClosestNextArgValueOfArg(av,
614 if (structureImageFilename.equals(siAv.getValue()))
616 sisv = siAv.getSubVals();
618 File structureImageFile = new File(structureImageFilename);
619 String width = ArgParser.getValueFromSubValOrArg(avm, av,
620 Arg.STRUCTUREIMAGEWIDTH, sisv);
621 String height = ArgParser.getValueFromSubValOrArg(avm, av,
622 Arg.STRUCTUREIMAGEHEIGHT, sisv);
623 String scale = ArgParser.getValueFromSubValOrArg(avm, av,
624 Arg.STRUCTUREIMAGESCALE, sisv);
625 String renderer = ArgParser.getValueFromSubValOrArg(avm, av,
626 Arg.STRUCTUREIMAGETEXTRENDERER, sisv);
627 String typeS = ArgParser.getValueFromSubValOrArg(avm, av,
628 Arg.STRUCTUREIMAGETYPE, sisv);
629 if (typeS == null || typeS.length() == 0)
631 typeS = FileUtils.getExtension(structureImageFile);
636 imageType = Enum.valueOf(TYPE.class,
637 typeS.toUpperCase(Locale.ROOT));
638 } catch (IllegalArgumentException e)
640 Console.warn("Do not know image format '" + typeS
642 imageType = TYPE.PNG;
644 BitmapImageSizing userBis = ImageMaker
645 .parseScaleWidthHeightStrings(scale, width, height);
646 // TODO MAKE THIS VIEWER INDEPENDENT!!
647 switch (StructureViewer.getViewerType())
652 Thread.sleep(1000); // WHY ???
653 } catch (InterruptedException e)
655 // TODO Auto-generated catch block
658 JalviewStructureDisplayI sview = sv
659 .getJalviewStructureDisplay();
660 if (sview instanceof AppJmol)
662 AppJmol jmol = (AppJmol) sview;
665 Console.debug("Rendering image to " + structureImageFile);
666 jmol.makePDBImage(structureImageFile, imageType, renderer,
668 Console.debug("Finished Rendering image to "
669 + structureImageFile);
671 } catch (ImageOutputException ioexc)
673 Console.warn("Unexpected error whilst exporting image to "
674 + structureImageFile, ioexc);
680 Console.warn("Cannot export image for structure viewer "
681 + sv.getViewerType() + " yet");
691 AlignFrame af = afMap.get(id);
694 af.setWrapFormat(wrap);
699 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
702 AlignFrame af = afMap.get(id);
703 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
704 .findAnnotation(PDBChain.class.getName().toString()))
706 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
707 af.alignPanel.av.getGlobalColourScheme(), 0);
708 acg.setSeqAssociated(true);
709 af.changeColour(acg);
710 Console.info("Changed colour " + acg.toString());
715 return theseArgsWereParsed;
718 protected void processGroovyScript(String id)
720 ArgValuesMap avm = argParser.getLinkedArgs(id);
721 AlignFrame af = afMap.get(id);
725 Console.warn("Did not have an alignment window for id=" + id);
729 if (avm.containsArg(Arg.GROOVY))
731 String groovyscript = avm.getValue(Arg.GROOVY);
732 if (groovyscript != null)
734 // Execute the groovy script after we've done all the rendering stuff
735 // and before any images or figures are generated.
736 Console.info("Executing script " + groovyscript);
737 Jalview.getInstance().executeGroovyScript(groovyscript, af);
742 protected boolean processImages(String id)
744 ArgValuesMap avm = argParser.getLinkedArgs(id);
745 AlignFrame af = afMap.get(id);
749 Console.warn("Did not have an alignment window for id=" + id);
753 if (avm.containsArg(Arg.IMAGE))
755 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
757 String val = av.getValue();
758 SubVals subVal = av.getSubVals();
759 String fileName = subVal.getContent();
760 File file = new File(fileName);
761 String name = af.getName();
762 String renderer = ArgParser.getValueFromSubValOrArg(avm, av,
763 Arg.TEXTRENDERER, subVal);
764 if (renderer == null)
766 String type = "png"; // default
768 String scale = ArgParser.getValueFromSubValOrArg(avm, av, Arg.SCALE,
770 String width = ArgParser.getValueFromSubValOrArg(avm, av, Arg.WIDTH,
772 String height = ArgParser.getValueFromSubValOrArg(avm, av,
774 BitmapImageSizing userBis = ImageMaker
775 .parseScaleWidthHeightStrings(scale, width, height);
777 type = ArgParser.getValueFromSubValOrArg(avm, av, Arg.TYPE, subVal);
778 if (type == null && fileName != null)
780 for (String ext : new String[] { "svg", "png", "html", "eps" })
782 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
788 // for moment we disable JSON export
789 Cache.setPropsAreReadOnly(true);
790 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
792 Console.info("Writing " + file);
799 Console.debug("Outputting type '" + type + "' to " + fileName);
800 af.createSVG(file, renderer);
804 Console.debug("Outputting type '" + type + "' to " + fileName);
805 af.createPNG(file, null, userBis);
809 Console.debug("Outputting type '" + type + "' to " + fileName);
810 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
811 htmlSVG.exportHTML(fileName, renderer);
816 "Creating BioJS MSA Viwer HTML file: " + fileName);
819 BioJsHTMLOutput.refreshVersionInfo(
820 BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
821 } catch (URISyntaxException e)
825 BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
826 bjs.exportHTML(fileName);
830 Console.debug("Creating EPS file: " + fileName);
831 af.createEPS(file, name);
835 Console.debug("Creating ImageMap file: " + fileName);
836 af.createImageMap(file, name);
840 Console.warn(Arg.IMAGE.argString() + " type '" + type
841 + "' not known. Ignoring");
844 } catch (Exception ioex)
846 Console.warn("Unexpected error during export", ioex);
853 protected boolean processOutput(String id)
855 ArgValuesMap avm = argParser.getLinkedArgs(id);
856 AlignFrame af = afMap.get(id);
860 Console.warn("Did not have an alignment window for id=" + id);
864 if (avm.containsArg(Arg.OUTPUT))
866 for (ArgValue av : avm.getArgValueList(Arg.OUTPUT))
868 String val = av.getValue();
869 SubVals subVals = av.getSubVals();
870 String fileName = subVals.getContent();
871 File file = new File(fileName);
872 boolean overwrite = ArgParser.getFromSubValArgOrPref(avm,
873 Arg.OVERWRITE, subVals, null, "OVERWRITE_OUTPUT", false);
874 // backups. Use the Arg.BACKUPS or subval "backups" setting first,
875 // otherwise if headless assume false, if not headless use the user
876 // preference with default true.
877 boolean backups = ArgParser.getFromSubValArgOrPref(avm, Arg.BACKUPS,
879 Platform.isHeadless() ? null : BackupFiles.ENABLED,
880 !Platform.isHeadless());
882 // if backups is not true then --overwrite must be specified
883 if (file.exists() && !(overwrite || backups))
885 Console.error("Won't overwrite file '" + fileName + "' without "
886 + Arg.OVERWRITE.argString() + " or "
887 + Arg.BACKUPS.argString() + " set");
891 String name = af.getName();
892 String format = ArgParser.getValueFromSubValOrArg(avm, av,
893 Arg.FORMAT, subVals);
894 FileFormats ffs = FileFormats.getInstance();
895 List<String> validFormats = ffs.getWritableFormats(false);
897 FileFormatI ff = null;
898 if (format == null && fileName != null)
900 FORMAT: for (String fname : validFormats)
902 FileFormatI tff = ffs.forName(fname);
903 String[] extensions = tff.getExtensions().split(",");
904 for (String ext : extensions)
906 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
909 format = ff.getName();
915 if (ff == null && format != null)
917 ff = ffs.forName(format);
921 StringBuilder validSB = new StringBuilder();
922 for (String f : validFormats)
924 if (validSB.length() > 0)
925 validSB.append(", ");
927 FileFormatI tff = ffs.forName(f);
928 validSB.append(" (");
929 validSB.append(tff.getExtensions());
933 Jalview.exit("No valid format specified for "
934 + Arg.OUTPUT.argString() + ". Valid formats are "
935 + validSB.toString() + ".", 1);
936 // this return really shouldn't happen
940 String savedBackupsPreference = Cache
941 .getDefault(BackupFiles.ENABLED, null);
942 Console.debug("Setting backups to " + backups);
943 Cache.applicationProperties.put(BackupFiles.ENABLED,
944 Boolean.toString(backups));
946 Console.info("Writing " + fileName);
948 af.saveAlignment(fileName, ff);
949 Console.debug("Returning backups to " + savedBackupsPreference);
950 if (savedBackupsPreference != null)
951 Cache.applicationProperties.put(BackupFiles.ENABLED,
952 savedBackupsPreference);
953 if (af.isSaveAlignmentSuccessful())
955 Console.debug("Written alignment '" + name + "' in "
956 + ff.getName() + " format to " + file);
960 Console.warn("Error writing file " + file + " in " + ff.getName()
969 private SequenceI getSpecifiedSequence(AlignFrame af, ArgValuesMap avm,
972 SubVals subVals = av.getSubVals();
973 ArgValue idAv = avm.getClosestNextArgValueOfArg(av, Arg.SEQID);
974 SequenceI seq = null;
975 if (subVals == null && idAv == null)
977 if (af == null || af.getCurrentView() == null)
981 AlignmentI al = af.getCurrentView().getAlignment();
988 if (subVals.has(Arg.SEQID.getName()))
990 seq = al.findName(subVals.get(Arg.SEQID.getName()));
992 else if (-1 < subVals.getIndex()
993 && subVals.getIndex() < al.getSequences().size())
995 seq = al.getSequenceAt(subVals.getIndex());
998 else if (idAv != null)
1000 seq = al.findName(idAv.getValue());