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