JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / jalview / io / JnetAnnotationMaker.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.io;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
28
29 public class JnetAnnotationMaker
30 {
31   public static void add_annotation(AlignFile prediction, AlignmentI al,
32           int firstSeq, boolean noMsa) throws Exception
33   {
34     add_annotation(prediction, al, firstSeq, noMsa,
35             (int[]) null);
36   }
37
38   /**
39    * adds the annotation parsed by prediction to al.
40    * 
41    * @param prediction
42    *          JPredFile
43    * @param al
44    *          AlignmentI
45    * @param firstSeq
46    *          int the index of the sequence to attach the annotation to (usually
47    *          zero)
48    * @param noMsa
49    *          boolean
50    * @param delMap
51    *          mapping from columns in JPredFile prediction to residue number in
52    *          al.getSequence(firstSeq)
53    */
54   public static void add_annotation(AlignFile prediction, AlignmentI al,
55           int firstSeq, boolean noMsa, int[] delMap) throws Exception
56   {
57     int i = 0;
58     SequenceI[] preds = prediction.getSeqsAsArray();
59     // in the future we could search for the query
60     // sequence in the alignment before calling this function.
61     SequenceI seqRef = al.getSequenceAt(firstSeq);
62     int width = preds[0].getSequence().length;
63     int[] gapmap = al.getSequenceAt(firstSeq).gapMap();
64     if ((delMap != null && delMap.length > width)
65             || (delMap == null && gapmap.length != width))
66     {
67       throw (new Exception(
68               MessageManager
69                       .formatMessage(
70                               "exception.number_of_residues_in_query_sequence_differ_from_prediction",
71                               new String[]
72                               {
73                                   (delMap == null ? "" : MessageManager
74                                           .getString("label.mapped")),
75                                   al.getSequenceAt(firstSeq).getName(),
76                                   al.getSequenceAt(firstSeq)
77                                           .getSequenceAsString(),
78                                   Integer.valueOf(width).toString() })));
79     }
80
81     AlignmentAnnotation annot;
82     Annotation[] annotations = null;
83
84     int existingAnnotations = 0;
85     if (al.getAlignmentAnnotation() != null)
86     {
87       existingAnnotations = al.getAlignmentAnnotation().length;
88     }
89
90     Annotation[] sol = new Annotation[al.getWidth()];
91     boolean firstsol = true;
92
93     while (i < preds.length)
94     {
95       String id = preds[i].getName().toUpperCase();
96
97       if (id.startsWith("LUPAS") || id.startsWith("JNET")
98               || id.startsWith("JPRED"))
99       {
100         if (id.startsWith("JNETSOL"))
101         {
102           float amnt = (id.endsWith("25") ? 3f : id.endsWith("5") ? 6f : 9f);
103           for (int spos = 0; spos < width; spos++)
104           {
105             int sposw = (delMap == null) ? gapmap[spos]
106                     : gapmap[delMap[spos]];
107             if (firstsol)
108             {
109               sol[sposw] = new Annotation(0f);
110             }
111             if (preds[i].getCharAt(spos) == 'B'
112                     && (sol[sposw].value == 0f || sol[sposw].value < amnt))
113             {
114               sol[sposw].value = amnt;
115             }
116           }
117           firstsol = false;
118         }
119         else
120         {
121           // some other kind of annotation
122           annotations = new Annotation[al.getWidth()];
123           /*
124            * if (delMap!=null) { for (int j=0; j<annotations.length; j++)
125            * annotations[j] = new Annotation("","",'',0); }
126            */
127           if (id.equals("JNETPRED") || id.equals("JNETPSSM")
128                   || id.equals("JNETFREQ") || id.equals("JNETHMM")
129                   || id.equals("JNETALIGN") || id.equals("JPRED"))
130           {
131             if (delMap == null)
132             {
133               for (int j = 0; j < width; j++)
134               {
135                 annotations[gapmap[j]] = new Annotation("", "",
136                         preds[i].getCharAt(j), 0);
137               }
138             }
139             else
140             {
141               for (int j = 0; j < width; j++)
142               {
143                 annotations[gapmap[delMap[j]]] = new Annotation("", "",
144                         preds[i].getCharAt(j), 0);
145               }
146             }
147           }
148           else if (id.equals("JNETCONF"))
149           {
150             if (delMap == null)
151             {
152               for (int j = 0; j < width; j++)
153               {
154                 float value = new Float(preds[i].getCharAt(j) + "")
155                         .floatValue();
156                 annotations[gapmap[j]] = new Annotation(
157                         preds[i].getCharAt(j) + "", "",
158                         preds[i].getCharAt(j), value);
159               }
160             }
161             else
162             {
163               for (int j = 0; j < width; j++)
164               {
165                 float value = new Float(preds[i].getCharAt(j) + "")
166                         .floatValue();
167                 annotations[gapmap[delMap[j]]] = new Annotation(
168                         preds[i].getCharAt(j) + "", "",
169                         preds[i].getCharAt(j), value);
170               }
171             }
172           }
173           else
174           {
175             if (delMap == null)
176             {
177               for (int j = 0; j < width; j++)
178               {
179                 annotations[gapmap[j]] = new Annotation(
180                         preds[i].getCharAt(j) + "", "", ' ', 0);
181               }
182             }
183             else
184             {
185               for (int j = 0; j < width; j++)
186               {
187                 annotations[gapmap[delMap[j]]] = new Annotation(
188                         preds[i].getCharAt(j) + "", "", ' ', 0);
189               }
190             }
191           }
192
193           if (id.equals("JNETCONF"))
194           {
195             annot = new AlignmentAnnotation(preds[i].getName(),
196                     "JNet Output", annotations, 0f, 10f,
197                     AlignmentAnnotation.BAR_GRAPH);
198           }
199           else
200           {
201             annot = new AlignmentAnnotation(preds[i].getName(),
202                     "JNet Output", annotations);
203           }
204
205           if (seqRef != null)
206           {
207             annot.createSequenceMapping(seqRef, 1, true);
208             seqRef.addAlignmentAnnotation(annot);
209           }
210
211           al.addAnnotation(annot);
212           al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
213                   - existingAnnotations - 1);
214         }
215         if (noMsa)
216         {
217           al.deleteSequence(preds[i]);
218         }
219       }
220
221       i++;
222     }
223     if (!firstsol)
224     {
225       // add the solvent accessibility
226       annot = new AlignmentAnnotation(
227               "Jnet Burial",
228               "<html>Prediction of Solvent Accessibility<br/>levels are<ul><li>0 - Exposed</li><li>3 - 25% or more S.A. accessible</li><li>6 - 5% or more S.A. accessible</li><li>9 - Buried (<5% exposed)</li></ul>",
229               sol, 0f, 9f, AlignmentAnnotation.BAR_GRAPH);
230
231       annot.validateRangeAndDisplay();
232       if (seqRef != null)
233       {
234         annot.createSequenceMapping(seqRef, 1, true);
235         seqRef.addAlignmentAnnotation(annot);
236       }
237       al.addAnnotation(annot);
238       al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
239               - existingAnnotations - 1);
240     }
241     // Hashtable scores = prediction.getScores();
242
243     /*
244      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
245      * "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
246      * 
247      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
248      * "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
249      * 
250      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
251      * "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
252      */
253
254   }
255 }