+ if (progressBarSet && desktop != null)
+ desktop.setProgressBar(null, progress);
+
+ }
+
+ // open the structure (from same PDB file or given PDBfile)
+ if (!avm.getBoolean(Arg.NOSTRUCTURE))
+ {
+
+ AlignFrame af = afMap.get(id);
+ if (avm.containsArg(Arg.STRUCTURE))
+ {
+ commandArgsProvided = true;
+ for (
+
+ ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
+ {
+ argParser.setStructureFilename(null);
+ String val = av.getValue();
+ SubVals subVals = av.getSubVals();
+ int argIndex = av.getArgIndex();
+ SequenceI seq = getSpecifiedSequence(af, avm, av);
+ if (seq == null)
+ {
+ // Could not find sequence from subId, let's assume the first
+ // sequence in the alignframe
+ AlignmentI al = af.getCurrentView().getAlignment();
+ seq = al.getSequenceAt(0);
+ }
+
+ if (seq == null)
+ {
+ addWarn("Could not find sequence for argument "
+ + Arg.STRUCTURE.argString() + "=" + val);
+ continue;
+ }
+ String structureFilename = null;
+ File structureFile = null;
+ if (subVals.getContent() != null
+ && subVals.getContent().length() != 0)
+ {
+ structureFilename = subVals.getContent();
+ Console.debug("Using structure file (from argument) '"
+ + structureFilename + "'");
+ structureFile = new File(structureFilename);
+ }
+ /* THIS DOESN'T WORK */
+ else if (seq.getAllPDBEntries() != null
+ && seq.getAllPDBEntries().size() > 0)
+ {
+ structureFile = new File(
+ seq.getAllPDBEntries().elementAt(0).getFile());
+ if (structureFile != null)
+ {
+ Console.debug("Using structure file (from sequence) '"
+ + structureFile.getAbsolutePath() + "'");
+ }
+ structureFilename = structureFile.getAbsolutePath();
+ }
+
+ if (structureFilename == null || structureFile == null)
+ {
+ addWarn("Not provided structure file with '" + val + "'");
+ continue;
+ }
+
+ if (!structureFile.exists())
+ {
+ addWarn("Structure file '" + structureFile.getAbsoluteFile()
+ + "' not found.");
+ continue;
+ }
+
+ Console.debug("Using structure file "
+ + structureFile.getAbsolutePath());
+
+ argParser.setStructureFilename(structureFilename);
+
+ // open structure view
+ AlignmentPanel ap = af.alignPanel;
+ if (headless)
+ {
+ Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
+ StructureViewer.ViewerType.JMOL.toString());
+ }
+
+ String structureFilepath = structureFile.getAbsolutePath();
+
+ // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
+ String paeFilepath = avm.getFromSubValArgOrPrefWithSubstitutions(
+ argParser, Arg.PAEMATRIX, ArgValuesMap.Position.AFTER, av,
+ subVals, null, null, null);
+ if (paeFilepath != null)
+ {
+ File paeFile = new File(paeFilepath);
+
+ try
+ {
+ paeFilepath = paeFile.getCanonicalPath();
+ } catch (IOException e)
+ {
+ paeFilepath = paeFile.getAbsolutePath();
+ addWarn("Problem with the PAE file path: '"
+ + paeFile.getPath() + "'");
+ }
+ }
+
+ // showing annotations from structure file or not
+ boolean ssFromStructure = avm.getFromSubValArgOrPref(
+ Arg.SHOWSSANNOTATIONS, subVals, null, "STRUCT_FROM_PDB",
+ true);
+
+ // get TEMPFAC type from subvals or Arg.TEMPFAC in case user Adds
+ // reference annotations
+ String tftString = avm.getFromSubValArgOrPrefWithSubstitutions(
+ argParser, Arg.TEMPFAC, ArgValuesMap.Position.AFTER, av,
+ subVals, null, null, null);
+ boolean notempfac = avm.getFromSubValArgOrPref(Arg.NOTEMPFAC,
+ subVals, null, "ADD_TEMPFACT_ANN", false, true);
+ TFType tft = notempfac ? null : TFType.DEFAULT;
+ if (tftString != null && !notempfac)
+ {
+ // get kind of temperature factor annotation
+ try
+ {
+ tft = TFType.valueOf(tftString.toUpperCase(Locale.ROOT));
+ Console.debug("Obtained Temperature Factor type of '" + tft
+ + "' for structure '" + structureFilepath + "'");
+ } catch (IllegalArgumentException e)
+ {
+ // Just an error message!
+ StringBuilder sb = new StringBuilder().append("Cannot set ")
+ .append(Arg.TEMPFAC.argString()).append(" to '")
+ .append(tft)
+ .append("', ignoring. Valid values are: ");
+ Iterator<TFType> it = Arrays.stream(TFType.values())
+ .iterator();
+ while (it.hasNext())
+ {
+ sb.append(it.next().toString().toLowerCase(Locale.ROOT));
+ if (it.hasNext())
+ sb.append(", ");
+ }
+ addWarn(sb.toString());
+ }
+ }
+
+ String sViewerName = avm.getFromSubValArgOrPref(
+ Arg.STRUCTUREVIEWER, ArgValuesMap.Position.AFTER, av,
+ subVals, null, null, "jmol");
+ ViewerType viewerType = ViewerType.getFromString(sViewerName);
+
+ // TODO use ssFromStructure
+ StructureViewer structureViewer = StructureChooser
+ .openStructureFileForSequence(null, null, ap, seq, false,
+ structureFilepath, tft, paeFilepath, false,
+ ssFromStructure, false, viewerType);
+
+ if (structureViewer == null)
+ {
+ if (!StringUtils.equalsIgnoreCase(sViewerName, "none"))
+ {
+ addError("Failed to import and open structure view for file '"
+ + structureFile + "'.");
+ }
+ continue;
+ }
+ try
+ {
+ long tries = 1000;
+ while (structureViewer.isBusy() && tries > 0)
+ {
+ Thread.sleep(25);
+ if (structureViewer.isBusy())
+ {
+ tries--;
+ Console.debug(
+ "Waiting for viewer for " + structureFilepath);
+ }
+ }
+ if (tries == 0 && structureViewer.isBusy())
+ {
+ addWarn("Gave up waiting for structure viewer to load file '"
+ + structureFile
+ + "'. Something may have gone wrong.");
+ }
+ } catch (Exception x)
+ {
+ addError("Exception whilst waiting for structure viewer "
+ + structureFilepath, x);
+ isError = true;
+ }
+
+ // add StructureViewer to svMap list
+ if (svMap == null)
+ {
+ svMap = new HashMap<>();
+ }
+ if (svMap.get(id) == null)
+ {
+ svMap.put(id, new ArrayList<>());
+ }
+ svMap.get(id).add(structureViewer);
+
+ Console.debug(
+ "Successfully opened viewer for " + structureFilepath);
+
+ if (avm.containsArg(Arg.STRUCTUREIMAGE))
+ {
+ for (ArgValue structureImageArgValue : avm
+ .getArgValueList(Arg.STRUCTUREIMAGE))
+ {
+ String structureImageFilename = argParser.makeSubstitutions(
+ structureImageArgValue.getValue(), id, true);
+ if (structureViewer != null && structureImageFilename != null)
+ {
+ SubVals structureImageSubVals = null;
+ structureImageSubVals = structureImageArgValue.getSubVals();
+ File structureImageFile = new File(structureImageFilename);
+ String width = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.WIDTH,
+ structureImageSubVals);
+ String height = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.HEIGHT,
+ structureImageSubVals);
+ String scale = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.SCALE,
+ structureImageSubVals);
+ String renderer = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.TEXTRENDERER,
+ structureImageSubVals);
+ String typeS = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.TYPE,
+ structureImageSubVals);
+ if (typeS == null || typeS.length() == 0)
+ {
+ typeS = FileUtils.getExtension(structureImageFile);
+ }
+ TYPE imageType;
+ try
+ {
+ imageType = Enum.valueOf(TYPE.class,
+ typeS.toUpperCase(Locale.ROOT));
+ } catch (IllegalArgumentException e)
+ {
+ addWarn("Do not know image format '" + typeS
+ + "', using PNG");
+ imageType = TYPE.PNG;
+ }
+ BitmapImageSizing userBis = ImageMaker
+ .parseScaleWidthHeightStrings(scale, width, height);
+
+ /////
+ // DON'T TRY TO EXPORT IF VIEWER IS UNSUPPORTED
+ if (viewerType != ViewerType.JMOL)
+ {
+ addWarn("Cannot export image for structure viewer "
+ + viewerType.name() + " yet");
+ continue;
+ }
+
+ /////
+ // Apply the temporary colourscheme to the linked alignment
+ // TODO: enhance for multiple linked alignments.
+
+ String imageColour = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.IMAGECOLOUR,
+ structureImageSubVals);
+ ColourSchemeI originalColourScheme = this
+ .getColourScheme(af);
+ this.colourAlignFrame(af, imageColour);
+
+ /////
+ // custom image background colour
+
+ String bgcolourstring = avm.getValueFromSubValOrArg(
+ structureImageArgValue, Arg.BGCOLOUR,
+ structureImageSubVals);
+ Color bgcolour = null;
+ if (bgcolourstring != null && bgcolourstring.length() > 0)
+ {
+ bgcolour = ColorUtils.parseColourString(bgcolourstring);
+ if (bgcolour == null)
+ {
+ Console.warn(
+ "Background colour string '" + bgcolourstring
+ + "' not recognised -- using default");
+ }
+ }
+
+ JalviewStructureDisplayI sview = structureViewer
+ .getJalviewStructureDisplay();
+
+ File sessionToRestore = null;
+
+ List<StructureCommandI> extraCommands = new ArrayList<>();
+
+ if (extraCommands.size() > 0 || bgcolour != null)
+ {
+ try
+ {
+ sessionToRestore = sview.saveSession();
+ } catch (Throwable t)
+ {
+ Console.warn(
+ "Unable to save temporary session file before custom structure view export operation.");
+ }
+ }
+
+ ////
+ // Do temporary ops
+
+ if (bgcolour != null)
+ {
+ sview.getBinding().setBackgroundColour(bgcolour);
+ }
+
+ sview.getBinding().executeCommands(extraCommands, false,
+ "Executing Custom Commands");
+
+ // and export the view as an image
+ boolean success = this.checksBeforeWritingToFile(avm,
+ subVals, false, structureImageFilename,
+ "structure image", isError);
+
+ if (!success)
+ {
+ continue;
+ }
+ Console.debug("Rendering image to " + structureImageFile);
+ //
+ // TODO - extend StructureViewer / Binding with makePDBImage so
+ // we can do this with every viewer
+ //
+
+ try
+ {
+ // We don't expect class cast exception
+ AppJmol jmol = (AppJmol) sview;
+ jmol.makePDBImage(structureImageFile, imageType, renderer,
+ userBis);
+ Console.info("Exported structure image to "
+ + structureImageFile);
+
+ // RESTORE SESSION AFTER EXPORT IF NEED BE
+ if (sessionToRestore != null)
+ {
+ Console.debug(
+ "Restoring session from " + sessionToRestore);
+
+ sview.getBinding().restoreSession(
+ sessionToRestore.getAbsolutePath());
+
+ }
+ } catch (ImageOutputException ioexec)
+ {
+ addError(
+ "Unexpected error when restoring structure viewer session after custom view operations.");
+ isError = true;
+ continue;
+ } finally
+ {
+ try
+ {
+ this.colourAlignFrame(af, originalColourScheme);
+ } catch (Exception t)
+ {
+ addError(
+ "Unexpected error when restoring colourscheme to alignment after temporary change for export.",
+ t);
+ }
+ }
+ }
+ }
+ }
+ argParser.setStructureFilename(null);
+ }
+ }
+ }
+
+ if (wrap)
+ {
+
+ AlignFrame af = afMap.get(id);
+ if (af != null)
+ {
+ af.setWrapFormat(wrap, true);
+ }
+ }
+
+ /*
+ boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
+ if (doShading)
+ {
+ AlignFrame af = afMap.get(id);
+ for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
+ .findAnnotation(PDBChain.class.getName().toString()))
+ {
+ AnnotationColourGradient acg = new AnnotationColourGradient(aa,
+ af.alignPanel.av.getGlobalColourScheme(), 0);
+ acg.setSeqAssociated(true);
+ af.changeColour(acg);
+ Console.info("Changed colour " + acg.toString());
+ }
+ }
+ */
+
+ return theseArgsWereParsed && !isError;
+ }
+
+ protected void processGroovyScript(String id)
+ {
+ ArgValuesMap avm = argParser.getLinkedArgs(id);
+ AlignFrame af = afMap.get(id);
+
+ if (avm != null && !avm.containsArg(Arg.GROOVY))
+ {
+ // nothing to do
+ return;
+ }
+
+ if (af == null)
+ {
+ addWarn("Groovy script does not have an alignment window. Proceeding with caution!");
+ }
+
+ if (avm.containsArg(Arg.GROOVY))
+ {
+ for (ArgValue groovyAv : avm.getArgValueList(Arg.GROOVY))
+ {
+ String groovyscript = groovyAv.getValue();
+ if (groovyscript != null)
+ {
+ // Execute the groovy script after we've done all the rendering stuff
+ // and before any images or figures are generated.
+ Console.info("Executing script " + groovyscript);
+ Jalview.getInstance().executeGroovyScript(groovyscript, af);
+ }
+ }
+ }
+ }
+
+ protected boolean processImages(String id)
+ {
+ ArgValuesMap avm = argParser.getLinkedArgs(id);
+ AlignFrame af = afMap.get(id);
+
+ if (avm != null && !avm.containsArg(Arg.IMAGE))
+ {
+ // nothing to do
+ return true;
+ }
+
+ if (af == null)
+ {
+ addWarn("Do not have an alignment window to create image from (id="
+ + id + "). Not proceeding.");
+ return false;
+ }
+
+ Boolean isError = Boolean.valueOf(false);
+ if (avm.containsArg(Arg.IMAGE))
+ {
+ for (ArgValue imageAv : avm.getArgValueList(Arg.IMAGE))
+ {
+ String val = imageAv.getValue();
+ SubVals imageSubVals = imageAv.getSubVals();
+ String fileName = imageSubVals.getContent();
+ File file = new File(fileName);
+ String name = af.getName();
+ String renderer = avm.getValueFromSubValOrArg(imageAv,
+ Arg.TEXTRENDERER, imageSubVals);
+ if (renderer == null)
+ renderer = "text";
+ String type = "png"; // default
+
+ String scale = avm.getValueFromSubValOrArg(imageAv, Arg.SCALE,
+ imageSubVals);
+ String width = avm.getValueFromSubValOrArg(imageAv, Arg.WIDTH,
+ imageSubVals);
+ String height = avm.getValueFromSubValOrArg(imageAv, Arg.HEIGHT,
+ imageSubVals);
+ BitmapImageSizing userBis = ImageMaker
+ .parseScaleWidthHeightStrings(scale, width, height);
+
+ type = avm.getValueFromSubValOrArg(imageAv, Arg.TYPE, imageSubVals);
+ if (type == null && fileName != null)
+ {
+ for (String ext : new String[] { "svg", "png", "html", "eps" })
+ {
+ if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
+ {
+ type = ext;
+ }
+ }
+ }
+ // for moment we disable JSON export
+ Cache.setPropsAreReadOnly(true);
+ Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false");
+
+ String imageColour = avm.getValueFromSubValOrArg(imageAv,
+ Arg.IMAGECOLOUR, imageSubVals);
+ ColourSchemeI originalColourScheme = this.getColourScheme(af);
+ this.colourAlignFrame(af, imageColour);
+
+ Console.info("Writing " + file);
+
+ boolean success = checksBeforeWritingToFile(avm, imageSubVals,
+ false, fileName, "image", isError);
+ if (!success)
+ {
+ continue;
+ }
+
+ try
+ {
+ switch (type)
+ {
+
+ case "svg":
+ Console.debug("Outputting type '" + type + "' to " + fileName);
+ af.createSVG(file, renderer);
+ break;
+
+ case "png":
+ Console.debug("Outputting type '" + type + "' to " + fileName);
+ af.createPNG(file, null, userBis);
+ break;
+
+ case "html":
+ Console.debug("Outputting type '" + type + "' to " + fileName);
+ HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
+ htmlSVG.exportHTML(fileName, renderer);
+ break;
+
+ case "biojs":
+ Console.debug(
+ "Outputting BioJS MSA Viwer HTML file: " + fileName);
+ try
+ {
+ BioJsHTMLOutput.refreshVersionInfo(
+ BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
+ } catch (URISyntaxException e)
+ {
+ e.printStackTrace();
+ }
+ BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
+ bjs.exportHTML(fileName);
+ break;
+
+ case "eps":
+ Console.debug("Outputting EPS file: " + fileName);
+ af.createEPS(file, renderer);
+ break;
+
+ case "imagemap":
+ Console.debug("Outputting ImageMap file: " + fileName);
+ af.createImageMap(file, name);
+ break;
+
+ default:
+ addWarn(Arg.IMAGE.argString() + " type '" + type
+ + "' not known. Ignoring");
+ break;
+ }
+ } catch (Exception ioex)
+ {
+ addError("Unexpected error during export to '" + fileName + "'",
+ ioex);
+ isError = true;
+ }
+
+ this.colourAlignFrame(af, originalColourScheme);
+ }
+ }
+ return !isError;
+ }
+
+ protected boolean processOutput(String id)
+ {
+ ArgValuesMap avm = argParser.getLinkedArgs(id);
+ AlignFrame af = afMap.get(id);
+
+ if (avm != null && !avm.containsArg(Arg.OUTPUT))
+ {
+ // nothing to do
+ return true;
+ }
+
+ if (af == null)
+ {
+ addWarn("Do not have an alignment window (id=" + id
+ + "). Not proceeding.");
+ return false;
+ }
+
+ Boolean isError = Boolean.valueOf(false);
+
+ if (avm.containsArg(Arg.OUTPUT))
+ {
+ for (ArgValue av : avm.getArgValueList(Arg.OUTPUT))
+ {
+ String val = av.getValue();
+ SubVals subVals = av.getSubVals();
+ String fileName = subVals.getContent();
+ boolean stdout = ArgParser.STDOUTFILENAME.equals(fileName);
+ File file = new File(fileName);
+
+ String name = af.getName();
+ String format = avm.getValueFromSubValOrArg(av, Arg.FORMAT,
+ subVals);
+ FileFormats ffs = FileFormats.getInstance();
+ List<String> validFormats = ffs.getWritableFormats(false);
+
+ FileFormatI ff = null;
+ if (format == null && fileName != null)
+ {
+ FORMAT: for (String fname : validFormats)
+ {
+ FileFormatI tff = ffs.forName(fname);
+ String[] extensions = tff.getExtensions().split(",");
+ for (String ext : extensions)
+ {
+ if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext))
+ {
+ ff = tff;
+ format = ff.getName();
+ break FORMAT;
+ }
+ }
+ }
+ }
+ if (ff == null && format != null)
+ {
+ ff = ffs.forName(format);
+ }
+ if (ff == null)
+ {
+ if (stdout)
+ {
+ ff = FileFormat.Fasta;
+ }
+ else
+ {
+ StringBuilder validSB = new StringBuilder();
+ for (String f : validFormats)
+ {
+ if (validSB.length() > 0)
+ validSB.append(", ");
+ validSB.append(f);
+ FileFormatI tff = ffs.forName(f);
+ validSB.append(" (");
+ validSB.append(tff.getExtensions());
+ validSB.append(")");
+ }
+
+ addError("No valid format specified for "
+ + Arg.OUTPUT.argString() + ". Valid formats are "
+ + validSB.toString() + ".");
+ continue;
+ }
+ }
+
+ boolean success = checksBeforeWritingToFile(avm, subVals, true,
+ fileName, ff.getName(), isError);
+ if (!success)
+ {
+ continue;
+ }
+
+ boolean backups = avm.getFromSubValArgOrPref(Arg.BACKUPS, subVals,
+ null, Platform.isHeadless() ? null : BackupFiles.ENABLED,
+ !Platform.isHeadless());
+
+ Console.info("Writing " + fileName);
+
+ af.saveAlignment(fileName, ff, stdout, backups);
+ if (af.isSaveAlignmentSuccessful())
+ {
+ Console.debug("Written alignment '" + name + "' in "
+ + ff.getName() + " format to '" + file + "'");
+ }
+ else
+ {
+ addError("Error writing file '" + file + "' in " + ff.getName()
+ + " format!");
+ isError = true;
+ continue;
+ }
+
+ }
+ }
+ return !isError;
+ }
+
+ private SequenceI getSpecifiedSequence(AlignFrame af, ArgValuesMap avm,
+ ArgValue av)
+ {
+ SubVals subVals = av.getSubVals();
+ ArgValue idAv = avm.getClosestNextArgValueOfArg(av, Arg.SEQID, true);
+ SequenceI seq = null;
+ if (subVals == null && idAv == null)
+ return null;
+ if (af == null || af.getCurrentView() == null)
+ {
+ return null;
+ }
+ AlignmentI al = af.getCurrentView().getAlignment();
+ if (al == null)
+ {
+ return null;
+ }
+ if (subVals != null)
+ {
+ if (subVals.has(Arg.SEQID.getName()))
+ {
+ seq = al.findName(subVals.get(Arg.SEQID.getName()));
+ }
+ else if (-1 < subVals.getIndex()
+ && subVals.getIndex() < al.getSequences().size())
+ {
+ seq = al.getSequenceAt(subVals.getIndex());
+ }
+ }
+ if (seq == null && idAv != null)
+ {
+ seq = al.findName(idAv.getValue());
+ }
+ return seq;
+ }
+
+ public AlignFrame[] getAlignFrames()
+ {
+ AlignFrame[] afs = null;
+ if (afMap != null)
+ {
+ afs = (AlignFrame[]) afMap.values().toArray();
+ }
+
+ return afs;
+ }
+
+ public List<StructureViewer> getStructureViewers()
+ {
+ List<StructureViewer> svs = null;
+ if (svMap != null)
+ {
+ for (List<StructureViewer> svList : svMap.values())
+ {
+ if (svs == null)
+ {
+ svs = new ArrayList<>();
+ }
+ svs.addAll(svList);
+ }
+ }
+ return svs;
+ }
+
+ private void colourAlignFrame(AlignFrame af, String colour)
+ {
+ // use string "none" to remove colour scheme
+ if (colour != null && "" != colour)
+ {
+ ColourSchemeI cs = ColourSchemeProperty.getColourScheme(
+ af.getViewport(), af.getViewport().getAlignment(), colour);
+ if (cs == null && !StringUtils.equalsIgnoreCase(colour, "none"))
+ {
+ addWarn("Couldn't parse '" + colour + "' as a colourscheme.");
+ }