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