Skip to content

Commit f4c9b69

Browse files
committed
adding a sequence.equals(Sequence) and hashCode class to sequences.
1 parent 5631796 commit f4c9b69

File tree

12 files changed

+247
-37
lines changed

12 files changed

+247
-37
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/alignment/SimpleAlignedSequence.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ public List<C> getAsList() {
269269
return compounds;
270270
}
271271

272+
@Override
273+
public boolean equals(Sequence<C>other){
274+
if ( original.getAsList().size() != other.getAsList().size())
275+
return false;
276+
277+
for ( int i = 0 ; i< original.getAsList().size() ; i++){
278+
if ( ! original.getAsList().get(i).equalsIgnoreCase(other.getAsList().get(i)))
279+
return false;
280+
}
281+
282+
return true;
283+
}
284+
272285
@Override
273286
public C getCompoundAt(int alignmentIndex) {
274287
return alignmentIndex >= 1 && alignmentIndex <= length && isGap(alignmentIndex) ?

biojava-core/src/main/java/org/biojava/nbio/core/sequence/loader/SequenceFileProxyLoader.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ public List<C> getAsList() {
218218

219219
}
220220

221+
@Override
222+
public boolean equals(Sequence<C> other) {
223+
224+
if ( other.getCompoundSet() != getCompoundSet())
225+
return false;
226+
227+
228+
List<C> rawCompounds = getAsList();
229+
List<C> otherCompounds = other.getAsList();
230+
231+
if ( rawCompounds.size() != otherCompounds.size())
232+
return false;
233+
234+
for (int i = 0 ; i < rawCompounds.size() ; i++){
235+
Compound myCompound = rawCompounds.get(i);
236+
Compound otherCompound = otherCompounds.get(i);
237+
if ( ! myCompound.equalsIgnoreCase(otherCompound))
238+
return false;
239+
}
240+
241+
return true;
242+
243+
}
244+
221245
/**
222246
*
223247
* @param bioBegin

biojava-core/src/main/java/org/biojava/nbio/core/sequence/loader/StringProxySequenceReader.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,28 @@ public int countCompounds(C... compounds) {
160160
public SequenceView<C> getInverse() {
161161
return SequenceMixin.inverse(this);
162162
}
163+
164+
@Override
165+
public boolean equals(Sequence<C> other){
166+
if ( other.getCompoundSet() != getCompoundSet())
167+
return false;
168+
169+
170+
List<C> rawCompounds = getAsList();
171+
List<C> otherCompounds = other.getAsList();
172+
173+
if ( rawCompounds.size() != otherCompounds.size())
174+
return false;
175+
176+
for (int i = 0 ; i < rawCompounds.size() ; i++){
177+
Compound myCompound = rawCompounds.get(i);
178+
Compound otherCompound = otherCompounds.get(i);
179+
if ( ! myCompound.equalsIgnoreCase(otherCompound))
180+
return false;
181+
}
182+
183+
return true;
184+
185+
186+
}
163187
}

biojava-core/src/main/java/org/biojava/nbio/core/sequence/loader/UniprotProxySequenceReader.java

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@
2424
*/
2525
package org.biojava.nbio.core.sequence.loader;
2626

27-
import java.io.BufferedReader;
28-
import java.io.ByteArrayInputStream;
29-
import java.io.File;
30-
import java.io.FileNotFoundException;
31-
import java.io.FileReader;
32-
import java.io.FileWriter;
33-
import java.io.IOException;
34-
import java.io.InputStreamReader;
35-
import java.net.HttpURLConnection;
36-
import java.net.URL;
37-
import java.rmi.RemoteException;
38-
import java.util.ArrayList;
39-
import java.util.Iterator;
40-
import java.util.LinkedHashMap;
41-
import java.util.List;
42-
import java.util.regex.Pattern;
43-
44-
import javax.xml.parsers.ParserConfigurationException;
45-
import javax.xml.xpath.XPathExpressionException;
46-
4727
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
4828
import org.biojava.nbio.core.sequence.AccessionID;
4929
import org.biojava.nbio.core.sequence.DataSource;
@@ -55,19 +35,26 @@
5535
import org.biojava.nbio.core.sequence.features.DatabaseReferenceInterface;
5636
import org.biojava.nbio.core.sequence.features.FeaturesKeyWordInterface;
5737
import org.biojava.nbio.core.sequence.storage.SequenceAsStringHelper;
58-
import org.biojava.nbio.core.sequence.template.Compound;
59-
import org.biojava.nbio.core.sequence.template.CompoundSet;
60-
import org.biojava.nbio.core.sequence.template.ProxySequenceReader;
61-
import org.biojava.nbio.core.sequence.template.SequenceMixin;
62-
import org.biojava.nbio.core.sequence.template.SequenceProxyView;
63-
import org.biojava.nbio.core.sequence.template.SequenceView;
38+
import org.biojava.nbio.core.sequence.template.*;
6439
import org.biojava.nbio.core.util.XMLHelper;
6540
import org.slf4j.Logger;
6641
import org.slf4j.LoggerFactory;
6742
import org.w3c.dom.Document;
6843
import org.w3c.dom.Element;
6944
import org.xml.sax.SAXException;
7045

46+
import javax.xml.parsers.ParserConfigurationException;
47+
import javax.xml.xpath.XPathExpressionException;
48+
import java.io.*;
49+
import java.net.HttpURLConnection;
50+
import java.net.URL;
51+
import java.rmi.RemoteException;
52+
import java.util.ArrayList;
53+
import java.util.Iterator;
54+
import java.util.LinkedHashMap;
55+
import java.util.List;
56+
import java.util.regex.Pattern;
57+
7158
/**
7259
*
7360
* Pass in a Uniprot ID and this ProxySequenceReader when passed to a ProteinSequence will get the sequence data and other data elements
@@ -243,6 +230,30 @@ public List<C> getAsList() {
243230
return this.parsedCompounds;
244231
}
245232

233+
@Override
234+
public boolean equals(Sequence<C> other){
235+
if ( other.getCompoundSet() != getCompoundSet())
236+
return false;
237+
238+
239+
List<C> rawCompounds = getAsList();
240+
List<C> otherCompounds = other.getAsList();
241+
242+
if ( rawCompounds.size() != otherCompounds.size())
243+
return false;
244+
245+
for (int i = 0 ; i < rawCompounds.size() ; i++){
246+
Compound myCompound = rawCompounds.get(i);
247+
Compound otherCompound = otherCompounds.get(i);
248+
if ( ! myCompound.equalsIgnoreCase(otherCompound))
249+
return false;
250+
}
251+
252+
return true;
253+
254+
255+
}
256+
246257
/**
247258
*
248259
* @return

biojava-core/src/main/java/org/biojava/nbio/core/sequence/storage/ArrayListSequenceReader.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public String getSequenceAsString() {
8585
return getSequenceAsString(1, getLength(), Strand.POSITIVE);
8686
}
8787

88+
public boolean equals(Sequence<C>other){
89+
if ( parsedCompounds.size() != other.getAsList().size())
90+
return false;
91+
92+
for ( int i = 0 ; i< parsedCompounds.size() ; i++){
93+
if ( ! parsedCompounds.get(i).equalsIgnoreCase(other.getAsList().get(i)))
94+
return false;
95+
}
96+
97+
return true;
98+
}
99+
88100
/**
89101
*
90102
* @param begin

biojava-core/src/main/java/org/biojava/nbio/core/sequence/storage/BitSequenceReader.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ public boolean equals(Object o) {
199199
return false;
200200
}
201201

202+
public boolean equals(Sequence<C> o){
203+
204+
if(Equals.classEqual(this, o)) {
205+
@SuppressWarnings("unchecked")
206+
BitSequenceReader<C> that = (BitSequenceReader<C>)o;
207+
return Equals.equal(this.accession, that.accession) &&
208+
Equals.equal(this.worker, that.worker);
209+
}
210+
return false;
211+
212+
}
213+
202214
/**
203215
* The logic of working with a bit has been separated out into this class
204216
* to help developers create the bit data structures without having to

biojava-core/src/main/java/org/biojava/nbio/core/sequence/storage/JoiningSequenceReader.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,29 @@ public List<C> getAsList() {
298298
return SequenceMixin.toList(this);
299299
}
300300

301+
@Override
302+
public boolean equals(Sequence<C> other) {
303+
304+
if ( other.getCompoundSet() != getCompoundSet())
305+
return false;
306+
307+
308+
List<C> rawCompounds = getAsList();
309+
List<C> otherCompounds = other.getAsList();
310+
311+
if ( rawCompounds.size() != otherCompounds.size())
312+
return false;
313+
314+
for (int i = 0 ; i < rawCompounds.size() ; i++){
315+
Compound myCompound = rawCompounds.get(i);
316+
Compound otherCompound = otherCompounds.get(i);
317+
if ( ! myCompound.equalsIgnoreCase(otherCompound))
318+
return false;
319+
}
320+
321+
return true;
322+
}
323+
301324

302325
@Override
303326
public int getIndexOf(C compound) {

biojava-core/src/main/java/org/biojava/nbio/core/sequence/storage/SingleCompoundSequenceReader.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,14 @@ public boolean equals(Object o) {
210210
}
211211
return false;
212212
}
213+
214+
public boolean equals(Sequence<C> o) {
215+
if(Equals.classEqual(this, o)) {
216+
SingleCompoundSequenceReader<C> that = (SingleCompoundSequenceReader<C>)o;
217+
return Equals.equal(compound, that.compound) &&
218+
Equals.equal(compoundSet, that.compoundSet) &&
219+
Equals.equal(length, that.length);
220+
}
221+
return false;
222+
}
213223
}

biojava-core/src/main/java/org/biojava/nbio/core/sequence/template/AbstractSequence.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ public Integer getBioBegin() {
159159
/**
160160
* @param bioBegin the bioBegin to set
161161
*/
162-
public void setBioBegin(Integer begin) {
163-
this.bioBegin = begin;
162+
public void setBioBegin(Integer bioBegin) {
163+
this.bioBegin = bioBegin;
164164
}
165165

166166
/**
@@ -177,8 +177,8 @@ public Integer getBioEnd() {
177177
/**
178178
* @param bioEnd the bioEnd to set
179179
*/
180-
public void setBioEnd(Integer end) {
181-
this.bioEnd = end;
180+
public void setBioEnd(Integer bioEnd) {
181+
this.bioEnd = bioEnd;
182182
}
183183

184184
/**
@@ -207,7 +207,7 @@ public AnnotationType getAnnotationType() {
207207
}
208208

209209
/**
210-
* @param annotation the annotation to set
210+
* @param annotationType the annotation to set
211211
*/
212212
public void setAnnotationType(AnnotationType annotationType) {
213213
this.annotationType = annotationType;
@@ -342,7 +342,6 @@ public List<FeatureInterface<AbstractSequence<C>, C>> getFeatures(String feature
342342

343343
/**
344344
* Return features at a sequence position
345-
* @param featureType
346345
* @param bioSequencePosition
347346
* @return
348347
*/
@@ -493,7 +492,7 @@ public TaxonomyID getTaxonomy() {
493492
}
494493

495494
/**
496-
* @param species the species to set
495+
* @param taxonomy the species to set
497496
*/
498497
public void setTaxonomy(TaxonomyID taxonomy) {
499498
this.taxonomy = taxonomy;
@@ -520,6 +519,35 @@ public void setCompoundSet(CompoundSet<C> compoundSet) {
520519
this.compoundSet = compoundSet;
521520
}
522521

522+
523+
public boolean equals(Sequence<C> other){
524+
if ( other.getCompoundSet() != getCompoundSet())
525+
return false;
526+
527+
528+
List<C> rawCompounds = getAsList();
529+
List<C> otherCompounds = other.getAsList();
530+
531+
if ( rawCompounds.size() != otherCompounds.size())
532+
return false;
533+
534+
for (int i = 0 ; i < rawCompounds.size() ; i++){
535+
Compound myCompound = rawCompounds.get(i);
536+
Compound otherCompound = otherCompounds.get(i);
537+
if ( ! myCompound.equalsIgnoreCase(otherCompound))
538+
return false;
539+
}
540+
541+
return true;
542+
543+
544+
}
545+
546+
public int hashCode(){
547+
String s = getSequenceAsString();
548+
return s.hashCode();
549+
}
550+
523551
@Override
524552
public String toString() {
525553
return getSequenceAsString();
@@ -552,8 +580,8 @@ private SequenceReader<C> getSequenceStorage() {
552580

553581
/**
554582
*
555-
* @param begin
556-
* @param end
583+
* @param bioStart
584+
* @param bioEnd
557585
* @param strand
558586
* @return
559587
*/
@@ -579,7 +607,8 @@ public String getSequenceAsString() {
579607
*/
580608
@Override
581609
public List<C> getAsList() {
582-
return SequenceMixin.toList(this);
610+
611+
return sequenceStorage.getAsList();
583612
}
584613

585614
/**
@@ -661,5 +690,5 @@ public SequenceView<C> getInverse() {
661690
return SequenceMixin.inverse(this);
662691
}
663692

664-
//TODO needs equals and hashcode
693+
665694
}

biojava-core/src/main/java/org/biojava/nbio/core/sequence/template/Sequence.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public interface Sequence<C extends Compound> extends Iterable<C>, Accessioned {
7979
*/
8080
public List<C> getAsList();
8181

82+
public boolean equals(Sequence<C> other);
83+
84+
public int hashCode();
85+
8286
/**
8387
* Returns a portion of the sequence from the different positions. This is
8488
* indexed from 1

0 commit comments

Comments
 (0)