e25dab4d174dc84db6eaf5fea56b5965ca4f96c6
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ext.jmol;
19
20 import jalview.api.FeatureRenderer;
21 import jalview.api.SequenceRenderer;
22 import jalview.datamodel.AlignmentI;
23 import jalview.datamodel.SequenceI;
24 import jalview.structure.StructureMapping;
25 import jalview.structure.StructureMappingcommandSet;
26 import jalview.structure.StructureSelectionManager;
27
28 import java.awt.Color;
29 import java.util.ArrayList;
30
31 /**
32  * Routines for generating Jmol commands for Jalview/Jmol binding another
33  * cruisecontrol test.
34  * 
35  * @author JimP
36  * 
37  */
38 public class JmolCommands
39 {
40
41   /**
42    * Jmol utility which constructs the commands to colour chains by the given
43    * alignment
44    * 
45    * @returns Object[] { Object[] { <model being coloured>,
46    * 
47    */
48   public static StructureMappingcommandSet[] getColourBySequenceCommand(
49           StructureSelectionManager ssm, String[] files,
50           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
51           AlignmentI alignment)
52   {
53
54     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
55
56     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
57     {
58       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
59       StringBuffer command = new StringBuffer();
60       StructureMappingcommandSet smc;
61       ArrayList<String> str = new ArrayList<String>();
62
63       if (mapping == null || mapping.length < 1)
64         continue;
65
66       int lastPos = -1;
67       for (int s = 0; s < sequence[pdbfnum].length; s++)
68       {
69         for (int sp, m = 0; m < mapping.length; m++)
70         {
71           if (mapping[m].getSequence() == sequence[pdbfnum][s]
72                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
73           {
74             SequenceI asp = alignment.getSequenceAt(sp);
75             for (int r = 0; r < asp.getLength(); r++)
76             {
77               // no mapping to gaps in sequence
78               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
79               {
80                 continue;
81               }
82               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
83
84               if (pos < 1 || pos == lastPos)
85                 continue;
86
87               lastPos = pos;
88
89               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
90
91               if (fr != null)
92                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
93               String newSelcom = (mapping[m].getChain() != " " ? ":"
94                       + mapping[m].getChain() : "")
95                       + "/"
96                       + (pdbfnum + 1)
97                       + ".1"
98                       + ";color["
99                       + col.getRed()
100                       + ","
101                       + col.getGreen()
102                       + ","
103                       + col.getBlue() + "]";
104               if (command.length() > newSelcom.length()
105                       && command.substring(
106                               command.length() - newSelcom.length())
107                               .equals(newSelcom))
108               {
109                 command = JmolCommands.condenseCommand(command, pos);
110                 continue;
111               }
112               // TODO: deal with case when buffer is too large for Jmol to parse
113               // - execute command and flush
114
115               command.append(";");
116               if (command.length() > 51200)
117               {
118                 // add another chunk
119                 str.add(command.toString());
120                 command.setLength(0);
121               }
122               command.append("select " + pos);
123               command.append(newSelcom);
124             }
125             break;
126           }
127         }
128       }
129       {
130         // add final chunk
131         str.add(command.toString());
132         command.setLength(0);
133       }
134       // Finally, add the command set ready to be returned.
135       cset.add(new StructureMappingcommandSet(JmolCommands.class,
136               files[pdbfnum], str.toArray(new String[str.size()])));
137
138     }
139     return cset.toArray(new StructureMappingcommandSet[cset.size()]);
140   }
141
142   public static StringBuffer condenseCommand(StringBuffer command, int pos)
143   {
144
145     // work back to last 'select'
146     int p = command.length(), q = p;
147     do
148     {
149       p -= 6;
150       if (p < 1)
151       {
152         p = 0;
153       }
154       ;
155     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
156
157     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
158
159     command = command.delete(0, q + 7);
160
161     String start;
162
163     if (command.indexOf("-") > -1)
164     {
165       start = command.substring(0, command.indexOf("-"));
166     }
167     else
168     {
169       start = command.substring(0, command.indexOf(":"));
170     }
171
172     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
173
174     return sb;
175   }
176
177 }