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