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