cf76839fbfe5a455d929553c62d38d98dc47b034
[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 import javax.xml.bind.annotation.XmlSeeAlso;\r
26 \r
27 import compbio.util.annotation.Immutable;\r
28 \r
29 /**\r
30  * Multiple sequence alignment.\r
31  * \r
32  * Does not give any guarantees on the content of individual FastaSequece\r
33  * records. It does not guarantee neither the uniqueness of the names of\r
34  * sequences nor it guarantees the uniqueness of the sequences.\r
35  * \r
36  * @see FastaSequence\r
37  * @see AlignmentMetadata\r
38  * \r
39  * @author pvtroshin\r
40  * \r
41  * @version 1.0 September 2009\r
42  * \r
43  */\r
44 @XmlSeeAlso({JpredAlignment.class})\r
45 @XmlAccessorType(XmlAccessType.FIELD)\r
46 public class Alignment {\r
47 \r
48         protected AlignmentMetadata metadata;\r
49         protected List<FastaSequence> sequences;\r
50 \r
51         protected Alignment() {\r
52                 // This has to has a default constructor for JaxB\r
53         }\r
54 \r
55         /**\r
56          * @param sequences\r
57          * @param program\r
58          * @param gapchar\r
59          */\r
60         public Alignment(List<FastaSequence> sequences, Program program,\r
61                         char gapchar) {\r
62                 this.sequences = sequences;\r
63                 this.metadata = new AlignmentMetadata(program, gapchar);\r
64         }\r
65 \r
66         /**\r
67          * \r
68          * @param sequences\r
69          * @param metadata\r
70          */\r
71         public Alignment(List<FastaSequence> sequences, AlignmentMetadata metadata) {\r
72                 this.sequences = sequences;\r
73                 this.metadata = metadata;\r
74         }\r
75 \r
76         /**\r
77          * \r
78          * @return list of FastaSequence records\r
79          */\r
80         public List<FastaSequence> getSequences() {\r
81                 return sequences;\r
82         }\r
83 \r
84         /**\r
85          * \r
86          * @return a number of sequence in the alignment\r
87          */\r
88         public int getSize() {\r
89                 return this.sequences.size();\r
90         }\r
91 \r
92         /**\r
93          * \r
94          * @return AlignmentMetadata object\r
95          */\r
96         public AlignmentMetadata getMetadata() {\r
97                 return metadata;\r
98         }\r
99 \r
100         @Override\r
101         public String toString() {\r
102                 String sseq = "";\r
103                 for (FastaSequence fs : getSequences()) {\r
104                         sseq += fs.toString() + "\n";\r
105                 }\r
106                 return sseq;\r
107         }\r
108 \r
109         @Override\r
110         public int hashCode() {\r
111                 final int prime = 31;\r
112                 int result = 1;\r
113                 result = prime * result\r
114                                 + ((metadata == null) ? 0 : metadata.hashCode());\r
115                 result = prime * result\r
116                                 + ((sequences == null) ? 0 : sequences.hashCode());\r
117                 return result;\r
118         }\r
119 \r
120         /**\r
121          * Please note that this implementation does not take the order of sequences\r
122          * into account!\r
123          */\r
124         @Override\r
125         public boolean equals(Object obj) {\r
126                 if (obj == null) {\r
127                         return false;\r
128                 }\r
129                 if (!(obj instanceof Alignment)) {\r
130                         return false;\r
131                 }\r
132                 Alignment al = (Alignment) obj;\r
133                 if (this.getSize() != al.getSize()) {\r
134                         return false;\r
135                 }\r
136                 if (!this.getMetadata().equals(al.getMetadata())) {\r
137                         return false;\r
138                 }\r
139                 int outerCounter = 0;\r
140                 int matchCounter = 0;\r
141                 for (FastaSequence fs : getSequences()) {\r
142                         outerCounter++;\r
143                         for (FastaSequence fs1 : al.getSequences()) {\r
144                                 if (fs.equals(fs1)) {\r
145                                         matchCounter++;\r
146                                         continue;\r
147                                 }\r
148                         }\r
149                         // Match for at lease one element was not found!\r
150                         if (outerCounter != matchCounter) {\r
151                                 return false;\r
152                         }\r
153                 }\r
154 \r
155                 return true;\r
156         }\r
157 \r
158 }\r