001 /*
002 * BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence. This should
006 * be distributed with the code. If you do not have a copy,
007 * see:
008 *
009 * http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors. These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 * http://www.biojava.org/
019 *
020 * Created on 01-21-2010
021 *
022 * @author Richard Holland
023 * @auther Scooter Willis
024 *
025 */
026 package org.biojava3.core.sequence.loader;
027
028 import java.util.ArrayList;
029 import java.util.Iterator;
030 import java.util.List;
031 import org.biojava3.core.sequence.AccessionID;
032
033 import org.biojava3.core.sequence.template.SequenceProxyView;
034 import org.biojava3.core.sequence.template.Compound;
035 import org.biojava3.core.exceptions.CompoundNotFoundError;
036 import org.biojava3.core.sequence.Strand;
037
038 import org.biojava3.core.sequence.storage.SequenceAsStringHelper;
039 import org.biojava3.core.sequence.template.CompoundSet;
040 import org.biojava3.core.sequence.template.ProxySequenceReader;
041 import org.biojava3.core.sequence.template.SequenceMixin;
042 import org.biojava3.core.sequence.template.SequenceView;
043
044 /**
045 * An example of a ProxySequenceReader that is created from a String. Used for testing
046 * @author Scooter Willis <willishf at gmail dot com>
047 * @param <C>
048 */
049
050 public class StringProxySequenceReader<C extends Compound> implements ProxySequenceReader<C> {
051
052 private String sequence;
053 private CompoundSet<C> compoundSet;
054 private List<C> parsedCompounds = new ArrayList<C>();
055
056
057 public StringProxySequenceReader(String sequence, CompoundSet<C> compoundSet) {
058 this.sequence = sequence;
059 setCompoundSet(compoundSet);
060 setContents(sequence);
061 }
062
063 public void setCompoundSet(CompoundSet<C> compoundSet) {
064 this.compoundSet = compoundSet;
065 }
066
067 public void setContents(String sequence) {
068 // Horrendously inefficient - pretty much the way the old BJ did things.
069 // TODO Should be optimised.
070 this.parsedCompounds.clear();
071 for (int i = 0; i < sequence.length();) {
072 String compoundStr = null;
073 C compound = null;
074 for (int compoundStrLength = 1; compound == null && compoundStrLength <= compoundSet.getMaxSingleCompoundStringLength(); compoundStrLength++) {
075 compoundStr = sequence.substring(i, i + compoundStrLength);
076 compound = compoundSet.getCompoundForString(compoundStr);
077 }
078 if (compound == null) {
079 throw new CompoundNotFoundError(compoundStr);
080 } else {
081 i += compoundStr.length();
082 }
083 this.parsedCompounds.add(compound);
084 }
085 }
086
087 public int getLength() {
088 return this.parsedCompounds.size();
089 }
090
091 public C getCompoundAt(int position) {
092 return this.parsedCompounds.get(position - 1);
093 }
094
095 public int getIndexOf(C compound) {
096 return this.parsedCompounds.indexOf(compound) + 1;
097 }
098
099 public int getLastIndexOf(C compound) {
100 return this.parsedCompounds.lastIndexOf(compound) + 1;
101 }
102
103
104 public String toString() {
105 return getSequenceAsString();
106 }
107
108 public String getSequenceAsString() {
109 return sequence;
110 }
111
112 public List<C> getAsList() {
113 return this.parsedCompounds;
114 }
115
116
117
118 public String getSequenceAsString(Integer bioBegin, Integer bioEnd,Strand strand) {
119 SequenceAsStringHelper<C> sequenceAsStringHelper = new SequenceAsStringHelper<C>();
120 return sequenceAsStringHelper.getSequenceAsString(this.parsedCompounds, compoundSet, bioBegin, bioEnd, strand);
121 }
122
123 public SequenceView<C> getSubSequence(final Integer bioBegin, final Integer bioEnd) {
124 return new SequenceProxyView<C>(StringProxySequenceReader.this,bioBegin,bioEnd);
125 }
126
127 public Iterator<C> iterator() {
128 return this.parsedCompounds.iterator();
129 }
130
131 public CompoundSet<C> getCompoundSet() {
132 return compoundSet;
133 }
134
135
136 public AccessionID getAccession() {
137 throw new UnsupportedOperationException("Not supported yet.");
138 }
139
140
141 public int countCompounds(C... compounds) {
142 throw new UnsupportedOperationException("Not supported yet.");
143 }
144
145 @Override
146 public SequenceView<C> getInverse() {
147 return SequenceMixin.inverse(this);
148 }
149 }