Switch JpredWS from SequenceAnnotation to MsaWS
[jabaws.git] / datamodel / compbio / data / sequence / JpredAlignment.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  * Copyright (c) 2013 Alexander Sherstnev\r
3  *  \r
4  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
5  * \r
6  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
7  *  Apache License version 2 as published by the Apache Software Foundation\r
8  * \r
9  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
10  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
11  *  License for more details.\r
12  * \r
13  *  A copy of the license is in apache_license.txt. It is also available here:\r
14  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
15  * \r
16  * Any republication or derived work distributed in source code form\r
17  * must include this copyright and license notice.\r
18  */\r
19 \r
20 package compbio.data.sequence;\r
21 \r
22 import java.util.List;\r
23 import java.util.ArrayList;\r
24 \r
25 import javax.xml.bind.annotation.XmlAccessType;\r
26 import javax.xml.bind.annotation.XmlAccessorType;\r
27 \r
28 import compbio.util.annotation.Immutable;\r
29 import compbio.data.sequence.Alignment;\r
30 /**\r
31  * Multiple sequence alignment and Jpred prediction.\r
32  * \r
33  * Does not give any guarantees on the content of individual FastaSequece\r
34  * records. It does not guarantee neither the uniqueness of the names of\r
35  * sequences nor it guarantees the uniqueness of the sequences.\r
36  * \r
37  * @see FastaSequence\r
38  * @see AlignmentMetadata\r
39  * \r
40  * @author pvtroshin\r
41  * \r
42  * @version 1.0 September 2009\r
43  * \r
44  */\r
45 @XmlAccessorType(XmlAccessType.FIELD)\r
46 @Immutable\r
47 public final class JpredAlignment extends Alignment{\r
48 \r
49         private AlignmentMetadata metadata;\r
50         private List<FastaSequence> sequences;\r
51         \r
52         FastaSequence jnetpred;\r
53         FastaSequence JNETCONF;\r
54         FastaSequence JNETSOL25;\r
55         FastaSequence JNETSOL5;\r
56         FastaSequence JNETSOL0;\r
57         FastaSequence JNETHMM;\r
58         FastaSequence JNETPSSM;\r
59         FastaSequence JNETJURY;\r
60 \r
61         private JpredAlignment() {\r
62                 // This has to has a default constructor for JaxB\r
63         }\r
64 \r
65         /**\r
66          * @param sequences\r
67          * @param program\r
68          * @param gapchar\r
69          */\r
70         public JpredAlignment(List<FastaSequence> sequences, Program program,\r
71                         char gapchar) {\r
72                 DecomposeInitalJpredSeq(sequences);\r
73                 this.metadata = new AlignmentMetadata(Program.CLUSTAL, gapchar);\r
74         }\r
75 \r
76         /**\r
77          * \r
78          * @param sequences\r
79          * @param metadata\r
80          */\r
81         public JpredAlignment(List<FastaSequence> sequences, AlignmentMetadata metadata) {\r
82                 DecomposeInitalJpredSeq(sequences);\r
83                 this.metadata = metadata;\r
84         }\r
85 \r
86         /**\r
87          * \r
88          * @param sequences\r
89          */\r
90         private void DecomposeInitalJpredSeq(List<FastaSequence> sequences) {\r
91                 List<FastaSequence> realsequences = new ArrayList<FastaSequence>();\r
92                 for (FastaSequence s : sequences) {\r
93                         if (s.getId().equals("jnetpred")) {\r
94                                 this.jnetpred = s;\r
95                         } else if (s.getId().equals("JNETCONF")) {\r
96                                 this.JNETCONF = s;\r
97                         } else if (s.getId().equals("JNETSOL25")) {\r
98                                 this.JNETSOL25 = s;\r
99                         } else if (s.getId().equals("JNETSOL5")) {\r
100                                 this.JNETSOL5 = s;\r
101                         } else if (s.getId().equals("JNETSOL0")) {\r
102                                 this.JNETSOL0 = s;\r
103                         } else if (s.getId().equals("JNETHMM")) {\r
104                                 this.JNETSOL0 = s;\r
105                         } else if (s.getId().equals("JNETHMM")) {\r
106                                 this.JNETHMM = s;\r
107                         } else if (s.getId().equals("JNETSOL0")) {\r
108                                 this.JNETPSSM = s;\r
109                         } else if (s.getId().equals("JNETHMM")) {\r
110                                 this.JNETJURY = s;\r
111                         } else {\r
112                                 realsequences.add(s);\r
113                         }\r
114                 }\r
115                 this.sequences = realsequences;\r
116         }\r
117 \r
118         /**\r
119          * \r
120          * @return Jpred prediction \r
121          */\r
122         public String getJpredPrediction() {\r
123                 return this.jnetpred.getSequence();\r
124         }\r
125 \r
126         /**\r
127          * \r
128          * @return Jpred JNETSOL25 line\r
129          */\r
130         public String getJpredSOL25() {\r
131                 return this.JNETSOL25.getSequence();\r
132         }\r
133 \r
134         /**\r
135          * \r
136          * @return Jpred JNETSOL5 line\r
137          */\r
138         public String getJpredSOL5() {\r
139                 return this.JNETSOL5.getSequence();\r
140         }\r
141         /**\r
142          * \r
143          * @return Jpred JNETSOL0 line\r
144          */\r
145         public String getJpredSOL0() {\r
146                 return this.JNETSOL0.getSequence();\r
147         }\r
148         /**\r
149          * \r
150          * @return Jpred JNETCONF line\r
151          */\r
152         public String getJpredJNETCONF() {\r
153                 return this.JNETCONF.getSequence();\r
154         }\r
155         /**\r
156          * \r
157          * @return list of FastaSequence records\r
158          */\r
159         public List<FastaSequence> getSequences() {\r
160                 return sequences;\r
161         }\r
162 \r
163         /**\r
164          * \r
165          * @return a number of sequence in the alignment\r
166          */\r
167         public int getSize() {\r
168                 return this.sequences.size();\r
169         }\r
170 \r
171         /**\r
172          * \r
173          * @return AlignmentMetadata object\r
174          */\r
175         public AlignmentMetadata getMetadata() {\r
176                 return metadata;\r
177         }\r
178 \r
179         @Override\r
180         public String toString() {\r
181                 String sseq = "";\r
182                 for (FastaSequence fs : getSequences()) {\r
183                         sseq += fs.toString() + "\n";\r
184                 }\r
185                 return sseq;\r
186         }\r
187 \r
188         @Override\r
189         public int hashCode() {\r
190                 final int prime = 31;\r
191                 int result = 1;\r
192                 result = prime * result\r
193                                 + ((metadata == null) ? 0 : metadata.hashCode());\r
194                 result = prime * result\r
195                                 + ((sequences == null) ? 0 : sequences.hashCode());\r
196                 return result;\r
197         }\r
198 \r
199         /**\r
200          * Please note that this implementation does not take the order of sequences\r
201          * into account!\r
202          */\r
203         @Override\r
204         public boolean equals(Object obj) {\r
205                 if (obj == null) {\r
206                         return false;\r
207                 }\r
208                 if (!(obj instanceof Alignment)) {\r
209                         return false;\r
210                 }\r
211                 Alignment al = (Alignment) obj;\r
212                 if (this.getSize() != al.getSize()) {\r
213                         return false;\r
214                 }\r
215                 if (!this.getMetadata().equals(al.getMetadata())) {\r
216                         return false;\r
217                 }\r
218                 int outerCounter = 0;\r
219                 int matchCounter = 0;\r
220                 for (FastaSequence fs : getSequences()) {\r
221                         outerCounter++;\r
222                         for (FastaSequence fs1 : al.getSequences()) {\r
223                                 if (fs.equals(fs1)) {\r
224                                         matchCounter++;\r
225                                         continue;\r
226                                 }\r
227                         }\r
228                         // Match for at lease one element was not found!\r
229                         if (outerCounter != matchCounter) {\r
230                                 return false;\r
231                         }\r
232                 }\r
233 \r
234                 return true;\r
235         }\r
236 \r
237 }\r