JAL-2465 bugfix and rerefactor renamed getPdbFile() method to getStructureFile()
[jalview.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
30 import java.awt.Color;
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         continue;
62
63       int lastPos = -1;
64       for (int s = 0; s < sequence[pdbfnum].length; s++)
65       {
66         for (int sp, m = 0; m < mapping.length; m++)
67         {
68           if (mapping[m].getSequence() == sequence[pdbfnum][s]
69                   && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
70           {
71             SequenceI asp = alignment.getSequenceAt(sp);
72             for (int r = 0; r < asp.getLength(); r++)
73             {
74               // no mapping to gaps in sequence
75               if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
76               {
77                 continue;
78               }
79               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
80
81               if (pos < 1 || pos == lastPos)
82                 continue;
83
84               lastPos = pos;
85
86               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
87
88               if (fr != null)
89                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
90               String newSelcom = (mapping[m].getChain() != " " ? ":"
91                       + mapping[m].getChain() : "")
92                       + "/"
93                       + (pdbfnum + 1)
94                       + ".1"
95                       + ";color["
96                       + col.getRed()
97                       + ","
98                       + col.getGreen()
99                       + ","
100                       + col.getBlue() + "]";
101               if (command.length() > newSelcom.length()
102                       && command.substring(
103                               command.length() - newSelcom.length())
104                               .equals(newSelcom))
105               {
106                 command = VarnaCommands.condenseCommand(command, pos);
107                 continue;
108               }
109               // TODO: deal with case when buffer is too large for Jmol to parse
110               // - execute command and flush
111
112               command.append(";");
113               if (command.length() > 51200)
114               {
115                 // add another chunk
116                 str.add(command.toString());
117                 command.setLength(0);
118               }
119               command.append("select " + pos);
120               command.append(newSelcom);
121             }
122             break;
123           }
124         }
125       }
126     }
127     {
128       // add final chunk
129       str.add(command.toString());
130       command.setLength(0);
131     }
132     return str.toArray(new String[str.size()]);
133   }
134
135   public static StringBuffer condenseCommand(StringBuffer command, int pos)
136   {
137
138     // work back to last 'select'
139     int p = command.length(), q = p;
140     do
141     {
142       p -= 6;
143       if (p < 1)
144       {
145         p = 0;
146       }
147       ;
148     } while ((q = command.indexOf("select", p)) == -1 && p > 0);
149
150     StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
151
152     command = command.delete(0, q + 7);
153
154     String start;
155
156     if (command.indexOf("-") > -1)
157     {
158       start = command.substring(0, command.indexOf("-"));
159     }
160     else
161     {
162       start = command.substring(0, command.indexOf(":"));
163     }
164
165     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
166
167     return sb;
168   }
169
170 }