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