e180481ee22949200bc5c845b2fae51dad1243c2
[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: www.phylosoft.org/forester
26
27 package org.forester.sequence;
28
29 import org.forester.util.ForesterUtil;
30
31 public class BasicSequence implements Sequence {
32
33     private final char[] _mol_sequence;
34     private final String _identifier;
35     private final TYPE   _type;
36
37     private BasicSequence( final String identifier, final String mol_sequence, final TYPE type ) {
38         if ( ForesterUtil.isEmpty( identifier ) ) {
39             throw new IllegalArgumentException( "identifier of sequence cannot be empty");
40         }
41         if ( ForesterUtil.isEmpty( mol_sequence ) ) {
42             throw new IllegalArgumentException( "molecular sequence cannot be empty");
43         }
44         _mol_sequence = mol_sequence.toCharArray();
45         _identifier = identifier;
46         _type = type;
47     }
48
49     // Only use if you know what you are doing!
50     public BasicSequence( final String identifier, final char[] mol_sequence, final TYPE type ) {
51         if ( ForesterUtil.isEmpty( identifier ) ) {
52             throw new IllegalArgumentException( "identifier of sequence cannot be empty");
53         }
54         if ( mol_sequence == null || mol_sequence.length < 1 ) {
55             throw new IllegalArgumentException( "molecular sequence cannot be empty");
56         }
57         _mol_sequence = mol_sequence;
58         _identifier = identifier;
59         _type = type;
60     }
61
62     @Override
63     public String getIdentifier() {
64         return _identifier;
65     }
66
67     @Override
68     public int getLength() {
69         return _mol_sequence.length;
70     }
71
72     @Override
73     public char[] getMolecularSequence() {
74         return _mol_sequence;
75     }
76
77     @Override
78     public char getResidueAt( final int position ) {
79         return _mol_sequence[ position ];
80     }
81
82     @Override
83     public TYPE getType() {
84         return _type;
85     }
86
87     @Override
88     public int getNumberOfGapResidues() {
89         int gaps = 0;
90         for( int i = 0; i < _mol_sequence.length; ++i ) {
91             if ( _mol_sequence[ i ] == GAP ) {
92                 ++gaps;
93             }
94         }
95         return gaps;
96     }
97
98     @Override
99     public boolean equals(Object obj) {
100         if (obj == null) {
101             return false;
102         }
103         if (obj.getClass() != getClass()) {
104             return false;
105         }
106         Sequence other = ( Sequence) obj;
107         if ( getMolecularSequenceAsString() .equals( other.getMolecularSequenceAsString() )) {
108             return true;
109         }
110         return false;
111     }
112  
113     @Override
114     public int hashCode() {
115         return getMolecularSequenceAsString().hashCode();
116     }
117     
118     
119     @Override
120     public String toString() {
121         final StringBuffer sb = new StringBuffer();
122         sb.append( _identifier.toString() );
123         sb.append( ": " );
124         sb.append( getMolecularSequenceAsString() );
125         return sb.toString();
126     }
127
128     public static Sequence copySequence( final Sequence seq ) {
129         final char[] s = new char[ seq.getMolecularSequence().length ];
130         for( int i = 0; i < seq.getMolecularSequence().length; i++ ) {
131             s[ i ] = seq.getMolecularSequence()[ i ];
132         }
133         return new BasicSequence( new String( seq.getIdentifier() ), s, seq.getType() );
134     }
135
136     public static Sequence createAaSequence( final String identifier, final String mol_sequence ) {
137         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
138                 .replaceAll( AA_REGEXP, Character.toString( UNSPECIFIED_AA ) ), TYPE.AA );
139     }
140
141     public static Sequence createDnaSequence( final String identifier, final String mol_sequence ) {
142         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
143                 .replaceAll( DNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.DNA );
144     }
145
146     public static Sequence createRnaSequence( final String identifier, final String mol_sequence ) {
147         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
148                 .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );
149     }
150
151     @Override
152     public String getMolecularSequenceAsString() {
153        
154         return new String( getMolecularSequence() );
155     }
156 }