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