X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fjmol%2FJmolCommands.java;h=c8a54cdca1c6df99fc5353403db7a12326f11249;hb=2ab7b9b152018bb808693218ad88dc3778166492;hp=4c4e716cbb09ce9eb217cca5515287ab6fd4eccb;hpb=2f7206801276e822f4a7d36d9684ba313e0ee896;p=jalview.git diff --git a/src/jalview/ext/jmol/JmolCommands.java b/src/jalview/ext/jmol/JmolCommands.java index 4c4e716..c8a54cd 100644 --- a/src/jalview/ext/jmol/JmolCommands.java +++ b/src/jalview/ext/jmol/JmolCommands.java @@ -1,141 +1,288 @@ -/** - * - */ -package jalview.ext.jmol; - -import jalview.api.FeatureRenderer; -import jalview.api.SequenceRenderer; -import jalview.datamodel.AlignmentI; -import jalview.datamodel.SequenceI; -import jalview.structure.StructureMapping; -import jalview.structure.StructureSelectionManager; -import jalview.util.Comparison; - -import java.awt.Color; -import java.util.ArrayList; - -/** - * Routines for generating Jmol commands for Jalview/Jmol binding - * Innocuous change - * @author JimP - * - */ -public class JmolCommands -{ - - /** - * Jmol utility which constructs the commands to colour chains by the given alignment - * - */ - public static String[] getColourBySequenceCommand(StructureSelectionManager ssm, String[] files, SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr, AlignmentI alignment) - { - ArrayList str = new ArrayList(); - StringBuffer command = new StringBuffer(); - - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); - - if (mapping == null || mapping.length < 1) - continue; - - int lastPos = -1; - 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 = alignment.findIndex(sequence[pdbfnum][s])) > -1) - { - SequenceI asp = alignment.getSequenceAt(sp); - for (int r = 0; r < asp.getLength(); r++) - { - // no mapping to gaps in sequence - if (jalview.util.Comparison.isGap(asp.getCharAt(r))) - { - continue; - } - int pos = mapping[m].getPDBResNum(asp.findPosition(r)); - - if (pos < 1 || pos == lastPos) - continue; - - lastPos = pos; - - Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r); - - if (fr != null) - col = fr.findFeatureColour(col, sequence[pdbfnum][s], r); - String newSelcom = (mapping[m].getChain() != " " ? ":" - + mapping[m].getChain() : "") - + "/" - + (pdbfnum + 1) - + ".1" - + ";color[" - + col.getRed() - + "," - + col.getGreen() - + "," - + col.getBlue() + "]"; - 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 - - 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); - } - return str.toArray(new String[str.size()]); - } - - public static StringBuffer condenseCommand(StringBuffer 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); - - StringBuffer sb = new StringBuffer(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; - } - -} +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.ext.jmol; + +import jalview.api.AlignViewportI; +import jalview.api.AlignmentViewPanel; +import jalview.api.FeatureRenderer; +import jalview.api.SequenceRenderer; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.HiddenColumns; +import jalview.datamodel.SequenceI; +import jalview.renderer.seqfeatures.FeatureColourFinder; +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.List; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Routines for generating Jmol commands for Jalview/Jmol binding + * + * @author JimP + * + */ +public class JmolCommands extends StructureCommandsBase +{ + private static final String CMD_COLOUR_BY_CHARGE = "select *;color white;select ASP,GLU;color red;" + + "select LYS,ARG;color blue;select CYS;color yellow"; + + private static final String CMD_COLOUR_BY_CHAIN = "select *;color chain"; + + private static String formatRGB(Color c) { + return c == null ? null + : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(), + c.getBlue()); + } + + @Override + public String[] colourBySequence( + StructureSelectionManager ssm, String[] files, + SequenceI[][] sequence, SequenceRenderer sr, + AlignmentViewPanel viewPanel) + { + // TODO refactor to call buildColoursMap() first... + + 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[" + + col.getRed() + "," + col.getGreen() + "," + + col.getBlue() + "]"; + 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 String colourByChain() + { + return CMD_COLOUR_BY_CHAIN; + } + + @Override + public String colourByCharge() + { + return CMD_COLOUR_BY_CHARGE; + } + + @Override + public String colourByResidues(Map colours) + { + StringBuilder cmd = new StringBuilder(128); + cmd.append("select *;color white;"); + + /* + * concatenate commands like + * select VAL;color[100,215,55] + */ + for (Entry entry : colours.entrySet()) + { + Color col = entry.getValue(); + String resCode = entry.getKey(); + cmd.append("select ").append(resCode).append(";"); + cmd.append("color["); + cmd.append(formatRGB(col)); + cmd.append("];"); + } + + return cmd.toString(); + } + + @Override + public String setBackgroundColour(Color col) + { + return "background " + formatRGB(col); + } + + @Override + public String focusView() + { + return "zoom 0"; + } + + @Override + public String showChains(List toShow) + { + StringBuilder atomSpec = new StringBuilder(128); + boolean first = true; + for (String chain : toShow) + { + String[] tokens = chain.split(":"); + if (tokens.length == 2) + { + if (!first) + { + atomSpec.append(" or "); + } + first = false; + atomSpec.append(":").append(tokens[1]).append(" /").append(tokens[0]); + } + } + + String spec = atomSpec.toString(); + String command = "select *;restrict " + spec + ";cartoon;center " + + spec; + return command; + } + +}