initial commit
[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 public class BasicSequence implements Sequence {
30
31     private final char[] _mol_sequence;
32     private final Object _identifier;
33     private final TYPE   _type;
34
35     private BasicSequence( final Object identifier, final String mol_sequence, final TYPE type ) {
36         _mol_sequence = mol_sequence.toCharArray();
37         _identifier = identifier;
38         _type = type;
39     }
40
41     // Only use if you know what you are doing!
42     public BasicSequence( final Object identifier, final char[] mol_sequence, final TYPE type ) {
43         _mol_sequence = mol_sequence;
44         _identifier = identifier;
45         _type = type;
46     }
47
48     public Object getIdentifier() {
49         return _identifier;
50     }
51
52     public int getLength() {
53         return _mol_sequence.length;
54     }
55
56     public char[] getMolecularSequence() {
57         return _mol_sequence;
58     }
59
60     public char getResidueAt( final int position ) {
61         return _mol_sequence[ position ];
62     }
63
64     public TYPE getType() {
65         return _type;
66     }
67
68     @Override
69     public String toString() {
70         final StringBuffer sb = new StringBuffer();
71         sb.append( _identifier.toString() );
72         sb.append( " " );
73         sb.append( new String( _mol_sequence ) );
74         return sb.toString();
75     }
76
77     public static Sequence createAaSequence( final Object identifier, final String mol_sequence ) {
78         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
79                 .replaceAll( AA_REGEXP, Character.toString( UNSPECIFIED_AA ) ), TYPE.AA );
80     }
81
82     public static Sequence createDnaSequence( final Object identifier, final String mol_sequence ) {
83         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
84                 .replaceAll( DNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.DNA );
85     }
86
87     public static Sequence createRnaSequence( final Object identifier, final String mol_sequence ) {
88         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
89                 .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );
90     }
91 }