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