JAL-1432 updated copyright notices
[jalview.git] / src / jalview / io / JnetAnnotationMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 (usually
41    *          zero)
42    * @param noMsa
43    *          boolean
44    * @param delMap
45    *          mapping from columns in JPredFile prediction to residue number in
46    *          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("", "",
100                       preds[i].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(
131                       preds[i].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(
151                       preds[i].getCharAt(j) + "", "", ' ', 0);
152             }
153           }
154         }
155
156         if (id.equals("JNETCONF"))
157         {
158           annot = new AlignmentAnnotation(preds[i].getName(),
159                   "JNet Output", annotations, 0f, 10f,
160                   AlignmentAnnotation.BAR_GRAPH);
161         }
162         else
163         {
164           annot = new AlignmentAnnotation(preds[i].getName(),
165                   "JNet Output", annotations);
166         }
167
168         if (seqRef != null)
169         {
170           annot.createSequenceMapping(seqRef, 1, true);
171           seqRef.addAlignmentAnnotation(annot);
172         }
173
174         al.addAnnotation(annot);
175         al.setAnnotationIndex(annot, al.getAlignmentAnnotation().length
176                 - existingAnnotations - 1);
177
178         if (noMsa)
179         {
180           al.deleteSequence(preds[i]);
181         }
182       }
183
184       i++;
185     }
186
187     // Hashtable scores = prediction.getScores();
188
189     /*
190      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPH"),
191      * "JnetpropH", "Jnet Helix Propensity", 0f,1f,1);
192      * 
193      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPB"),
194      * "JnetpropB", "Jnet Beta Sheet Propensity", 0f,1f,1);
195      * 
196      * addFloatAnnotations(al, gapmap, (Vector)scores.get("JNETPROPC"),
197      * "JnetpropC", "Jnet Coil Propensity", 0f,1f,1);
198      */
199
200   }
201 }