JAL-2379 'direct' pairwise score calculation for PCA (no encoding)
[jalview.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
31 import java.awt.Color;
32 import java.util.ArrayList;
33
34 /**
35  * Routines for generating Jmol commands for Jalview/Jmol binding another
36  * cruisecontrol test.
37  * 
38  * @author JimP
39  * 
40  */
41 public class JmolCommands
42 {
43
44   /**
45    * Jmol utility which constructs the commands to colour chains by the given
46    * alignment
47    * 
48    * @returns Object[] { Object[] { <model being coloured>,
49    * 
50    */
51   public static StructureMappingcommandSet[] getColourBySequenceCommand(
52           StructureSelectionManager ssm, String[] files,
53           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
54           AlignmentI alignment)
55   {
56
57     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
58
59     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
60     {
61       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
62       StringBuffer command = new StringBuffer();
63       StructureMappingcommandSet smc;
64       ArrayList<String> str = new ArrayList<String>();
65
66       if (mapping == null || mapping.length < 1)
67       {
68         continue;
69       }
70
71       int lastPos = -1;
72       for (int s = 0; s < sequence[pdbfnum].length; s++)
73       {
74         for (int sp, m = 0; m < mapping.length; m++)
75         {
76           if (mapping[m].getSequence() == sequence[pdbfnum][s]
77                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
78           {
79             SequenceI asp = alignment.getSequenceAt(sp);
80             for (int r = 0; r < asp.getLength(); r++)
81             {
82               // no mapping to gaps in sequence
83               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
84               {
85                 continue;
86               }
87               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
88
89               if (pos < 1 || pos == lastPos)
90               {
91                 continue;
92               }
93
94               lastPos = pos;
95
96               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
97
98               if (fr != null)
99               {
100                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
101               }
102               String newSelcom = (mapping[m].getChain() != " " ? ":"
103                       + mapping[m].getChain() : "")
104                       + "/"
105                       + (pdbfnum + 1)
106                       + ".1"
107                       + ";color["
108                       + col.getRed()
109                       + ","
110                       + col.getGreen()
111                       + ","
112                       + col.getBlue() + "]";
113               if (command.length() > newSelcom.length()
114                       && command.substring(
115                               command.length() - newSelcom.length())
116                               .equals(newSelcom))
117               {
118                 command = JmolCommands.condenseCommand(command, pos);
119                 continue;
120               }
121               // TODO: deal with case when buffer is too large for Jmol to parse
122               // - execute command and flush
123
124               command.append(";");
125               if (command.length() > 51200)
126               {
127                 // add another chunk
128                 str.add(command.toString());
129                 command.setLength(0);
130               }
131               command.append("select " + pos);
132               command.append(newSelcom);
133             }
134             // break;
135           }
136         }
137       }
138       {
139         // add final chunk
140         str.add(command.toString());
141         command.setLength(0);
142       }
143       // Finally, add the command set ready to be returned.
144       cset.add(new StructureMappingcommandSet(JmolCommands.class,
145               files[pdbfnum], str.toArray(new String[str.size()])));
146
147     }
148     return cset.toArray(new StructureMappingcommandSet[cset.size()]);
149   }
150
151   public static StringBuffer condenseCommand(StringBuffer command, int pos)
152   {
153
154     // work back to last 'select'
155     int p = command.length(), q = p;
156     do
157     {
158       p -= 6;
159       if (p < 1)
160       {
161         p = 0;
162       }
163       ;
164     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
165
166     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
167
168     command = command.delete(0, q + 7);
169
170     String start;
171
172     if (command.indexOf("-") > -1)
173     {
174       start = command.substring(0, command.indexOf("-"));
175     }
176     else
177     {
178       start = command.substring(0, command.indexOf(":"));
179     }
180
181     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
182
183     return sb;
184   }
185
186 }