inprogress
[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 Sequence {
32
33     private final char[] _mol_sequence;
34     private 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     public void setIdentifier( final String id ) {
63         _identifier = id;
64     }
65
66     @Override
67     public String getIdentifier() {
68         return _identifier;
69     }
70
71     @Override
72     public int getLength() {
73         return _mol_sequence.length;
74     }
75
76     @Override
77     public char[] getMolecularSequence() {
78         return _mol_sequence;
79     }
80
81     @Override
82     public char getResidueAt( final int position ) {
83         return _mol_sequence[ position ];
84     }
85
86     @Override
87     public TYPE getType() {
88         return _type;
89     }
90
91     @Override
92     public int getNumberOfGapResidues() {
93         int gaps = 0;
94         for( final char element : _mol_sequence ) {
95             if ( element == GAP ) {
96                 ++gaps;
97             }
98         }
99         return gaps;
100     }
101
102     @Override
103     public boolean equals( final Object obj ) {
104         if ( obj == null ) {
105             return false;
106         }
107         if ( obj.getClass() != getClass() ) {
108             return false;
109         }
110         final Sequence other = ( Sequence ) obj;
111         if ( getMolecularSequenceAsString().equals( other.getMolecularSequenceAsString() ) ) {
112             return true;
113         }
114         return false;
115     }
116
117     @Override
118     public int hashCode() {
119         return getMolecularSequenceAsString().hashCode();
120     }
121
122     @Override
123     public String toString() {
124         final StringBuffer sb = new StringBuffer();
125         sb.append( _identifier.toString() );
126         sb.append( ": " );
127         sb.append( getMolecularSequenceAsString() );
128         return sb.toString();
129     }
130
131     public static Sequence copySequence( final Sequence seq ) {
132         final char[] s = new char[ seq.getMolecularSequence().length ];
133         for( int i = 0; i < seq.getMolecularSequence().length; i++ ) {
134             s[ i ] = seq.getMolecularSequence()[ i ];
135         }
136         return new BasicSequence( new String( seq.getIdentifier() ), s, seq.getType() );
137     }
138
139     public static Sequence createAaSequence( final String identifier, final String mol_sequence ) {
140         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
141                 .replaceAll( AA_REGEXP, Character.toString( UNSPECIFIED_AA ) ), TYPE.AA );
142     }
143
144     public static Sequence createDnaSequence( final String identifier, final String mol_sequence ) {
145         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
146                 .replaceAll( DNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.DNA );
147     }
148
149     public static Sequence createRnaSequence( final String identifier, final String mol_sequence ) {
150         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
151                 .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );
152     }
153
154     @Override
155     public String getMolecularSequenceAsString() {
156         return new String( getMolecularSequence() );
157     }
158 }