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