d0d0b5b67cead8dcb1593bcefedee6ef35c989ab
[jalview.git] / 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.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceI;
28 import jalview.structure.StructureMapping;
29 import jalview.structure.StructureSelectionManager;
30
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, FeatureRenderer fr,
51           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               ColorI col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
91
92               if (fr != null)
93               {
94                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
95               }
96               String newSelcom = (mapping[m].getChain() != " " ? ":"
97                       + mapping[m].getChain() : "")
98                       + "/"
99                       + (pdbfnum + 1)
100                       + ".1"
101                       + ";color["
102                       + col.getRed()
103                       + ","
104                       + col.getGreen()
105                       + ","
106                       + col.getBlue() + "]";
107               if (command.length() > newSelcom.length()
108                       && command.substring(
109                               command.length() - newSelcom.length())
110                               .equals(newSelcom))
111               {
112                 command = VarnaCommands.condenseCommand(command, pos);
113                 continue;
114               }
115               // TODO: deal with case when buffer is too large for Jmol to parse
116               // - execute command and flush
117
118               command.append(";");
119               if (command.length() > 51200)
120               {
121                 // add another chunk
122                 str.add(command.toString());
123                 command.setLength(0);
124               }
125               command.append("select " + pos);
126               command.append(newSelcom);
127             }
128             break;
129           }
130         }
131       }
132     }
133     {
134       // add final chunk
135       str.add(command.toString());
136       command.setLength(0);
137     }
138     return str.toArray(new String[str.size()]);
139   }
140
141   public static StringBuffer condenseCommand(StringBuffer command, int pos)
142   {
143
144     // work back to last 'select'
145     int p = command.length(), q = p;
146     do
147     {
148       p -= 6;
149       if (p < 1)
150       {
151         p = 0;
152       }
153       ;
154     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
155
156     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
157
158     command = command.delete(0, q + 7);
159
160     String start;
161
162     if (command.indexOf("-") > -1)
163     {
164       start = command.substring(0, command.indexOf("-"));
165     }
166     else
167     {
168       start = command.substring(0, command.indexOf(":"));
169     }
170
171     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
172
173     return sb;
174   }
175
176 }