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