JAL-1620 version bump and release notes
[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.*;
24 import jalview.util.MessageManager;
25
26 public class JnetAnnotationMaker
27 {
28   public static void add_annotation(JPredFile prediction, AlignmentI al,
29           int firstSeq, boolean noMsa) throws Exception
30   {
31     JnetAnnotationMaker.add_annotation(prediction, al, firstSeq, noMsa,
32             (int[]) null);
33   }
34
35   /**
36    * adds the annotation parsed by prediction to al.
37    * 
38    * @param prediction
39    *          JPredFile
40    * @param al
41    *          AlignmentI
42    * @param firstSeq
43    *          int the index of the sequence to attach the annotation to (usually
44    *          zero)
45    * @param noMsa
46    *          boolean
47    * @param delMap
48    *          mapping from columns in JPredFile prediction to residue number in
49    *          al.getSequence(firstSeq)
50    */
51   public static void add_annotation(JPredFile prediction, AlignmentI al,
52           int firstSeq, boolean noMsa, int[] delMap) throws Exception
53   {
54     int i = 0;
55     SequenceI[] preds = prediction.getSeqsAsArray();
56     // in the future we could search for the query
57     // sequence in the alignment before calling this function.
58     SequenceI seqRef = al.getSequenceAt(firstSeq);
59     int width = preds[0].getSequence().length;
60     int[] gapmap = al.getSequenceAt(firstSeq).gapMap();
61     if ((delMap != null && delMap.length > width)
62             || (delMap == null && gapmap.length != width))
63     {
64       throw (new Exception(MessageManager.formatMessage("exception.number_of_residues_in_query_sequence_differ_from_prediction", new String[]{
65                   (delMap == null ? "" : MessageManager.getString("label.mapped")),
66                   al.getSequenceAt(firstSeq).getName(),
67                   al.getSequenceAt(firstSeq).getSequenceAsString(),
68                   Integer.valueOf(width).toString()
69       })));
70     }
71
72     AlignmentAnnotation annot;
73     Annotation[] annotations = null;
74
75     int existingAnnotations = 0;
76     if (al.getAlignmentAnnotation() != null)
77     {
78       existingAnnotations = al.getAlignmentAnnotation().length;
79     }
80
81     while (i < preds.length)
82     {
83       String id = preds[i].getName().toUpperCase();
84
85       if (id.startsWith("LUPAS") || id.startsWith("JNET")
86               || id.startsWith("JPRED"))
87       {
88         annotations = new Annotation[al.getWidth()];
89         /*
90          * if (delMap!=null) { for (int j=0; j<annotations.length; j++)
91          * annotations[j] = new Annotation("","",'',0); }
92          */
93         if (id.equals("JNETPRED") || id.equals("JNETPSSM")
94                 || id.equals("JNETFREQ") || id.equals("JNETHMM")
95                 || id.equals("JNETALIGN") || id.equals("JPRED"))
96         {
97           if (delMap == null)
98           {
99             for (int j = 0; j < width; j++)
100             {
101               annotations[gapmap[j]] = new Annotation("", "",
102                       preds[i].getCharAt(j), 0);
103             }
104           }
105           else
106           {
107             for (int j = 0; j < width; j++)
108             {
109               annotations[gapmap[delMap[j]]] = new Annotation("", "",
110                       preds[i].getCharAt(j), 0);
111             }
112           }
113         }
114         else if (id.equals("JNETCONF"))
115         {
116           if (delMap == null)
117           {
118             for (int j = 0; j < width; j++)
119             {
120               float value = new Float(preds[i].getCharAt(j) + "")
121                       .floatValue();
122               annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)
123                       + "", "", preds[i].getCharAt(j), value);
124             }
125           }
126           else
127           {
128             for (int j = 0; j < width; j++)
129             {
130               float value = new Float(preds[i].getCharAt(j) + "")
131                       .floatValue();
132               annotations[gapmap[delMap[j]]] = new Annotation(
133                       preds[i].getCharAt(j) + "", "",
134                       preds[i].getCharAt(j), value);
135             }
136           }
137         }
138         else
139         {
140           if (delMap == null)
141           {
142             for (int j = 0; j < width; j++)
143             {
144               annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)
145                       + "", "", ' ', 0);
146             }
147           }
148           else
149           {
150             for (int j = 0; j < width; j++)
151             {
152               annotations[gapmap[delMap[j]]] = new Annotation(
153                       preds[i].getCharAt(j) + "", "", ' ', 0);
154             }
155           }
156         }
157
158         if (id.equals("JNETCONF"))
159         {
160           annot = new AlignmentAnnotation(preds[i].getName(),
161                   "JNet Output", annotations, 0f, 10f,
162                   AlignmentAnnotation.BAR_GRAPH);
163         }
164         else
165         {
166           annot = new AlignmentAnnotation(preds[i].getName(),
167                   "JNet Output", annotations);
168         }
169
170         if (seqRef != null)
171         {
172           annot.createSequenceMapping(seqRef, 1, true);
173           seqRef.addAlignmentAnnotation(annot);
174         }
175
176         al.addAnnotation(annot);
177         al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
178                 - existingAnnotations - 1);
179
180         if (noMsa)
181         {
182           al.deleteSequence(preds[i]);
183         }
184       }
185
186       i++;
187     }
188
189     // Hashtable scores = prediction.getScores();
190
191     /*
192      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
193      * "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
194      * 
195      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
196      * "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
197      * 
198      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
199      * "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
200      */
201
202   }
203 }