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