3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
27 package org.forester.sequence;
29 public class BasicSequence implements Sequence {
31 private final char[] _mol_sequence;
32 private final String _identifier;
33 private final TYPE _type;
35 private BasicSequence( final String identifier, final String mol_sequence, final TYPE type ) {
36 _mol_sequence = mol_sequence.toCharArray();
37 _identifier = identifier;
41 // Only use if you know what you are doing!
42 public BasicSequence( final String identifier, final char[] mol_sequence, final TYPE type ) {
43 _mol_sequence = mol_sequence;
44 _identifier = identifier;
49 public String getIdentifier() {
54 public int getLength() {
55 return _mol_sequence.length;
59 public char[] getMolecularSequence() {
64 public char getResidueAt( final int position ) {
65 return _mol_sequence[ position ];
69 public TYPE getType() {
74 public int getNumberOfGapResidues() {
76 for( int i = 0; i < _mol_sequence.length; ++i ) {
77 if ( _mol_sequence[ i ] == GAP ) {
85 public String toString() {
86 final StringBuffer sb = new StringBuffer();
87 sb.append( _identifier.toString() );
89 sb.append( new String( _mol_sequence ) );
93 public static Sequence copySequence( final Sequence seq ) {
94 final char[] s = new char[ seq.getMolecularSequence().length ];
95 for( int i = 0; i < seq.getMolecularSequence().length; i++ ) {
96 s[ i ] = seq.getMolecularSequence()[ i ];
98 return new BasicSequence( new String( seq.getIdentifier() ), s, seq.getType() );
101 public static Sequence createAaSequence( final String identifier, final String mol_sequence ) {
102 return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
103 .replaceAll( AA_REGEXP, Character.toString( UNSPECIFIED_AA ) ), TYPE.AA );
106 public static Sequence createDnaSequence( final String identifier, final String mol_sequence ) {
107 return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
108 .replaceAll( DNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.DNA );
111 public static Sequence createRnaSequence( final String identifier, final String mol_sequence ) {
112 return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
113 .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );