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.HashMap;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Locale;
15 import jalview.analysis.AlignmentUtils;
16 import jalview.api.structures.JalviewStructureDisplayI;
17 import jalview.bin.Jalview.ExitCode;
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 Map<String, List<StructureViewer>> svMap;
72 private boolean commandArgsProvided = false;
74 private boolean argsWereParsed = false;
76 private List<String> errors = new ArrayList<>();
78 public Commands(ArgParser argparser, boolean headless)
80 this(Desktop.instance, argparser, headless);
83 public Commands(Desktop d, ArgParser argparser, boolean h)
85 argParser = argparser;
88 afMap = new HashMap<>();
91 protected boolean processArgs()
93 if (argParser == null)
98 boolean theseArgsWereParsed = false;
100 if (argParser != null && argParser.getLinkedIds() != null)
102 for (String id : argParser.getLinkedIds())
104 ArgValuesMap avm = argParser.getLinkedArgs(id);
105 theseArgsWereParsed = true;
106 boolean processLinkedOkay = processLinked(id);
107 theseArgsWereParsed &= processLinkedOkay;
109 processGroovyScript(id);
111 // wait around until alignFrame isn't busy
112 AlignFrame af = afMap.get(id);
113 while (af != null && af.getViewport().isCalcInProgress())
118 } catch (Exception q)
124 theseArgsWereParsed &= processImages(id);
126 if (processLinkedOkay)
128 theseArgsWereParsed &= processOutput(id);
132 if (avm.getBoolean(Arg.CLOSE))
137 af.closeMenuItem_actionPerformed(true);
146 Console.debug("All errors from command line argument commands:\n"
148 // gui errors reported in Jalview
150 if (argParser.getBoolean(Arg.QUIT))
152 Jalview.getInstance().exit(
153 "Exiting due to " + Arg.QUIT.argString() + " argument.",
157 // carry on with jalview.bin.Jalview
158 argsWereParsed = theseArgsWereParsed;
159 return argsWereParsed;
162 public boolean commandArgsProvided()
164 return commandArgsProvided;
167 public boolean argsWereParsed()
169 return argsWereParsed;
172 protected boolean processLinked(String id)
174 boolean theseArgsWereParsed = false;
175 ArgValuesMap avm = argParser.getLinkedArgs(id);
181 boolean isError = false;
183 // set wrap scope here so it can be applied after structures are opened
184 boolean wrap = false;
186 if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN))
188 commandArgsProvided = true;
191 boolean first = true;
192 boolean progressBarSet = false;
194 // Combine the APPEND and OPEN files into one list, along with whether it
195 // was APPEND or OPEN
196 List<ArgValue> openAvList = new ArrayList<>();
197 openAvList.addAll(avm.getArgValueList(Arg.OPEN));
198 openAvList.addAll(avm.getArgValueList(Arg.APPEND));
199 // sort avlist based on av.getArgIndex()
200 Collections.sort(openAvList);
201 for (ArgValue av : openAvList)
204 SubVals sv = av.getSubVals();
205 String openFile = av.getValue();
206 if (openFile == null)
209 theseArgsWereParsed = true;
213 if (!headless && desktop != null)
215 desktop.setProgressBar(
216 MessageManager.getString(
217 "status.processing_commandline_args"),
218 progress = System.currentTimeMillis());
219 progressBarSet = true;
223 if (!Platform.isJS())
225 * ignore in JavaScript -- can't just file existence - could load it?
230 if (!HttpUtils.startsWithHttpOrHttps(openFile))
232 if (!(new File(openFile)).exists())
234 addError("Can't find file '" + openFile + "'");
241 DataSourceType protocol = AppletFormatAdapter
242 .checkProtocol(openFile);
244 FileFormatI format = null;
247 format = new IdentifyFile().identify(openFile, protocol);
248 } catch (FileFormatException e1)
250 addError("Unknown file format for '" + openFile + "'");
256 // When to open a new AlignFrame
257 if (af == null || "true".equals(av.getSubVal("new"))
258 || a == Arg.OPEN || format == FileFormat.Jalview)
262 Jalview.testoutput(argParser, Arg.OPEN, "examples/uniref50.fa",
267 "Opening '" + openFile + "' in new alignment frame");
268 FileLoader fileLoader = new FileLoader(!headless);
269 boolean xception = false;
272 af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
274 } catch (Throwable thr)
277 addError("Couldn't open '" + openFile + "' as " + format + " "
278 + thr.getLocalizedMessage()
279 + " (Enable debug for full stack trace)");
281 Console.debug("Exception when opening '" + openFile + "'", thr);
284 if (af == null && !xception)
286 addInfo("Ignoring '" + openFile
287 + "' - no alignment data found.");
293 String colour = ArgParser.getFromSubValArgOrPref(avm, av,
294 Arg.COLOUR, sv, null, "DEFAULT_COLOUR_PROT", "");
297 ColourSchemeI cs = ColourSchemeProperty.getColourScheme(
298 af.getViewport(), af.getViewport().getAlignment(),
301 if (cs == null && !"None".equals(colour))
303 addWarn("Couldn't parse '" + colour + "' as a colourscheme.");
309 Jalview.testoutput(argParser, Arg.COLOUR, "zappo", colour);
312 // Change alignment frame title
313 String title = ArgParser.getFromSubValArgOrPref(avm, av,
314 Arg.TITLE, sv, null, null, null);
318 Jalview.testoutput(argParser, Arg.TITLE, "test title", title);
322 String featuresfile = ArgParser.getValueFromSubValOrArg(avm, av,
324 if (featuresfile != null)
326 af.parseFeaturesFile(featuresfile,
327 AppletFormatAdapter.checkProtocol(featuresfile));
328 Jalview.testoutput(argParser, Arg.FEATURES,
329 "examples/testdata/plantfdx.features", featuresfile);
332 // Add annotations from file
333 String annotationsfile = ArgParser.getValueFromSubValOrArg(avm,
334 av, Arg.ANNOTATIONS, sv);
335 if (annotationsfile != null)
337 af.loadJalviewDataFile(annotationsfile, null, null, null);
338 Jalview.testoutput(argParser, Arg.ANNOTATIONS,
339 "examples/testdata/plantfdx.annotations",
343 // Set or clear the sortbytree flag
344 boolean sortbytree = ArgParser.getBoolFromSubValOrArg(avm,
348 af.getViewport().setSortByTree(true);
349 Jalview.testoutput(argParser, Arg.SORTBYTREE);
352 // Load tree from file
353 String treefile = ArgParser.getValueFromSubValOrArg(avm, av,
355 if (treefile != null)
359 NewickFile nf = new NewickFile(treefile,
360 AppletFormatAdapter.checkProtocol(treefile));
361 af.getViewport().setCurrentTree(
362 af.showNewickTree(nf, treefile).getTree());
363 Jalview.testoutput(argParser, Arg.TREE,
364 "examples/testdata/uniref50_test_tree", treefile);
365 } catch (IOException e)
367 addError("Couldn't add tree " + treefile, e);
372 // Show secondary structure annotations?
373 boolean showSSAnnotations = ArgParser.getFromSubValArgOrPref(avm,
374 Arg.SHOWSSANNOTATIONS, av.getSubVals(), null,
375 "STRUCT_FROM_PDB", true);
376 af.setAnnotationsVisibility(showSSAnnotations, true, false);
378 // Show sequence annotations?
379 boolean showAnnotations = ArgParser.getFromSubValArgOrPref(avm,
380 Arg.SHOWANNOTATIONS, av.getSubVals(), null,
381 "SHOW_ANNOTATIONS", true);
382 af.setAnnotationsVisibility(showAnnotations, false, true);
384 // show temperature factor annotations?
385 if (avm.getBoolean(Arg.NOTEMPFAC))
387 // do this better (annotation types?)
388 List<String> hideThese = new ArrayList<>();
389 hideThese.add("Temperature Factor");
390 hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
391 AlignmentUtils.showOrHideSequenceAnnotations(
392 af.getCurrentView().getAlignment(), hideThese, null,
396 // wrap alignment? do this last for formatting reasons
397 wrap = ArgParser.getFromSubValArgOrPref(avm, Arg.WRAP, sv, null,
398 "WRAP_ALIGNMENT", false);
399 // af.setWrapFormat(wrap) is applied after structures are opened for
400 // annotation reasons
402 // store the AlignFrame for this id
405 // is it its own structure file?
406 if (format.isStructureFile())
408 StructureSelectionManager ssm = StructureSelectionManager
409 .getStructureSelectionManager(Desktop.instance);
410 SequenceI seq = af.alignPanel.getAlignment().getSequenceAt(0);
411 ssm.computeMapping(false, new SequenceI[] { seq }, null,
412 openFile, DataSourceType.FILE, null, null, null, false);
418 "Opening '" + openFile + "' in existing alignment frame");
419 DataSourceType dst = HttpUtils.startsWithHttpOrHttps(openFile)
421 : DataSourceType.FILE;
422 FileLoader fileLoader = new FileLoader(!headless);
423 fileLoader.LoadFile(af.getCurrentView(), openFile, dst, null,
427 Console.debug("Command " + Arg.APPEND + " executed successfully!");
430 if (first) // first=true means nothing opened
434 Jalview.exit("Could not open any files in headless mode",
439 Console.info("No more files to open");
442 if (progressBarSet && desktop != null)
443 desktop.setProgressBar(null, progress);
447 // open the structure (from same PDB file or given PDBfile)
448 if (!avm.getBoolean(Arg.NOSTRUCTURE))
450 AlignFrame af = afMap.get(id);
451 if (avm.containsArg(Arg.STRUCTURE))
453 commandArgsProvided = true;
454 for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
456 String val = av.getValue();
457 SubVals subVals = av.getSubVals();
458 SequenceI seq = getSpecifiedSequence(af, avm, av);
461 // Could not find sequence from subId, let's assume the first
462 // sequence in the alignframe
463 AlignmentI al = af.getCurrentView().getAlignment();
464 seq = al.getSequenceAt(0);
469 addWarn("Could not find sequence for argument "
470 + Arg.STRUCTURE.argString() + "=" + val);
473 File structureFile = null;
474 if (subVals.getContent() != null
475 && subVals.getContent().length() != 0)
477 structureFile = new File(subVals.getContent());
478 Console.debug("Using structure file (from argument) '"
479 + structureFile.getAbsolutePath() + "'");
483 * PDBEntry fileEntry = new AssociatePdbFileWithSeq()
484 * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE,
485 * selectedSequence, true, Desktop.instance);
487 * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new
488 * SequenceI[] { selectedSequence });
491 /* THIS DOESN'T WORK */
492 else if (seq.getAllPDBEntries() != null
493 && seq.getAllPDBEntries().size() > 0)
495 structureFile = new File(
496 seq.getAllPDBEntries().elementAt(0).getFile());
497 Console.debug("Using structure file (from sequence) '"
498 + structureFile.getAbsolutePath() + "'");
501 if (structureFile == null)
503 addWarn("Not provided structure file with '" + val + "'");
507 if (!structureFile.exists())
509 addWarn("Structure file '" + structureFile.getAbsoluteFile()
514 Console.debug("Using structure file "
515 + structureFile.getAbsolutePath());
517 // open structure view
518 AlignmentPanel ap = af.alignPanel;
521 Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
522 StructureViewer.ViewerType.JMOL.toString());
525 String structureFilepath = structureFile.getAbsolutePath();
527 // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
528 String paeFilepath = ArgParser
529 .getFromSubValArgOrPrefWithSubstitutions(argParser, avm,
530 Arg.PAEMATRIX, Position.AFTER, av, subVals, null,
532 if (paeFilepath != null)
534 File paeFile = new File(paeFilepath);
538 paeFilepath = paeFile.getCanonicalPath();
539 } catch (IOException e)
541 paeFilepath = paeFile.getAbsolutePath();
542 addWarn("Problem with the PAE file path: '"
543 + paeFile.getPath() + "'");
547 // showing annotations from structure file or not
548 boolean ssFromStructure = ArgParser.getFromSubValArgOrPref(avm,
549 Arg.SHOWSSANNOTATIONS, subVals, null, "STRUCT_FROM_PDB",
552 // get TEMPFAC type from subvals or Arg.TEMPFAC in case user Adds
553 // reference annotations
554 String tftString = ArgParser
555 .getFromSubValArgOrPrefWithSubstitutions(argParser, avm,
556 Arg.TEMPFAC, Position.AFTER, av, subVals, null,
558 boolean notempfac = ArgParser.getFromSubValArgOrPref(avm,
559 Arg.NOTEMPFAC, subVals, null, "ADD_TEMPFACT_ANN", false,
561 TFType tft = notempfac ? null : TFType.DEFAULT;
562 if (tftString != null && !notempfac)
564 // get kind of temperature factor annotation
567 tft = TFType.valueOf(tftString.toUpperCase(Locale.ROOT));
568 Console.debug("Obtained Temperature Factor type of '" + tft
569 + "' for structure '" + structureFilepath + "'");
570 } catch (IllegalArgumentException e)
572 // Just an error message!
573 StringBuilder sb = new StringBuilder().append("Cannot set ")
574 .append(Arg.TEMPFAC.argString()).append(" to '")
576 .append("', ignoring. Valid values are: ");
577 Iterator<TFType> it = Arrays.stream(TFType.values())
581 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
585 addWarn(sb.toString());
589 String sViewer = ArgParser.getFromSubValArgOrPref(avm,
590 Arg.STRUCTUREVIEWER, Position.AFTER, av, subVals, null,
592 ViewerType viewerType = ViewerType.getFromString(sViewer);
594 // TODO use ssFromStructure
595 StructureViewer sv = StructureChooser
596 .openStructureFileForSequence(null, null, ap, seq, false,
597 structureFilepath, tft, paeFilepath, false,
598 ssFromStructure, false, viewerType);
602 addError("Failed to import and open structure view for file '"
603 + structureFile + "'.");
609 while (sv.isBusy() && tries > 0)
616 "Waiting for viewer for " + structureFilepath);
619 if (tries == 0 && sv.isBusy())
621 addWarn("Gave up waiting for structure viewer to load file '"
623 + "'. Something may have gone wrong.");
625 } catch (Exception x)
627 addError("Exception whilst waiting for structure viewer "
628 + structureFilepath, x);
632 // add StructureViewer to svMap list
635 svMap = new HashMap<>();
637 if (svMap.get(id) == null)
639 svMap.put(id, new ArrayList<>());
641 svMap.get(id).add(sv);
644 "Successfully opened viewer for " + structureFilepath);
645 String structureImageFilename = ArgParser.getValueFromSubValOrArg(
646 avm, av, Arg.STRUCTUREIMAGE, subVals);
647 if (sv != null && structureImageFilename != null)
649 ArgValue siAv = avm.getClosestNextArgValueOfArg(av,
652 if (structureImageFilename.equals(siAv.getValue()))
654 sisv = siAv.getSubVals();
656 File structureImageFile = new File(structureImageFilename);
657 String width = ArgParser.getValueFromSubValOrArg(avm, av,
658 Arg.STRUCTUREIMAGEWIDTH, sisv);
659 String height = ArgParser.getValueFromSubValOrArg(avm, av,
660 Arg.STRUCTUREIMAGEHEIGHT, sisv);
661 String scale = ArgParser.getValueFromSubValOrArg(avm, av,
662 Arg.STRUCTUREIMAGESCALE, sisv);
663 String renderer = ArgParser.getValueFromSubValOrArg(avm, av,
664 Arg.STRUCTUREIMAGETEXTRENDERER, sisv);
665 String typeS = ArgParser.getValueFromSubValOrArg(avm, av,
666 Arg.STRUCTUREIMAGETYPE, sisv);
667 if (typeS == null || typeS.length() == 0)
669 typeS = FileUtils.getExtension(structureImageFile);
674 imageType = Enum.valueOf(TYPE.class,
675 typeS.toUpperCase(Locale.ROOT));
676 } catch (IllegalArgumentException e)
678 addWarn("Do not know image format '" + typeS
680 imageType = TYPE.PNG;
682 BitmapImageSizing userBis = ImageMaker
683 .parseScaleWidthHeightStrings(scale, width, height);
684 // TODO MAKE THIS VIEWER INDEPENDENT!!
685 switch (StructureViewer.getViewerType())
688 JalviewStructureDisplayI sview = sv
689 .getJalviewStructureDisplay();
690 if (sview instanceof AppJmol)
692 AppJmol jmol = (AppJmol) sview;
695 whatNext wn = this.checksBeforeWritingToFile(avm, subVals,
696 false, structureImageFilename, "structure image");
697 if (wn == whatNext.ERROR)
702 else if (wn == whatNext.CONTINUE)
707 Console.debug("Rendering image to " + structureImageFile);
708 jmol.makePDBImage(structureImageFile, imageType, renderer,
710 Console.debug("Finished Rendering image to "
711 + structureImageFile);
713 } catch (ImageOutputException ioexc)
715 addError("Unexpected error whilst exporting image to "
716 + structureImageFile, ioexc);
724 addWarn("Cannot export image for structure viewer "
725 + sv.getViewerType() + " yet");
735 AlignFrame af = afMap.get(id);
738 af.setWrapFormat(wrap, true);
743 boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
746 AlignFrame af = afMap.get(id);
747 for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
748 .findAnnotation(PDBChain.class.getName().toString()))
750 AnnotationColourGradient acg = new AnnotationColourGradient(aa,
751 af.alignPanel.av.getGlobalColourScheme(), 0);
752 acg.setSeqAssociated(true);
753 af.changeColour(acg);
754 Console.info("Changed colour " + acg.toString());
759 return theseArgsWereParsed && !isError;
762 protected void processGroovyScript(String id)
764 ArgValuesMap avm = argParser.getLinkedArgs(id);
765 AlignFrame af = afMap.get(id);
769 addWarn("Did not have an alignment window for id=" + id);
773 if (avm.containsArg(Arg.GROOVY))
775 String groovyscript = avm.getValue(Arg.GROOVY);
776 if (groovyscript != null)
778 // Execute the groovy script after we've done all the rendering stuff
779 // and before any images or figures are generated.
780 Console.info("Executing script " + groovyscript);
781 Jalview.getInstance().executeGroovyScript(groovyscript, af);
786 protected boolean processImages(String id)
788 ArgValuesMap avm = argParser.getLinkedArgs(id);
789 AlignFrame af = afMap.get(id);
793 addWarn("Did not have an alignment window for id=" + id);
797 boolean isError = false;
798 if (avm.containsArg(Arg.IMAGE))
800 for (ArgValue av : avm.getArgValueList(Arg.IMAGE))
802 String val = av.getValue();
803 SubVals subVal = av.getSubVals();
804 String fileName = subVal.getContent();
805 File file = new File(fileName);
806 String name = af.getName();
807 String renderer = ArgParser.getValueFromSubValOrArg(avm, av,
808 Arg.TEXTRENDERER, subVal);
809 if (renderer == null)
811 String type = "png"; // default
813 String scale = ArgParser.getValueFromSubValOrArg(avm, av, Arg.SCALE,
815 String width = ArgParser.getValueFromSubValOrArg(avm, av, Arg.WIDTH,
817 String height = ArgParser.getValueFromSubValOrArg(avm, av,
819 BitmapImageSizing userBis = ImageMaker
820 .parseScaleWidthHeightStrings(scale, width, height);
822 type = ArgParser.getValueFromSubValOrArg(avm, av, Arg.TYPE, subVal);
823 if (type == null && fileName != null)
825 for (String ext : new String[] { "svg", "png", "html", "eps" })
827 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
833 // for moment we disable JSON export
834 Cache.setPropsAreReadOnly(true);
835 Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
837 Console.info("Writing " + file);
839 whatNext wn = this.checksBeforeWritingToFile(avm, subVal, false,
841 if (wn == whatNext.ERROR)
846 else if (wn == whatNext.CONTINUE)
857 Console.debug("Outputting type '" + type + "' to " + fileName);
858 af.createSVG(file, renderer);
862 Console.debug("Outputting type '" + type + "' to " + fileName);
863 af.createPNG(file, null, userBis);
867 Console.debug("Outputting type '" + type + "' to " + fileName);
868 HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
869 htmlSVG.exportHTML(fileName, renderer);
874 "Outputting BioJS MSA Viwer HTML file: " + fileName);
877 BioJsHTMLOutput.refreshVersionInfo(
878 BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
879 } catch (URISyntaxException e)
883 BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
884 bjs.exportHTML(fileName);
888 Console.debug("Outputting EPS file: " + fileName);
889 af.createEPS(file, renderer);
893 Console.debug("Outputting ImageMap file: " + fileName);
894 af.createImageMap(file, name);
898 addWarn(Arg.IMAGE.argString() + " type '" + type
899 + "' not known. Ignoring");
902 } catch (Exception ioex)
904 addError("Unexpected error during export to '" + fileName + "'",
913 protected boolean processOutput(String id)
915 ArgValuesMap avm = argParser.getLinkedArgs(id);
916 AlignFrame af = afMap.get(id);
920 addWarn("Did not have an alignment window for id=" + id);
924 boolean isError = false;
926 if (avm.containsArg(Arg.OUTPUT))
928 for (ArgValue av : avm.getArgValueList(Arg.OUTPUT))
930 String val = av.getValue();
931 SubVals subVals = av.getSubVals();
932 String fileName = subVals.getContent();
933 boolean stdout = ArgParser.STDOUTFILENAME.equals(fileName);
934 File file = new File(fileName);
936 String name = af.getName();
937 String format = ArgParser.getValueFromSubValOrArg(avm, av,
938 Arg.FORMAT, subVals);
939 FileFormats ffs = FileFormats.getInstance();
940 List<String> validFormats = ffs.getWritableFormats(false);
942 FileFormatI ff = null;
943 if (format == null && fileName != null)
945 FORMAT: for (String fname : validFormats)
947 FileFormatI tff = ffs.forName(fname);
948 String[] extensions = tff.getExtensions().split(",");
949 for (String ext : extensions)
951 if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
954 format = ff.getName();
960 if (ff == null && format != null)
962 ff = ffs.forName(format);
968 ff = FileFormat.Fasta;
972 StringBuilder validSB = new StringBuilder();
973 for (String f : validFormats)
975 if (validSB.length() > 0)
976 validSB.append(", ");
978 FileFormatI tff = ffs.forName(f);
979 validSB.append(" (");
980 validSB.append(tff.getExtensions());
984 addError("No valid format specified for "
985 + Arg.OUTPUT.argString() + ". Valid formats are "
986 + validSB.toString() + ".");
991 whatNext wn = this.checksBeforeWritingToFile(avm, subVals, true,
992 fileName, ff.getName());
993 if (wn == whatNext.ERROR)
998 else if (wn == whatNext.CONTINUE)
1003 boolean backups = ArgParser.getFromSubValArgOrPref(avm, Arg.BACKUPS,
1005 Platform.isHeadless() ? null : BackupFiles.ENABLED,
1006 !Platform.isHeadless());
1008 Console.info("Writing " + fileName);
1010 af.saveAlignment(fileName, ff, stdout, backups);
1011 if (af.isSaveAlignmentSuccessful())
1013 Console.debug("Written alignment '" + name + "' in "
1014 + ff.getName() + " format to '" + file + "'");
1018 addError("Error writing file '" + file + "' in " + ff.getName()
1029 private SequenceI getSpecifiedSequence(AlignFrame af, ArgValuesMap avm,
1032 SubVals subVals = av.getSubVals();
1033 ArgValue idAv = avm.getClosestNextArgValueOfArg(av, Arg.SEQID);
1034 SequenceI seq = null;
1035 if (subVals == null && idAv == null)
1037 if (af == null || af.getCurrentView() == null)
1041 AlignmentI al = af.getCurrentView().getAlignment();
1046 if (subVals != null)
1048 if (subVals.has(Arg.SEQID.getName()))
1050 seq = al.findName(subVals.get(Arg.SEQID.getName()));
1052 else if (-1 < subVals.getIndex()
1053 && subVals.getIndex() < al.getSequences().size())
1055 seq = al.getSequenceAt(subVals.getIndex());
1058 if (seq == null && idAv != null)
1060 seq = al.findName(idAv.getValue());
1065 public AlignFrame[] getAlignFrames()
1067 AlignFrame[] afs = null;
1070 afs = (AlignFrame[]) afMap.values().toArray();
1076 public List<StructureViewer> getStructureViewers()
1078 List<StructureViewer> svs = null;
1081 for (List<StructureViewer> svList : svMap.values())
1085 svs = new ArrayList<>();
1093 private void addInfo(String errorMessage)
1095 Console.info(errorMessage);
1096 errors.add(errorMessage);
1099 private void addWarn(String errorMessage)
1101 Console.warn(errorMessage);
1102 errors.add(errorMessage);
1105 private void addError(String errorMessage)
1107 addError(errorMessage, null);
1110 private void addError(String errorMessage, Exception e)
1112 Console.error(errorMessage, e);
1113 errors.add(errorMessage);
1116 private enum whatNext
1118 OKAY, CONTINUE, ERROR;
1121 private whatNext checksBeforeWritingToFile(ArgValuesMap avm,
1122 SubVals subVal, boolean includeBackups, String filename,
1125 File file = new File(filename);
1127 boolean overwrite = ArgParser.getFromSubValArgOrPref(avm, Arg.OVERWRITE,
1128 subVal, null, "OVERWRITE_OUTPUT", false);
1129 boolean stdout = false;
1130 boolean backups = false;
1133 stdout = ArgParser.STDOUTFILENAME.equals(filename);
1134 // backups. Use the Arg.BACKUPS or subval "backups" setting first,
1135 // otherwise if headless assume false, if not headless use the user
1136 // preference with default true.
1137 backups = ArgParser.getFromSubValArgOrPref(avm, Arg.BACKUPS, subVal,
1138 null, Platform.isHeadless() ? null : BackupFiles.ENABLED,
1139 !Platform.isHeadless());
1142 if (file.exists() && !(overwrite || backups || stdout))
1144 addWarn("Won't overwrite file '" + filename + "' without "
1145 + Arg.OVERWRITE.argString()
1146 + (includeBackups ? " or " + Arg.BACKUPS.argString() : "")
1148 return whatNext.CONTINUE;
1151 boolean mkdirs = ArgParser.getFromSubValArgOrPref(avm, Arg.MKDIRS,
1152 subVal, null, "MKDIRS_OUTPUT", false);
1154 if (!FileUtils.checkParentDir(file, mkdirs))
1156 addError("Directory '"
1157 + FileUtils.getParentDir(file).getAbsolutePath()
1158 + "' does not exist for " + adjective + " file '" + filename
1160 + (mkdirs ? "" : " Try using " + Arg.MKDIRS.argString()));
1161 return whatNext.ERROR;
1164 return whatNext.OKAY;
1167 public List<String> getErrors()
1172 public String errorsToString()
1174 StringBuilder sb = new StringBuilder();
1175 for (String error : errors)
1177 sb.append("- " + error);
1180 return sb.toString();