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