993737b38cefcf8d311fe1a4aaa4cde80b42582a
[jalview.git] / src / jalview / io / JnetAnnotationMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import jalview.datamodel.*;
22
23 public class JnetAnnotationMaker
24 {
25   public static void add_annotation(JPredFile prediction, AlignmentI al,
26           int firstSeq, boolean noMsa) throws Exception
27   {
28     JnetAnnotationMaker.add_annotation(prediction, al, firstSeq, noMsa,
29             (int[]) null);
30   }
31
32   /**
33    * adds the annotation parsed by prediction to al.
34    * 
35    * @param prediction
36    *                JPredFile
37    * @param al
38    *                AlignmentI
39    * @param firstSeq
40    *                int the index of the sequence to attach the annotation to
41    *                (usually zero)
42    * @param noMsa
43    *                boolean
44    * @param delMap
45    *                mapping from columns in JPredFile prediction to residue
46    *                number in al.getSequence(firstSeq)
47    */
48   public static void add_annotation(JPredFile prediction, AlignmentI al,
49           int firstSeq, boolean noMsa, int[] delMap) throws Exception
50   {
51     int i = 0;
52     SequenceI[] preds = prediction.getSeqsAsArray();
53     // in the future we could search for the query
54     // sequence in the alignment before calling this function.
55     SequenceI seqRef = al.getSequenceAt(firstSeq);
56     int width = preds[0].getSequence().length;
57     int[] gapmap = al.getSequenceAt(firstSeq).gapMap();
58     if ((delMap != null && delMap.length > width)
59             || (delMap == null && gapmap.length != width))
60     {
61       throw (new Exception("Number of residues in "
62               + (delMap == null ? "" : " mapped ")
63               + "supposed query sequence ('"
64               + al.getSequenceAt(firstSeq).getName() + "'\n"
65               + al.getSequenceAt(firstSeq).getSequenceAsString()
66               + ")\ndiffer from number of prediction sites in prediction ("
67               + width + ")"));
68     }
69
70     AlignmentAnnotation annot;
71     Annotation[] annotations = null;
72
73     int existingAnnotations = 0;
74     if (al.getAlignmentAnnotation() != null)
75     {
76       existingAnnotations = al.getAlignmentAnnotation().length;
77     }
78
79     while (i < preds.length)
80     {
81       String id = preds[i].getName().toUpperCase();
82
83       if (id.startsWith("LUPAS") || id.startsWith("JNET")
84               || id.startsWith("JPRED"))
85       {
86         annotations = new Annotation[al.getWidth()];
87         /*
88          * if (delMap!=null) { for (int j=0; j<annotations.length; j++)
89          * annotations[j] = new Annotation("","",'',0); }
90          */
91         if (id.equals("JNETPRED") || id.equals("JNETPSSM")
92                 || id.equals("JNETFREQ") || id.equals("JNETHMM")
93                 || id.equals("JNETALIGN") || id.equals("JPRED"))
94         {
95           if (delMap == null)
96           {
97             for (int j = 0; j < width; j++)
98             {
99               annotations[gapmap[j]] = new Annotation("", "", preds[i]
100                       .getCharAt(j), 0);
101             }
102           }
103           else
104           {
105             for (int j = 0; j < width; j++)
106             {
107               annotations[gapmap[delMap[j]]] = new Annotation("", "",
108                       preds[i].getCharAt(j), 0);
109             }
110           }
111         }
112         else if (id.equals("JNETCONF"))
113         {
114           if (delMap == null)
115           {
116             for (int j = 0; j < width; j++)
117             {
118               float value = new Float(preds[i].getCharAt(j) + "")
119                       .floatValue();
120               annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)
121                       + "", "", preds[i].getCharAt(j), value);
122             }
123           }
124           else
125           {
126             for (int j = 0; j < width; j++)
127             {
128               float value = new Float(preds[i].getCharAt(j) + "")
129                       .floatValue();
130               annotations[gapmap[delMap[j]]] = new Annotation(preds[i]
131                       .getCharAt(j)
132                       + "", "", preds[i].getCharAt(j), value);
133             }
134           }
135         }
136         else
137         {
138           if (delMap == null)
139           {
140             for (int j = 0; j < width; j++)
141             {
142               annotations[gapmap[j]] = new Annotation(preds[i].getCharAt(j)
143                       + "", "", ' ', 0);
144             }
145           }
146           else
147           {
148             for (int j = 0; j < width; j++)
149             {
150               annotations[gapmap[delMap[j]]] = new Annotation(preds[i]
151                       .getCharAt(j)
152                       + "", "", ' ', 0);
153             }
154           }
155         }
156
157         if (id.equals("JNETCONF"))
158         {
159           annot = new AlignmentAnnotation(preds[i].getName(),
160                   "JNet Output", annotations, 0f, 10f,
161                   AlignmentAnnotation.BAR_GRAPH);
162         }
163         else
164         {
165           annot = new AlignmentAnnotation(preds[i].getName(),
166                   "JNet Output", annotations);
167         }
168
169         if (seqRef != null)
170         {
171           annot.createSequenceMapping(seqRef, 1, true);
172           seqRef.addAlignmentAnnotation(annot);
173         }
174
175         al.addAnnotation(annot);
176         al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
177                 - existingAnnotations - 1);
178
179         if (noMsa)
180         {
181           al.deleteSequence(preds[i]);
182         }
183       }
184
185       i++;
186     }
187
188     // Hashtable scores = prediction.getScores();
189
190     /*
191      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
192      * "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
193      * 
194      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
195      * "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
196      * 
197      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
198      * "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
199      */
200
201   }
202 }