6828202da66c0dda62cc07345543fb8f2bad629b
[jalview.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(JPredFile prediction, AlignmentI al,
32           int firstSeq, boolean noMsa) throws Exception
33   {
34     JnetAnnotationMaker.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(JPredFile 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].getLength();
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(MessageManager.formatMessage(
68               "exception.number_of_residues_in_query_sequence_differ_from_prediction",
69               new String[]
70               { (delMap == null ? ""
71                       : MessageManager.getString("label.mapped")),
72                   al.getSequenceAt(firstSeq).getName(),
73                   al.getSequenceAt(firstSeq).getSequenceAsString(),
74                   Integer.valueOf(width).toString() })));
75     }
76
77     AlignmentAnnotation annot;
78     Annotation[] annotations = null;
79
80     int existingAnnotations = 0;
81     if (al.getAlignmentAnnotation() != null)
82     {
83       existingAnnotations = al.getAlignmentAnnotation().length;
84     }
85
86     Annotation[] sol = new Annotation[al.getWidth()];
87     boolean firstsol = true;
88
89     while (i < preds.length)
90     {
91       String id = preds[i].getName().toUpperCase();
92
93       if (id.startsWith("LUPAS") || id.startsWith("JNET")
94               || id.startsWith("JPRED"))
95       {
96         if (id.startsWith("JNETSOL"))
97         {
98           float amnt = (id.endsWith("25") ? 3f
99                   : id.endsWith("5") ? 6f : 9f);
100           for (int spos = 0; spos < width; spos++)
101           {
102             int sposw = (delMap == null) ? gapmap[spos]
103                     : gapmap[delMap[spos]];
104             if (firstsol)
105             {
106               sol[sposw] = new Annotation(0f);
107             }
108             if (preds[i].getCharAt(spos) == 'B'
109                     && (sol[sposw].value == 0f || sol[sposw].value < amnt))
110             {
111               sol[sposw].value = amnt;
112             }
113           }
114           firstsol = false;
115         }
116         else
117         {
118           // some other kind of annotation
119           annotations = new Annotation[al.getWidth()];
120           /*
121            * if (delMap!=null) { for (int j=0; j<annotations.length; j++)
122            * annotations[j] = new Annotation("","",'',0); }
123            */
124           if (id.equals("JNETPRED") || id.equals("JNETPSSM")
125                   || id.equals("JNETFREQ") || id.equals("JNETHMM")
126                   || id.equals("JNETALIGN") || id.equals("JPRED"))
127           {
128             if (delMap == null)
129             {
130               for (int j = 0; j < width; j++)
131               {
132                 annotations[gapmap[j]] = new Annotation("", "",
133                         preds[i].getCharAt(j), 0);
134               }
135             }
136             else
137             {
138               for (int j = 0; j < width; j++)
139               {
140                 annotations[gapmap[delMap[j]]] = new Annotation("", "",
141                         preds[i].getCharAt(j), 0);
142               }
143             }
144           }
145           else if (id.equals("JNETCONF"))
146           {
147             if (delMap == null)
148             {
149               for (int j = 0; j < width; j++)
150               {
151                 float value = Float.valueOf(preds[i].getCharAt(j) + "")
152                         .floatValue();
153                 annotations[gapmap[j]] = new Annotation(
154                         preds[i].getCharAt(j) + "", "",
155                         preds[i].getCharAt(j), value);
156               }
157             }
158             else
159             {
160               for (int j = 0; j < width; j++)
161               {
162                 float value = Float.valueOf(preds[i].getCharAt(j) + "")
163                         .floatValue();
164                 annotations[gapmap[delMap[j]]] = new Annotation(
165                         preds[i].getCharAt(j) + "", "",
166                         preds[i].getCharAt(j), value);
167               }
168             }
169           }
170           else
171           {
172             if (delMap == null)
173             {
174               for (int j = 0; j < width; j++)
175               {
176                 annotations[gapmap[j]] = new Annotation(
177                         preds[i].getCharAt(j) + "", "", ' ', 0);
178               }
179             }
180             else
181             {
182               for (int j = 0; j < width; j++)
183               {
184                 annotations[gapmap[delMap[j]]] = new Annotation(
185                         preds[i].getCharAt(j) + "", "", ' ', 0);
186               }
187             }
188           }
189
190           if (id.equals("JNETCONF"))
191           {
192             annot = new AlignmentAnnotation(preds[i].getName(),
193                     "JPred Output", annotations, 0f, 10f,
194                     AlignmentAnnotation.BAR_GRAPH);
195           }
196           else
197           {
198             annot = new AlignmentAnnotation(preds[i].getName(),
199                     "JPred Output", annotations);
200           }
201
202           if (seqRef != null)
203           {
204             annot.createSequenceMapping(seqRef, 1, true);
205             seqRef.addAlignmentAnnotation(annot);
206           }
207
208           al.addAnnotation(annot);
209           al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
210                   - existingAnnotations - 1);
211         }
212         if (noMsa)
213         {
214           al.deleteSequence(preds[i]);
215         }
216       }
217
218       i++;
219     }
220     if (!firstsol)
221     {
222       // add the solvent accessibility
223       annot = new AlignmentAnnotation("Jnet Burial",
224               "<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>",
225               sol, 0f, 9f, AlignmentAnnotation.BAR_GRAPH);
226
227       annot.validateRangeAndDisplay();
228       if (seqRef != null)
229       {
230         annot.createSequenceMapping(seqRef, 1, true);
231         seqRef.addAlignmentAnnotation(annot);
232       }
233       al.addAnnotation(annot);
234       al.setAnnotationIndex(annot,
235               al.getAlignmentAnnotation().length - existingAnnotations - 1);
236     }
237     // Hashtable scores = prediction.getScores();
238
239     /*
240      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
241      * "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
242      * 
243      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
244      * "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
245      * 
246      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
247      * "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
248      */
249
250   }
251 }