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: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.sequence;
29 import org.forester.util.ForesterUtil;
31 public class BasicSequence implements MolecularSequence {
33 private final char[] _mol_sequence;
34 private String _identifier;
35 private final TYPE _type;
38 * Only use if you know what you are doing!
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" );
45 if ( ForesterUtil.isEmpty( mol_sequence ) ) {
46 throw new IllegalArgumentException( "molecular sequence cannot be empty" );
48 _mol_sequence = mol_sequence.toCharArray();
49 _identifier = identifier;
54 * Only use if you know what you are doing!
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" );
61 if ( ( mol_sequence == null ) || ( mol_sequence.length < 1 ) ) {
62 throw new IllegalArgumentException( "molecular sequence cannot be empty" );
64 _mol_sequence = mol_sequence;
65 _identifier = identifier;
69 public void setIdentifier( final String id ) {
74 public String getIdentifier() {
79 public int getLength() {
80 return _mol_sequence.length;
84 public char[] getMolecularSequence() {
89 public char getResidueAt( final int position ) {
90 return _mol_sequence[ position ];
94 public TYPE getType() {
99 public int getNumberOfGapResidues() {
101 for( final char element : _mol_sequence ) {
102 if ( element == GAP ) {
110 public boolean equals( final Object obj ) {
114 if ( obj.getClass() != getClass() ) {
117 final MolecularSequence other = ( MolecularSequence ) obj;
118 if ( getMolecularSequenceAsString().equals( other.getMolecularSequenceAsString() ) ) {
125 public int hashCode() {
126 return getMolecularSequenceAsString().hashCode();
130 public String toString() {
131 final StringBuffer sb = new StringBuffer();
132 sb.append( _identifier.toString() );
134 sb.append( getMolecularSequenceAsString() );
135 return sb.toString();
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 ];
143 return new BasicSequence( new String( seq.getIdentifier() ), s, seq.getType() );
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 );
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 );
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 );
162 public String getMolecularSequenceAsString() {
163 return new String( getMolecularSequence() );
167 public boolean isGapAt( final int position ) {
168 return getResidueAt( position ) == GAP;