Skip to content

Commit 26eaebf

Browse files
committed
Change in the way equals() is evaluated for Results, Hits, Hsps.Modified tests accordingly.
1 parent b78d387 commit 26eaebf

File tree

8 files changed

+282
-121
lines changed

8 files changed

+282
-121
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/search/io/Hit.java

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public abstract class Hit implements Iterable<Hsp>{
2020
private final String hitId;
2121
private final String hitDef;
2222
private final String hitAccession;
23+
/**
24+
* the length of the hit sequence
25+
*/
2326
private final int hitLen;
2427
private final List<Hsp> hsps;
2528
private Sequence hitSequence;
@@ -35,44 +38,37 @@ public Hit(int hitNum, String hitId, String hitDef, String hitAccession, int hit
3538
this.hsps = hsps;
3639
this.hitSequence = hitSequence;
3740
}
38-
39-
/**
40-
* Experimental.
41-
* Wants to return an hashcode designed to allow conceptual comparisons of search results.
41+
42+
@Override
43+
public int hashCode() {
44+
int hash = 3;
45+
hash = 89 * hash + this.hitLen;
46+
hash = 89 * hash + (this.hsps != null ? this.hsps.hashCode() : 0);
47+
return hash;
48+
}
49+
/**
50+
* Implements conceptual comparisons of search results.
4251
* Fields unrelated to search are deliberately not considered.
43-
*
44-
* This latter wants to be a way to compare two hits such that
45-
* if two different queries hit for example chromososome 1,
46-
* with of course different alignments, they will remain still equals to comparison.
4752
* @return
4853
*/
49-
public int hashCode(){
50-
String allInOne = hitId+hitLen;
51-
return allInOne.hashCode();
52-
}
53-
/**
54-
* gets a String to be hashcoded representing contained hsp.
55-
* @return null if hsp does not contain alignment information.
56-
* @return an hashcode representing all hsp
57-
*/
58-
String getHspsHashString(){
59-
String cat = ""+hashCode();
60-
if (hsps != null){
61-
for (Hsp h: hsps){
62-
// hsp hashcode cannot be calculated
63-
if (h.getHspQseq() == null && h.getHspIdentityString() == null && h.getHspHseq()==null) return null;
64-
cat += h.getHspQseq()+"\n"+h.getHspIdentityString()+"\n"+h.getHspHseq()+"\n";
65-
}
54+
@Override
55+
public boolean equals(Object obj) {
56+
if (obj == null) {
57+
return false;
58+
}
59+
if (getClass() != obj.getClass()) {
60+
return false;
6661
}
67-
return cat;
62+
final Hit other = (Hit) obj;
63+
if (this.hitLen != other.hitLen) {
64+
return false;
65+
}
66+
if (this.hsps != other.hsps && (this.hsps == null || !this.hsps.equals(other.hsps))) {
67+
return false;
68+
}
69+
return true;
6870
}
6971

70-
@Override
71-
public boolean equals(Object o){
72-
if (!(o instanceof Hit)) return false;
73-
74-
return o.hashCode() == this.hashCode();
75-
}
7672
public int getHitNum() {
7773
return hitNum;
7874
}

biojava-core/src/main/java/org/biojava/nbio/core/search/io/Hsp.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,43 @@ public abstract class Hsp <S extends Sequence<C>, C extends Compound> {
5050
private Double percentageIdentity = null;
5151
private Integer mismatchCount = null;
5252
private SimpleSequencePair<S, C> returnAln;
53-
53+
54+
@Override
55+
public int hashCode() {
56+
int hash = 5;
57+
hash = 67 * hash + (this.hspQseq != null ? this.hspQseq.hashCode() : 0);
58+
hash = 67 * hash + (this.hspHseq != null ? this.hspHseq.hashCode() : 0);
59+
hash = 67 * hash + (this.hspIdentityString != null ? this.hspIdentityString.hashCode() : 0);
60+
return hash;
61+
}
5462
/**
5563
* Experimental.
56-
* Wants to return an hashcode designed to allow conceptual comparisons of search results.
5764
* Wants to implement conceptual comparisons of search results.
5865
* Fields unrelated to search are deliberately not considered.
59-
* @return
66+
*
67+
* In HSP case, alignment representation strings are considered.
68+
* @return true if HSP alignments are the same,
69+
* false otherwise or if alignment strings are undetermined
6070
*/
61-
public int hashCode(){
62-
String allInOne = hspQseq+"\n"+hspIdentityString+"\n"+hspHseq;
63-
return allInOne.hashCode();
64-
}
65-
6671
@Override
67-
public boolean equals(Object o){
68-
if (!(o instanceof Hsp)) return false;
69-
Hsp other = (Hsp)o;
70-
//if (this.getRepresentationString()==null || other.getRepresentationString()==null) return false;
71-
72-
return o.hashCode() == this.hashCode();
72+
public boolean equals(Object obj) {
73+
if (obj == null) {
74+
return false;
75+
}
76+
if (getClass() != obj.getClass()) {
77+
return false;
78+
}
79+
final Hsp<?, ?> other = (Hsp<?, ?>) obj;
80+
if ((this.hspQseq == null) ? (other.hspQseq != null) : !this.hspQseq.equals(other.hspQseq)) {
81+
return false;
82+
}
83+
if ((this.hspHseq == null) ? (other.hspHseq != null) : !this.hspHseq.equals(other.hspHseq)) {
84+
return false;
85+
}
86+
if ((this.hspIdentityString == null) ? (other.hspIdentityString != null) : !this.hspIdentityString.equals(other.hspIdentityString)) {
87+
return false;
88+
}
89+
return true;
7390
}
7491

7592
public SequencePair<S,C> getAlignment(){

biojava-core/src/main/java/org/biojava/nbio/core/search/io/Result.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,41 @@ public Result(String program, String version, String reference, String dbFile, H
4747
this.hits = hits;
4848
this.querySequence = querySequence;
4949
}
50+
51+
@Override
52+
public int hashCode() {
53+
int hash = 7;
54+
hash = 29 * hash + (this.queryID != null ? this.queryID.hashCode() : 0);
55+
hash = 29 * hash + (this.queryDef != null ? this.queryDef.hashCode() : 0);
56+
hash = 29 * hash + (this.hits != null ? this.hits.hashCode() : 0);
57+
return hash;
58+
}
5059
/**
5160
* Experimental.
5261
* Wants to return an hashcode designed to allow conceptual comparisons of search results.
5362
* Wants to implement conceptual comparisons of search results.
5463
* Fields unrelated to search are deliberately not considered.
5564
* @return
5665
*/
57-
public int hashCode(){
58-
String prefix = queryID+queryDef;
59-
String suffix="";
60-
if (hits != null){
61-
for (Hit h: hits) {
62-
String hashString = h.getHspsHashString();
63-
if (hashString== null) return prefix.hashCode();
64-
suffix += hashString;
65-
}
66-
}
67-
return (prefix+suffix).hashCode();
68-
}
69-
7066
@Override
71-
public boolean equals(Object o){
72-
if (!(o instanceof Result)) return false;
73-
74-
return o.hashCode() == this.hashCode();
67+
public boolean equals(Object obj) {
68+
if (obj == null) {
69+
return false;
70+
}
71+
if (getClass() != obj.getClass()) {
72+
return false;
73+
}
74+
final Result other = (Result) obj;
75+
if ((this.queryID == null) ? (other.queryID != null) : !this.queryID.equals(other.queryID)) {
76+
return false;
77+
}
78+
if ((this.queryDef == null) ? (other.queryDef != null) : !this.queryDef.equals(other.queryDef)) {
79+
return false;
80+
}
81+
if (this.hits != other.hits && (this.hits == null || !this.hits.equals(other.hits))) {
82+
return false;
83+
}
84+
return true;
7585
}
7686

7787
public int getIterationNumber() {

biojava-core/src/test/java/org/biojava/nbio/core/search/io/HspTest.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
import org.junit.Ignore;
1414

1515
/**
16-
* Designed by Paolo Pavan.
17-
* You may want to find my contacts on Github and LinkedIn for code info
18-
* or discuss major changes.
19-
* https://github.com/paolopavan
20-
*
2116
* @author Paolo Pavan
2217
*/
2318

@@ -83,13 +78,42 @@ public void testHashCode() {
8378
int expResult;
8479
int result;
8580

86-
instance = hspImpl;
87-
expResult = 71782805;
81+
instance = new BlastHspBuilder()
82+
.setHspNum(1)
83+
.setHspBitScore(377.211)
84+
.setHspEvalue(8.04143e-093)
85+
.setHspQueryFrom(1)
86+
.setHspQueryTo(224)
87+
.setHspHitFrom(1035)
88+
.setHspHitTo(811)
89+
.setHspQueryFrame(-1)
90+
.setHspIdentity(213)
91+
.setHspPositive(213)
92+
.setHspGaps(5)
93+
.setHspAlignLen(227)
94+
.setHspQseq("CTGACGACAGCCATGCACCACCTGTCTCGACTTTCCCCCGAAGGGCACCTAATGTATCTCTACCTCGTTAGTCGGATGTCAAGACCTGGTAAGGTTTTTTCGCGTATCTTCGAATTAAACCACATACTCCACTGCTTGTGCGG-CCCCCGTCAATTCCTTTGAGTTTCAACCTTGCGGCCGTACTCCC-AGGTGGA-TACTTATTGTGTTAACTCCGGCACGGAAGG")
95+
.setHspHseq("CTGACGACAACCATGCACCACCTGTCTCAACTTTCCCC-GAAGGGCACCTAATGTATCTCTACTTCGTTAGTTGGATGTCAAGACCTGGTAAGGTT-CTTCGCGTTGCTTCGAATTAAACCACATACTCCACTGCTTGTGCGGGCCCCCGTCAATTCCTTTGAGTTTCAACCTTGCGGTCGTACTCCCCAGGTGGATTACTTATTGTGTTAACTCCGGCACAGAAGG")
96+
.setHspIdentityString("||||||||| |||||||||||||||||| ||||||||| |||||||||||||||||||||||| |||||||| ||||||||||||||||||||||| ||||||| |||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||| ||||||||| ||||||| |||||||||||||||||||||||| |||||")
97+
.createBlastHsp();
98+
99+
expResult = hspImpl.hashCode();
88100
result = instance.hashCode();
89101
assertEquals(expResult, result);
90102

91-
instance = uncompleteHsp;
92-
expResult = 679;
103+
instance = new BlastHspBuilder()
104+
.setPercentageIdentity(100.00/100)
105+
.setHspAlignLen(48)
106+
.setMismatchCount(0)
107+
.setHspGaps(0)
108+
.setHspQueryFrom(1)
109+
.setHspQueryTo(48)
110+
.setHspHitFrom(344)
111+
.setHspHitTo(391)
112+
.setHspEvalue(4e-19)
113+
.setHspBitScore(95.6)
114+
.createBlastHsp();
115+
116+
expResult = uncompleteHsp.hashCode();
93117
result = instance.hashCode();
94118
assertEquals(expResult, result);
95119

@@ -106,7 +130,7 @@ public void testHashCode() {
106130
.setHspBitScore(95.6)
107131
.createBlastHsp();
108132

109-
assertFalse(uncompleteHsp.hashCode() == uncompleteHsp2.hashCode());
133+
assertEquals(uncompleteHsp.hashCode(), uncompleteHsp2.hashCode());
110134
}
111135

112136
/**
@@ -137,7 +161,7 @@ public void testEquals() {
137161

138162
assertEquals(o, instance);
139163

140-
// example of Hsp retrieved from uncomplete report. The result is null, hance false
164+
// example of Hsp retrieved from uncomplete report.
141165
// (Those HSP may come from a tabular format, for example)
142166
o = new BlastHspBuilder()
143167
.setPercentageIdentity(100.00/100)
@@ -152,8 +176,7 @@ public void testEquals() {
152176
.setHspBitScore(95.6)
153177
.createBlastHsp();
154178

155-
// At now, check will return false as it could be not determined
156-
assertFalse(uncompleteHsp.equals(o));
179+
assertEquals(uncompleteHsp, o);
157180
}
158181

159182
/**

biojava-core/src/test/java/org/biojava/nbio/core/search/io/blast/BlastTabularParserTest.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testCreateObjects() throws Exception {
7676
Hit expHit1res1;
7777
Hsp expHsp1hit1res1;
7878

79-
String resource = "/org/biojava/nbio/core/search/io/blast/test.two-query.blasttxt";
79+
String resource = "/org/biojava/nbio/core/search/io/blast/small-blastreport.blasttxt";
8080
URL resourceURL = getClass().getResource(resource);
8181
File file = new File(resourceURL.getFile());
8282

@@ -87,25 +87,47 @@ public void testCreateObjects() throws Exception {
8787

8888
BlastHsp hsp1Hit1Res1 = new BlastHspBuilder()
8989
.setHspNum(1)
90-
.setPercentageIdentity(97.40/100)
91-
.setHspAlignLen(77)
92-
.setMismatchCount(2)
90+
.setPercentageIdentity(100.0/100)
91+
.setHspAlignLen(1567)
92+
.setMismatchCount(0)
9393
.setHspGaps(0)
94-
.setHspQueryFrom(774)
95-
.setHspQueryTo(850)
96-
.setHspHitFrom(45396566)
97-
.setHspQueryTo(45396336)
98-
.setHspEvalue(1e-46)
99-
.setHspBitScore(157)
94+
.setHspQueryFrom(1)
95+
.setHspQueryTo(1567)
96+
.setHspHitFrom(616309)
97+
.setHspQueryTo(617875)
98+
.setHspEvalue(0)
99+
.setHspBitScore(2894)
100100
.createBlastHsp();
101101

102+
BlastHsp hsp1Hit1Res2 = new BlastHspBuilder()
103+
.setHspNum(1)
104+
.setPercentageIdentity(100.0/100)
105+
.setHspAlignLen(1567)
106+
.setMismatchCount(0)
107+
.setHspGaps(0)
108+
.setHspQueryFrom(1)
109+
.setHspQueryTo(1567)
110+
.setHspHitFrom(1278699)
111+
.setHspQueryTo(1277133)
112+
.setHspEvalue(0)
113+
.setHspBitScore(2894)
114+
.createBlastHsp();
115+
116+
List<Hsp> hsplist = new ArrayList();
117+
hsplist.add(hsp1Hit1Res1);
118+
hsplist.add(hsp1Hit1Res2);
119+
102120
BlastHit hit1Res1 = new BlastHitBuilder()
103-
.setHitDef("chr15")
121+
.setHitDef("CP000411")
122+
.setHsps(hsplist)
104123
.createBlastHit();
124+
List<Hit> hitlist = new ArrayList();
125+
hitlist.add(hit1Res1);
105126

106127
BlastResult res1 = new BlastResultBuilder()
107-
.setQueryID("Query_1")
108-
.setQueryDef("Dual oxidase (DUOX1_RAT)")
128+
.setQueryID("CP000411_-_16S_rRNA")
129+
.setQueryDef("CP000411_-_16S_rRNA")
130+
.setHits(hitlist)
109131
.createBlastResult();
110132

111133
expRes1 = results.get(0);
@@ -145,20 +167,10 @@ public void testCreateObjects() throws Exception {
145167
.setHspBitScore(95.6)
146168
.createBlastHsp();
147169

148-
hit1Res1 = new BlastHitBuilder()
149-
.setHitDef("KF031625.1.1775")
150-
.createBlastHit();
151-
152-
res1 = new BlastResultBuilder()
153-
.setQueryID("1_759_906_F3")
154-
.setQueryDef("1_759_906_F3")
155-
.createBlastResult();
156-
157170
// results test
158-
assertEquals(expRes1, res1);
159-
// hit test
160-
assertEquals(expHit1res1, hit1Res1);
161-
// hsp test
171+
assertEquals(expRes1.getQueryID(), "1_759_906_F3");
172+
assertEquals(results2.size(), 298);
173+
// only one hsp test
162174
assertEquals(expHsp1hit1res1, hsp1Hit1Res1);
163175
}
164176

0 commit comments

Comments
 (0)