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