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