768ac9ef8e7cc3afb7ace648c9e158a39a2b5acc
[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 public final class JpredAlignment extends Alignment{\r
47 \r
48         private JpredAlignment() {\r
49                 // This has to has a default constructor for JaxB\r
50         }\r
51 \r
52         /**\r
53          * @param sequences\r
54          * @param program\r
55          * @param gapchar\r
56          */\r
57         public JpredAlignment(List<FastaSequence> sequences, Program program, char gapchar) {\r
58                 this.sequences = sequences;\r
59                 this.metadata = new AlignmentMetadata(Program.Jpred, gapchar);\r
60         }\r
61 \r
62         /**\r
63          * \r
64          * @param sequences\r
65          * @param metadata\r
66          */\r
67         public JpredAlignment(List<FastaSequence> sequences, AlignmentMetadata metadata) {\r
68                 this.sequences = sequences;\r
69                 this.metadata = metadata;\r
70         }\r
71 \r
72         private List<FastaSequence> getRealSeqs() {\r
73                 List<FastaSequence> realsequences = new ArrayList<FastaSequence>();\r
74                 for (FastaSequence s : this.sequences) {\r
75                         if (s.getId().equals("jnetpred") || \r
76                                 s.getId().equals("JNETCONF") ||\r
77                                 s.getId().equals("JNETSOL25") ||\r
78                                 s.getId().equals("JNETSOL5") ||\r
79                                 s.getId().equals("JNETSOL0") ||\r
80                                 s.getId().equals("JNETHMM") ||\r
81                                 s.getId().equals("JNETHMM") ||\r
82                                 s.getId().equals("JNETSOL0") ||\r
83                                 s.getId().equals("JNETHMM")) {\r
84                                 // do nothing;\r
85                         } else {\r
86                                 realsequences.add(s);\r
87                         }\r
88                 }\r
89                 return realsequences;\r
90         }\r
91 \r
92 \r
93         public List<FastaSequence> getJpredSequences() {\r
94                 List<FastaSequence> realsequences = new ArrayList<FastaSequence>();\r
95                 for (FastaSequence s : this.sequences) {\r
96                         if (s.getId().equals("jnetpred") || \r
97                                 s.getId().equals("JNETCONF") ||\r
98                                 s.getId().equals("JNETSOL25") ||\r
99                                 s.getId().equals("JNETSOL5") ||\r
100                                 s.getId().equals("JNETSOL0") ||\r
101                                 s.getId().equals("JNETHMM") ||\r
102                                 s.getId().equals("JNETHMM") ||\r
103                                 s.getId().equals("JNETSOL0") ||\r
104                                 s.getId().equals("JNETSOL0") ||\r
105                                 s.getId().equals("QUERY")) {\r
106                                 realsequences.add(s);\r
107                         }\r
108                 }\r
109                 return realsequences;\r
110         }\r
111         /**\r
112          * \r
113          * @return Jpred prediction \r
114          */\r
115         public String getJpredPrediction() {\r
116                 for (FastaSequence s : this.sequences) {\r
117                         if (s.getId().equals("jnetpred")) {\r
118                           return s.getSequence();\r
119                         }\r
120                 }\r
121                 return new String("");\r
122         }\r
123 \r
124         /**\r
125          * \r
126          * @return Jpred JNETSOL25 line\r
127          */\r
128         public String getJpredSOL25() {\r
129                 for (FastaSequence s : this.sequences) {\r
130                         if (s.getId().equals("JNETSOL25")) {\r
131                           return s.getSequence();\r
132                         }\r
133                 }\r
134                 return new String("");\r
135         }\r
136 \r
137         /**\r
138          * \r
139          * @return Jpred JNETSOL5 line\r
140          */\r
141         public String getJpredSOL5() {\r
142                 for (FastaSequence s : this.sequences) {\r
143                         if (s.getId().equals("JNETSOL5")) {\r
144                           return s.getSequence();\r
145                         }\r
146                 }\r
147                 return new String("");\r
148         }\r
149         /**\r
150          * \r
151          * @return Jpred JNETSOL0 line\r
152          */\r
153         public String getJpredSOL0() {\r
154                 for (FastaSequence s : this.sequences) {\r
155                         if (s.getId().equals("JNETSOL0")) {\r
156                           return s.getSequence();\r
157                         }\r
158                 }\r
159                 return new String("");\r
160         }\r
161         /**\r
162          * \r
163          * @return Jpred JNETCONF line\r
164          */\r
165         public String getJpredJNETCONF() {\r
166                 for (FastaSequence s : this.sequences) {\r
167                         if (s.getId().equals("JNETCONF")) {\r
168                           return s.getSequence();\r
169                         }\r
170                 }\r
171                 return new String("");\r
172         }\r
173 \r
174         /**\r
175          * \r
176          * @return list of FastaSequence records\r
177          */\r
178         @Override\r
179         public List<FastaSequence> getSequences() {\r
180                 return this.getRealSeqs();\r
181         }\r
182 \r
183         /**\r
184          * \r
185          * @return a number of sequence in the alignment\r
186          */\r
187         @Override\r
188         public int getSize() {\r
189                 return this.getRealSeqs().size();\r
190         }\r
191 \r
192         /**\r
193          * Please note that this implementation does not take the order of sequences\r
194          * into account!\r
195          */\r
196         @Override\r
197         public boolean equals(Object obj) {\r
198                 if (obj == null) {\r
199                         return false;\r
200                 }\r
201                 if (!(obj instanceof JpredAlignment)) {\r
202                         return false;\r
203                 }\r
204                 JpredAlignment al = (JpredAlignment) obj;\r
205                 if (this.getSize() != al.getSize()) {\r
206                         return false;\r
207                 }\r
208                 if (!this.getMetadata().equals(al.getMetadata())) {\r
209                         return false;\r
210                 }\r
211                 int outerCounter = 0;\r
212                 int matchCounter = 0;\r
213                 for (FastaSequence fs : getSequences()) {\r
214                         outerCounter++;\r
215                         for (FastaSequence fs1 : al.getSequences()) {\r
216                                 if (fs.equals(fs1)) {\r
217                                         matchCounter++;\r
218                                         continue;\r
219                                 }\r
220                         }\r
221                         // Match for at lease one element was not found!\r
222                         if (outerCounter != matchCounter) {\r
223                                 return false;\r
224                         }\r
225                 }\r
226                 return true;\r
227         }\r
228 \r
229 }\r