Fix problem with the JpredWS service. Now Jws2Client is able to test the service
[jabaws.git] / datamodel / compbio / data / sequence / Alignment.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.data.sequence;\r
20 \r
21 import java.util.List;\r
22 \r
23 import javax.xml.bind.annotation.XmlAccessType;\r
24 import javax.xml.bind.annotation.XmlAccessorType;\r
25 \r
26 import compbio.util.annotation.Immutable;\r
27 \r
28 /**\r
29  * Multiple sequence alignment.\r
30  * \r
31  * Does not give any guarantees on the content of individual FastaSequece\r
32  * records. It does not guarantee neither the uniqueness of the names of\r
33  * sequences nor it guarantees the uniqueness of the sequences.\r
34  * \r
35  * @see FastaSequence\r
36  * @see AlignmentMetadata\r
37  * \r
38  * @author pvtroshin\r
39  * \r
40  * @version 1.0 September 2009\r
41  * \r
42  */\r
43 @XmlAccessorType(XmlAccessType.FIELD)\r
44 public class Alignment {\r
45 \r
46         protected AlignmentMetadata metadata;\r
47         protected List<FastaSequence> sequences;\r
48 \r
49         protected Alignment() {\r
50                 // This has to has a default constructor for JaxB\r
51         }\r
52 \r
53         /**\r
54          * @param sequences\r
55          * @param program\r
56          * @param gapchar\r
57          */\r
58         public Alignment(List<FastaSequence> sequences, Program program,\r
59                         char gapchar) {\r
60                 this.sequences = sequences;\r
61                 this.metadata = new AlignmentMetadata(Program.CLUSTAL, gapchar);\r
62         }\r
63 \r
64         /**\r
65          * \r
66          * @param sequences\r
67          * @param metadata\r
68          */\r
69         public Alignment(List<FastaSequence> sequences, AlignmentMetadata metadata) {\r
70                 this.sequences = sequences;\r
71                 this.metadata = metadata;\r
72         }\r
73 \r
74         /**\r
75          * \r
76          * @return list of FastaSequence records\r
77          */\r
78         public List<FastaSequence> getSequences() {\r
79                 return sequences;\r
80         }\r
81 \r
82         /**\r
83          * \r
84          * @return a number of sequence in the alignment\r
85          */\r
86         public int getSize() {\r
87                 return this.sequences.size();\r
88         }\r
89 \r
90         /**\r
91          * \r
92          * @return AlignmentMetadata object\r
93          */\r
94         public AlignmentMetadata getMetadata() {\r
95                 return metadata;\r
96         }\r
97 \r
98         @Override\r
99         public String toString() {\r
100                 String sseq = "";\r
101                 for (FastaSequence fs : getSequences()) {\r
102                         sseq += fs.toString() + "\n";\r
103                 }\r
104                 return sseq;\r
105         }\r
106 \r
107         @Override\r
108         public int hashCode() {\r
109                 final int prime = 31;\r
110                 int result = 1;\r
111                 result = prime * result\r
112                                 + ((metadata == null) ? 0 : metadata.hashCode());\r
113                 result = prime * result\r
114                                 + ((sequences == null) ? 0 : sequences.hashCode());\r
115                 return result;\r
116         }\r
117 \r
118         /**\r
119          * Please note that this implementation does not take the order of sequences\r
120          * into account!\r
121          */\r
122         @Override\r
123         public boolean equals(Object obj) {\r
124                 if (obj == null) {\r
125                         return false;\r
126                 }\r
127                 if (!(obj instanceof Alignment)) {\r
128                         return false;\r
129                 }\r
130                 Alignment al = (Alignment) obj;\r
131                 if (this.getSize() != al.getSize()) {\r
132                         return false;\r
133                 }\r
134                 if (!this.getMetadata().equals(al.getMetadata())) {\r
135                         return false;\r
136                 }\r
137                 int outerCounter = 0;\r
138                 int matchCounter = 0;\r
139                 for (FastaSequence fs : getSequences()) {\r
140                         outerCounter++;\r
141                         for (FastaSequence fs1 : al.getSequences()) {\r
142                                 if (fs.equals(fs1)) {\r
143                                         matchCounter++;\r
144                                         continue;\r
145                                 }\r
146                         }\r
147                         // Match for at lease one element was not found!\r
148                         if (outerCounter != matchCounter) {\r
149                                 return false;\r
150                         }\r
151                 }\r
152 \r
153                 return true;\r
154         }\r
155 \r
156 }\r