JAL-2797 added constructor including embedded/standalone boolean
[jalview.git] / forester / java / src / org / forester / sequence / BasicSequence.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
5 //
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.sequence;
28
29 import org.forester.util.ForesterUtil;
30
31 public class BasicSequence implements MolecularSequence {
32
33     private final char[] _mol_sequence;
34     private String       _identifier;
35     private final TYPE   _type;
36
37     /**
38      * Only use if you know what you are doing!
39      *
40      */
41     public BasicSequence( final String identifier, final String mol_sequence, final TYPE type ) {
42         check( identifier, mol_sequence );
43         _mol_sequence = mol_sequence.toCharArray();
44         _identifier = identifier;
45         _type = type;
46     }
47
48     private static final void check( final String identifier, final String mol_sequence ) {
49         if ( ForesterUtil.isEmpty( identifier ) ) {
50             throw new IllegalArgumentException( "identifier of sequence cannot be empty" );
51         }
52         if ( ForesterUtil.isEmpty( mol_sequence ) ) {
53             throw new IllegalArgumentException( "molecular sequence cannot be empty" );
54         }
55     }
56
57     /**
58      * Only use if you know what you are doing!
59      *
60      */
61     public BasicSequence( final String identifier, final char[] mol_sequence, final TYPE type ) {
62         if ( ForesterUtil.isEmpty( identifier ) ) {
63             throw new IllegalArgumentException( "identifier of sequence cannot be empty" );
64         }
65         if ( ( mol_sequence == null ) || ( mol_sequence.length < 1 ) ) {
66             throw new IllegalArgumentException( "molecular sequence cannot be empty" );
67         }
68         _mol_sequence = mol_sequence;
69         _identifier = identifier;
70         _type = type;
71     }
72
73     public void setIdentifier( final String id ) {
74         _identifier = id;
75     }
76
77     @Override
78     public String getIdentifier() {
79         return _identifier;
80     }
81
82     @Override
83     public int getLength() {
84         return _mol_sequence.length;
85     }
86
87     @Override
88     public char[] getMolecularSequence() {
89         return _mol_sequence;
90     }
91
92     @Override
93     public char getResidueAt( final int position ) {
94         return _mol_sequence[ position ];
95     }
96
97     @Override
98     public TYPE getType() {
99         return _type;
100     }
101
102     @Override
103     public int getNumberOfGapResidues() {
104         int gaps = 0;
105         for( final char element : _mol_sequence ) {
106             if ( element == GAP ) {
107                 ++gaps;
108             }
109         }
110         return gaps;
111     }
112
113     @Override
114     public boolean equals( final Object obj ) {
115         if ( obj == null ) {
116             return false;
117         }
118         if ( obj.getClass() != getClass() ) {
119             return false;
120         }
121         final MolecularSequence other = ( MolecularSequence ) obj;
122         if ( getMolecularSequenceAsString().equals( other.getMolecularSequenceAsString() ) ) {
123             return true;
124         }
125         return false;
126     }
127
128     @Override
129     public int hashCode() {
130         return getMolecularSequenceAsString().hashCode();
131     }
132
133     @Override
134     public String toString() {
135         final StringBuffer sb = new StringBuffer();
136         sb.append( _identifier.toString() );
137         sb.append( ": " );
138         sb.append( getMolecularSequenceAsString() );
139         return sb.toString();
140     }
141
142     public static MolecularSequence copySequence( final MolecularSequence seq ) {
143         final char[] s = new char[ seq.getMolecularSequence().length ];
144         for( int i = 0; i < seq.getMolecularSequence().length; i++ ) {
145             s[ i ] = seq.getMolecularSequence()[ i ];
146         }
147         return new BasicSequence( new String( seq.getIdentifier() ), s, seq.getType() );
148     }
149
150     public static MolecularSequence createSequence( final String identifier, final String mol_sequence ) {
151         check( identifier, mol_sequence );
152         final TYPE type = ForesterUtil.guessMolecularSequenceType( mol_sequence );
153         final String re;
154         final char repl;
155         if ( type == TYPE.AA ) {
156             re = AA_REGEXP;
157             repl = UNSPECIFIED_AA;
158         }
159         else if ( type == TYPE.DNA ) {
160             re = DNA_REGEXP;
161             repl = UNSPECIFIED_NUC;
162         }
163         else if ( type == TYPE.RNA ) {
164             re = RNA_REGEXP;
165             repl = UNSPECIFIED_NUC;
166         }
167         else {
168             throw new IllegalArgumentException( "could not determine sequence type for: " + mol_sequence);
169         }
170         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
171                                   .replaceAll( re, Character.toString( repl ) ), type );
172     }
173     
174     public static MolecularSequence createGeneralSequence( final String identifier, final String mol_sequence ) {
175         check( identifier, mol_sequence );
176         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR 
177                                   ), TYPE.GENERAL );
178     }
179     
180     public static MolecularSequence createAaSequence( final String identifier, final String mol_sequence ) {
181         check( identifier, mol_sequence );
182         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
183                                   .replaceAll( AA_REGEXP, Character.toString( UNSPECIFIED_AA ) ), TYPE.AA );
184     }
185
186     public static MolecularSequence createDnaSequence( final String identifier, final String mol_sequence ) {
187         check( identifier, mol_sequence );
188         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
189                                   .replaceAll( DNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.DNA );
190     }
191
192     public static MolecularSequence createRnaSequence( final String identifier, final String mol_sequence ) {
193         check( identifier, mol_sequence );
194         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
195                                   .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );
196     }
197
198     @Override
199     public String getMolecularSequenceAsString() {
200         return new String( getMolecularSequence() );
201     }
202
203     @Override
204     public boolean isGapAt( final int position ) {
205         return getResidueAt( position ) == GAP;
206     }
207 }