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 */ 026package org.biojava.nbio.core.sequence.loader; 027 028import org.biojava.nbio.core.exceptions.CompoundNotFoundException; 029import org.biojava.nbio.core.sequence.AccessionID; 030import org.biojava.nbio.core.sequence.Strand; 031import org.biojava.nbio.core.sequence.storage.SequenceAsStringHelper; 032import org.biojava.nbio.core.sequence.template.*; 033import org.biojava.nbio.core.util.Equals; 034 035import java.util.ArrayList; 036import java.util.Iterator; 037import java.util.List; 038 039/** 040 * An example of a ProxySequenceReader that is created from a String. Used for testing 041 * @author Scooter Willis <willishf at gmail dot com> 042 * @param <C> 043 */ 044 045public class StringProxySequenceReader<C extends Compound> implements ProxySequenceReader<C> { 046 047 private String sequence; 048 private CompoundSet<C> compoundSet; 049 private List<C> parsedCompounds = new ArrayList<C>(); 050 051 public StringProxySequenceReader() {} 052 053 public StringProxySequenceReader(String sequence, CompoundSet<C> compoundSet) throws CompoundNotFoundException { 054 this.sequence = sequence; 055 setCompoundSet(compoundSet); 056 setContents(sequence); 057 } 058 059 @Override 060 public void setCompoundSet(CompoundSet<C> compoundSet) { 061 this.compoundSet = compoundSet; 062 } 063 064 @Override 065 public void setContents(String sequence) throws CompoundNotFoundException { 066 // Horrendously inefficient - pretty much the way the old BJ did things. 067 // TODO Should be optimised. 068 this.sequence = sequence; 069 this.parsedCompounds.clear(); 070 for (int i = 0; i < sequence.length();) { 071 String compoundStr = null; 072 C compound = null; 073 for (int compoundStrLength = 1; compound == null && compoundStrLength <= compoundSet.getMaxSingleCompoundStringLength(); compoundStrLength++) { 074 compoundStr = sequence.substring(i, i + compoundStrLength); 075 compound = compoundSet.getCompoundForString(compoundStr); 076 } 077 if (compound == null) { 078 throw new CompoundNotFoundException("Compound "+compoundStr+" not found"); 079 } else { 080 i += compoundStr.length(); 081 } 082 this.parsedCompounds.add(compound); 083 } 084 } 085 086 public void setContents(String sequence, ArrayList features) throws CompoundNotFoundException{ 087 setContents(sequence); 088 } 089 090 @Override 091 public int getLength() { 092 return this.parsedCompounds.size(); 093 } 094 095 @Override 096 public C getCompoundAt(int position) { 097 return this.parsedCompounds.get(position - 1); 098 } 099 100 @Override 101 public int getIndexOf(C compound) { 102 return this.parsedCompounds.indexOf(compound) + 1; 103 } 104 105 @Override 106 public int getLastIndexOf(C compound) { 107 return this.parsedCompounds.lastIndexOf(compound) + 1; 108 } 109 110 111 @Override 112 public String toString() { 113 return getSequenceAsString(); 114 } 115 116 @Override 117 public String getSequenceAsString() { 118 return sequence; 119 } 120 121 @Override 122 public List<C> getAsList() { 123 return this.parsedCompounds; 124 } 125 126 127 128 public String getSequenceAsString(Integer bioBegin, Integer bioEnd,Strand strand) { 129 SequenceAsStringHelper<C> sequenceAsStringHelper = new SequenceAsStringHelper<C>(); 130 return sequenceAsStringHelper.getSequenceAsString(this.parsedCompounds, compoundSet, bioBegin, bioEnd, strand); 131 } 132 133 @Override 134 public SequenceView<C> getSubSequence(final Integer bioBegin, final Integer bioEnd) { 135 return new SequenceProxyView<C>(StringProxySequenceReader.this,bioBegin,bioEnd); 136 } 137 138 @Override 139 public Iterator<C> iterator() { 140 return this.parsedCompounds.iterator(); 141 } 142 143 @Override 144 public CompoundSet<C> getCompoundSet() { 145 return compoundSet; 146 } 147 148 149 @Override 150 public AccessionID getAccession() { 151 throw new UnsupportedOperationException("Not supported yet."); 152 } 153 154 155 @Override 156 public int countCompounds(C... compounds) { 157 throw new UnsupportedOperationException("Not supported yet."); 158 } 159 160 @Override 161 public SequenceView<C> getInverse() { 162 return SequenceMixin.inverse(this); 163 } 164 165 @Override 166 public boolean equals(Object o){ 167 168 if(! Equals.classEqual(this, o)) { 169 return false; 170 } 171 172 Sequence<C> other = (Sequence<C>)o; 173 if ( other.getCompoundSet() != getCompoundSet()) 174 return false; 175 176 List<C> rawCompounds = getAsList(); 177 List<C> otherCompounds = other.getAsList(); 178 179 if ( rawCompounds.size() != otherCompounds.size()) 180 return false; 181 182 for (int i = 0 ; i < rawCompounds.size() ; i++){ 183 Compound myCompound = rawCompounds.get(i); 184 Compound otherCompound = otherCompounds.get(i); 185 if ( ! myCompound.equalsIgnoreCase(otherCompound)) 186 return false; 187 } 188 return true; 189 } 190 191 @Override 192 public int hashCode(){ 193 String s = getSequenceAsString(); 194 return s.hashCode(); 195 } 196}