Change header template for a new version
[jabaws.git] / datamodel / compbio / data / sequence / FastaSequence.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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.regex.Matcher;\r
22 import java.util.regex.Pattern;\r
23 \r
24 import javax.xml.bind.annotation.XmlAccessType;\r
25 import javax.xml.bind.annotation.XmlAccessorType;\r
26 \r
27 import compbio.util.SysPrefs;\r
28 import compbio.util.annotation.Immutable;\r
29 \r
30 /**\r
31  * A FASTA formatted sequence. Please note that this class does not make any\r
32  * assumptions as to what sequence it stores e.g. it could be nucleotide,\r
33  * protein or even gapped alignment sequence! The only guarantee it makes is\r
34  * that the sequence does not contain white space characters e.g. spaces, new\r
35  * lines etc\r
36  * \r
37  * @author pvtroshin\r
38  * \r
39  * @version 1.0 September 2009\r
40  */\r
41 \r
42 @XmlAccessorType(XmlAccessType.FIELD)\r
43 @Immutable\r
44 public class FastaSequence {\r
45 \r
46         /**\r
47          * Sequence id\r
48          */\r
49         private String id;\r
50 \r
51         // TODO what about gapped sequence here! should be indicated\r
52         /**\r
53          * Returns the string representation of sequence\r
54          */\r
55         private String sequence;\r
56 \r
57         FastaSequence() {\r
58                 // Default constructor for JaxB\r
59         }\r
60 \r
61         /**\r
62          * Upon construction the any whitespace characters are removed from the\r
63          * sequence\r
64          * \r
65          * @param id\r
66          * @param sequence\r
67          */\r
68         public FastaSequence(String id, String sequence) {\r
69                 this.id = id;\r
70                 this.sequence = SequenceUtil.cleanSequence(sequence);\r
71         }\r
72 \r
73         /**\r
74          * Gets the value of id\r
75          * \r
76          * @return the value of id\r
77          */\r
78         public String getId() {\r
79                 return this.id;\r
80         }\r
81 \r
82         /**\r
83          * Gets the value of sequence\r
84          * \r
85          * @return the value of sequence\r
86          */\r
87         public String getSequence() {\r
88                 return this.sequence;\r
89         }\r
90 \r
91         public static int countMatchesInSequence(final String theString,\r
92                         final String theRegExp) {\r
93                 final Pattern p = Pattern.compile(theRegExp);\r
94                 final Matcher m = p.matcher(theString);\r
95                 int cnt = 0;\r
96                 while (m.find()) {\r
97                         cnt++;\r
98                 }\r
99                 return cnt;\r
100         }\r
101 \r
102         public String getFormattedFasta() {\r
103                 return getFormatedSequence(80);\r
104         }\r
105 \r
106         /**\r
107          * \r
108          * @return one line name, next line sequence, no matter what the sequence\r
109          *         length is\r
110          */\r
111         public String getOnelineFasta() {\r
112                 String fasta = ">" + getId() + SysPrefs.newlinechar;\r
113                 fasta += getSequence() + SysPrefs.newlinechar;\r
114                 return fasta;\r
115         }\r
116 \r
117         /**\r
118          * Format sequence per width letter in one string. Without spaces.\r
119          * \r
120          * @return multiple line formated sequence, one line width letters length\r
121          * \r
122          */\r
123         public String getFormatedSequence(final int width) {\r
124                 if (sequence == null) {\r
125                         return "";\r
126                 }\r
127 \r
128                 assert width >= 0 : "Wrong width parameter ";\r
129 \r
130                 final StringBuilder sb = new StringBuilder(sequence);\r
131                 // int tail = nrOfWindows % WIN_SIZE;\r
132                 // final int turns = (nrOfWindows - tail) / WIN_SIZE;\r
133 \r
134                 int tailLen = sequence.length() % width;\r
135                 // add up inserted new line chars\r
136                 int nchunks = (sequence.length() - tailLen) / width;\r
137                 int nlineCharcounter = 0;\r
138                 int insPos = 0;\r
139                 for (int i = 1; i <= nchunks; i++) {\r
140                         insPos = width * i + nlineCharcounter;\r
141                         // to prevent inserting new line in the very end of a sequence then\r
142                         // it would have failed.\r
143                         if (sb.length() <= insPos) {\r
144                                 break;\r
145                         }\r
146                         sb.insert(insPos, "\n");\r
147                         nlineCharcounter++;\r
148                 }\r
149                 // sb.insert(insPos + tailLen, "\n");\r
150                 return sb.toString();\r
151         }\r
152 \r
153         /**\r
154          * \r
155          * @return sequence length\r
156          */\r
157         public int getLength() {\r
158                 return this.sequence.length();\r
159         }\r
160 \r
161         /**\r
162          * Same as oneLineFasta\r
163          */\r
164         @Override\r
165         public String toString() {\r
166                 return this.getOnelineFasta();\r
167         }\r
168 \r
169         @Override\r
170         public int hashCode() {\r
171                 final int prime = 17;\r
172                 int result = 1;\r
173                 result = prime * result + ((id == null) ? 0 : id.hashCode());\r
174                 result = prime * result\r
175                                 + ((sequence == null) ? 0 : sequence.hashCode());\r
176                 return result;\r
177         }\r
178 \r
179         @Override\r
180         public boolean equals(Object obj) {\r
181                 if (obj == null) {\r
182                         return false;\r
183                 }\r
184                 if (!(obj instanceof FastaSequence)) {\r
185                         return false;\r
186                 }\r
187                 FastaSequence fs = (FastaSequence) obj;\r
188                 if (!fs.getId().equals(this.getId())) {\r
189                         return false;\r
190                 }\r
191                 if (!fs.getSequence().equalsIgnoreCase(this.getSequence())) {\r
192                         return false;\r
193                 }\r
194                 return true;\r
195         }\r
196 \r
197 }\r