formatting
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 /**
19  * 
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         continue;
69
70       int lastPos = -1;
71       for (int s = 0; s < sequence[pdbfnum].length; s++)
72       {
73         for (int sp, m = 0; m < mapping.length; m++)
74         {
75           if (mapping[m].getSequence() == sequence[pdbfnum][s]
76                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
77           {
78             SequenceI asp = alignment.getSequenceAt(sp);
79             for (int r = 0; r < asp.getLength(); r++)
80             {
81               // no mapping to gaps in sequence
82               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
83               {
84                 continue;
85               }
86               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
87
88               if (pos < 1 || pos == lastPos)
89                 continue;
90
91               lastPos = pos;
92
93               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
94
95               if (fr != null)
96                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
97               String newSelcom = (mapping[m].getChain() != " " ? ":"
98                       + mapping[m].getChain() : "")
99                       + "/"
100                       + (pdbfnum + 1)
101                       + ".1"
102                       + ";color["
103                       + col.getRed()
104                       + ","
105                       + col.getGreen()
106                       + ","
107                       + col.getBlue() + "]";
108               if (command.length() > newSelcom.length()
109                       && command.substring(
110                               command.length() - newSelcom.length())
111                               .equals(newSelcom))
112               {
113                 command = JmolCommands.condenseCommand(command, pos);
114                 continue;
115               }
116               // TODO: deal with case when buffer is too large for Jmol to parse
117               // - execute command and flush
118
119               command.append(";");
120               if (command.length() > 51200)
121               {
122                 // add another chunk
123                 str.add(command.toString());
124                 command.setLength(0);
125               }
126               command.append("select " + pos);
127               command.append(newSelcom);
128             }
129             break;
130           }
131         }
132       }
133       {
134         // add final chunk
135         str.add(command.toString());
136         command.setLength(0);
137       }
138       // Finally, add the command set ready to be returned.
139       cset.add(new StructureMappingcommandSet(JmolCommands.class,
140               files[pdbfnum], str.toArray(new String[str.size()])));
141
142     }
143     return cset.toArray(new StructureMappingcommandSet[cset.size()]);
144   }
145
146   public static StringBuffer condenseCommand(StringBuffer command, int pos)
147   {
148
149     // work back to last 'select'
150     int p = command.length(), q = p;
151     do
152     {
153       p -= 6;
154       if (p < 1)
155       {
156         p = 0;
157       }
158       ;
159     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
160
161     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
162
163     command = command.delete(0, q + 7);
164
165     String start;
166
167     if (command.indexOf("-") > -1)
168     {
169       start = command.substring(0, command.indexOf("-"));
170     }
171     else
172     {
173       start = command.substring(0, command.indexOf(":"));
174     }
175
176     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
177
178     return sb;
179   }
180
181 }