X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fjmol%2FJmolCommands.java;h=25f6aecbb795b1d0361489612ec35847f9122515;hb=d46c8cbd1a5282ede9a0a94471f23ca002cf7df6;hp=bd449217f347a6f4a7f3d2069c427a68186bc65c;hpb=42f4227ed213d422a87d3b22fc9e85d14ffaf53f;p=jalview.git diff --git a/src/jalview/ext/jmol/JmolCommands.java b/src/jalview/ext/jmol/JmolCommands.java index bd44921..25f6aec 100644 --- a/src/jalview/ext/jmol/JmolCommands.java +++ b/src/jalview/ext/jmol/JmolCommands.java @@ -20,6 +20,12 @@ */ package jalview.ext.jmol; +import java.awt.Color; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureRenderer; @@ -35,12 +41,7 @@ import jalview.structure.StructureCommandsBase; import jalview.structure.StructureMapping; import jalview.structure.StructureSelectionManager; import jalview.util.Comparison; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import jalview.util.Platform; /** * Routines for generating Jmol commands for Jalview/Jmol binding @@ -98,160 +99,6 @@ public class JmolCommands extends StructureCommandsBase c.getBlue()); } - - public String[] colourBySequence(StructureSelectionManager ssm, - String[] files, - SequenceI[][] sequence, SequenceRenderer sr, - AlignmentViewPanel viewPanel) - { - // TODO delete method - - FeatureRenderer fr = viewPanel.getFeatureRenderer(); - FeatureColourFinder finder = new FeatureColourFinder(fr); - AlignViewportI viewport = viewPanel.getAlignViewport(); - HiddenColumns cs = viewport.getAlignment().getHiddenColumns(); - AlignmentI al = viewport.getAlignment(); - List cset = new ArrayList<>(); - - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); - StringBuilder command = new StringBuilder(128); - List str = new ArrayList<>(); - - if (mapping == null || mapping.length < 1) - { - continue; - } - - for (int s = 0; s < sequence[pdbfnum].length; s++) - { - for (int sp, m = 0; m < mapping.length; m++) - { - if (mapping[m].getSequence() == sequence[pdbfnum][s] - && (sp = al.findIndex(sequence[pdbfnum][s])) > -1) - { - int lastPos = StructureMapping.UNASSIGNED_VALUE; - SequenceI asp = al.getSequenceAt(sp); - for (int r = 0; r < asp.getLength(); r++) - { - // no mapping to gaps in sequence - if (Comparison.isGap(asp.getCharAt(r))) - { - continue; - } - int pos = mapping[m].getPDBResNum(asp.findPosition(r)); - - if (pos == lastPos) - { - continue; - } - if (pos == StructureMapping.UNASSIGNED_VALUE) - { - // terminate current colour op - if (command.length() > 0 - && command.charAt(command.length() - 1) != ';') - { - command.append(";"); - } - // reset lastPos - lastPos = StructureMapping.UNASSIGNED_VALUE; - continue; - } - - lastPos = pos; - - Color col = sr.getResidueColour(sequence[pdbfnum][s], r, - finder); - - /* - * shade hidden regions darker - */ - if (!cs.isVisible(r)) - { - col = Color.GRAY; - } - - String newSelcom = (mapping[m].getChain() != " " - ? ":" + mapping[m].getChain() - : "") + "/" + (pdbfnum + 1) + ".1" + ";color" - + getColourString(col); - if (command.length() > newSelcom.length() && command - .substring(command.length() - newSelcom.length()) - .equals(newSelcom)) - { - command = JmolCommands.condenseCommand(command, pos); - continue; - } - // TODO: deal with case when buffer is too large for Jmol to parse - // - execute command and flush - - if (command.length() > 0 - && command.charAt(command.length() - 1) != ';') - { - command.append(";"); - } - - if (command.length() > 51200) - { - // add another chunk - str.add(command.toString()); - command.setLength(0); - } - command.append("select " + pos); - command.append(newSelcom); - } - // break; - } - } - } - { - // add final chunk - str.add(command.toString()); - command.setLength(0); - } - cset.addAll(str); - - } - return cset.toArray(new String[cset.size()]); - } - - public static StringBuilder condenseCommand(StringBuilder command, - int pos) - { - - // work back to last 'select' - int p = command.length(), q = p; - do - { - p -= 6; - if (p < 1) - { - p = 0; - } - ; - } while ((q = command.indexOf("select", p)) == -1 && p > 0); - - StringBuilder sb = new StringBuilder(command.substring(0, q + 7)); - - command = command.delete(0, q + 7); - - String start; - - if (command.indexOf("-") > -1) - { - start = command.substring(0, command.indexOf("-")); - } - else - { - start = command.substring(0, command.indexOf(":")); - } - - sb.append(start + "-" + pos + command.substring(command.indexOf(":"))); - - return sb; - } - @Override public StructureCommandI colourByChain() { @@ -372,14 +219,13 @@ public class JmolCommands extends StructureCommandsBase public StructureCommandI saveSession(String filepath) { /* - * https://chemapps.stolaf.edu/jmol/docs/#write - * not currently used in Jalview + * https://chemapps.stolaf.edu/jmol/docs/#writemodel */ - return new StructureCommand("write \"" + filepath + "\""); + return new StructureCommand("write STATE \"" + filepath + "\""); } @Override - protected StructureCommandI getColourCommand(String atomSpec, Color colour) + protected StructureCommandI colourResidues(String atomSpec, Color colour) { StringBuilder sb = new StringBuilder(atomSpec.length()+20); sb.append("select ").append(atomSpec).append(getCommandSeparator()) @@ -446,6 +292,193 @@ public class JmolCommands extends StructureCommandsBase @Override public StructureCommandI loadFile(String file) { - return null; + // https://chemapps.stolaf.edu/jmol/docs/#loadfiles + return new StructureCommand("load FILES \"" + + Platform.escapeBackslashes(file) + "\""); + } + + /** + * Obsolete method, only referenced from + * jalview.javascript.MouseOverStructureListener + * + * @param ssm + * @param files + * @param sequence + * @param sr + * @param viewPanel + * @return + */ + @Deprecated + public String[] colourBySequence(StructureSelectionManager ssm, + String[] files, SequenceI[][] sequence, SequenceRenderer sr, + AlignmentViewPanel viewPanel) + { + // TODO delete method + + FeatureRenderer fr = viewPanel.getFeatureRenderer(); + FeatureColourFinder finder = new FeatureColourFinder(fr); + AlignViewportI viewport = viewPanel.getAlignViewport(); + HiddenColumns cs = viewport.getAlignment().getHiddenColumns(); + AlignmentI al = viewport.getAlignment(); + List cset = new ArrayList<>(); + + for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) + { + StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); + StringBuilder command = new StringBuilder(128); + List str = new ArrayList<>(); + + if (mapping == null || mapping.length < 1) + { + continue; + } + + for (int s = 0; s < sequence[pdbfnum].length; s++) + { + for (int sp, m = 0; m < mapping.length; m++) + { + if (mapping[m].getSequence() == sequence[pdbfnum][s] + && (sp = al.findIndex(sequence[pdbfnum][s])) > -1) + { + int lastPos = StructureMapping.UNASSIGNED_VALUE; + SequenceI asp = al.getSequenceAt(sp); + for (int r = 0; r < asp.getLength(); r++) + { + // no mapping to gaps in sequence + if (Comparison.isGap(asp.getCharAt(r))) + { + continue; + } + int pos = mapping[m].getPDBResNum(asp.findPosition(r)); + + if (pos == lastPos) + { + continue; + } + if (pos == StructureMapping.UNASSIGNED_VALUE) + { + // terminate current colour op + if (command.length() > 0 + && command.charAt(command.length() - 1) != ';') + { + command.append(";"); + } + // reset lastPos + lastPos = StructureMapping.UNASSIGNED_VALUE; + continue; + } + + lastPos = pos; + + Color col = sr.getResidueColour(sequence[pdbfnum][s], r, + finder); + + /* + * shade hidden regions darker + */ + if (!cs.isVisible(r)) + { + col = Color.GRAY; + } + + String newSelcom = (mapping[m].getChain() != " " + ? ":" + mapping[m].getChain() + : "") + "/" + (pdbfnum + 1) + ".1" + ";color" + + getColourString(col); + if (command.length() > newSelcom.length() && command + .substring(command.length() - newSelcom.length()) + .equals(newSelcom)) + { + command = JmolCommands.condenseCommand(command, pos); + continue; + } + // TODO: deal with case when buffer is too large for Jmol to parse + // - execute command and flush + + if (command.length() > 0 + && command.charAt(command.length() - 1) != ';') + { + command.append(";"); + } + + if (command.length() > 51200) + { + // add another chunk + str.add(command.toString()); + command.setLength(0); + } + command.append("select " + pos); + command.append(newSelcom); + } + // break; + } + } + } + { + // add final chunk + str.add(command.toString()); + command.setLength(0); + } + cset.addAll(str); + + } + return cset.toArray(new String[cset.size()]); + } + + /** + * Helper method + * + * @param command + * @param pos + * @return + */ + @Deprecated + private static StringBuilder condenseCommand( + StringBuilder command, + int pos) + { + + // work back to last 'select' + int p = command.length(), q = p; + do + { + p -= 6; + if (p < 1) + { + p = 0; + } + ; + } while ((q = command.indexOf("select", p)) == -1 && p > 0); + + StringBuilder sb = new StringBuilder(command.substring(0, q + 7)); + + command = command.delete(0, q + 7); + + String start; + + if (command.indexOf("-") > -1) + { + start = command.substring(0, command.indexOf("-")); + } + else + { + start = command.substring(0, command.indexOf(":")); + } + + sb.append(start + "-" + pos + command.substring(command.indexOf(":"))); + + return sb; + } + + @Override + public StructureCommandI openSession(String filepath) + { + return loadFile(filepath); + } + + @Override + public StructureCommandI closeViewer() + { + return null; // not an external viewer } }