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