Repository navigation
Expand file tree
/
Copy pathAbstractCompoundSet.java
More file actions
213 lines (178 loc) · 5.73 KB
/
Copy pathAbstractCompoundSet.java
File metadata and controls
213 lines (178 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on 01-21-2010
*/
package org.biojava.nbio.core.sequence.template;
import org.biojava.nbio.core.util.Equals;
import org.biojava.nbio.core.util.Hashcoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
*
* @author Andy Yates
*
* @param <C> The compound this set will contain
*/
public abstract class AbstractCompoundSet<C extends Compound> implements CompoundSet<C> {
private final static Logger logger = LoggerFactory.getLogger(AbstractCompoundSet.class);
private Map<CharSequence, C> charSeqToCompound = new HashMap<CharSequence, C>();
private int maxCompoundCharSequenceLength = -1;
private Boolean compoundStringLengthEqual = null;
Map<C,Set<C>> equivalentsMap = new HashMap<C, Set<C>>();
protected void addCompound(C compound, C lowerCasedCompound, Iterable<C> equivalents) {
addCompound(compound);
addCompound(lowerCasedCompound);
addEquivalent(compound, lowerCasedCompound);
addEquivalent(lowerCasedCompound, compound);
for(C equivalent: equivalents) {
addEquivalent(compound, equivalent);
addEquivalent(equivalent, compound);
addEquivalent(lowerCasedCompound, equivalent);
addEquivalent(equivalent, lowerCasedCompound);
}
}
protected void addCompound(C compound, C lowerCasedCompound, C... equivalents) {
List<C> equiv = new ArrayList<C>(equivalents.length);
equiv.addAll(Arrays.asList(equivalents));
addCompound(compound, lowerCasedCompound, equiv);
}
protected void addEquivalent(C compound, C equivalent) {
Set<C> s = equivalentsMap.get(compound);
if ( s == null){
s = new HashSet<C>();
equivalentsMap.put(compound, s);
}
s.add( equivalent);
}
protected void addCompound(C compound) {
charSeqToCompound.put(compound.toString(), compound);
maxCompoundCharSequenceLength = -1;
compoundStringLengthEqual = null;
}
@Override
public String getStringForCompound(C compound) {
return compound.toString();
}
@Override
public C getCompoundForString(String string) {
if(string == null) {
throw new IllegalArgumentException("Given a null CharSequence to process");
}
if (string.length()==0) {
return null;
}
if (string.length() > getMaxSingleCompoundStringLength()) {
throw new IllegalArgumentException("CharSequence supplied is too long.");
}
return charSeqToCompound.get(string);
}
@Override
public int getMaxSingleCompoundStringLength() {
if(maxCompoundCharSequenceLength == -1) {
for(C compound: charSeqToCompound.values()) {
int size = getStringForCompound(compound).length();
if(size > maxCompoundCharSequenceLength) {
maxCompoundCharSequenceLength = size;
}
}
}
return maxCompoundCharSequenceLength;
}
@Override
public boolean isCompoundStringLengthEqual() {
if(compoundStringLengthEqual == null) {
int lastSize = -1;
compoundStringLengthEqual = Boolean.TRUE;
for(CharSequence c: charSeqToCompound.keySet()) {
if(lastSize != c.length()) {
compoundStringLengthEqual = Boolean.FALSE;
break;
}
}
}
return compoundStringLengthEqual;
}
@Override
public boolean hasCompound(C compound) {
C retrievedCompound = getCompoundForString(compound.toString());
return retrievedCompound != null;
}
@Override
public boolean compoundsEquivalent(C compoundOne, C compoundTwo) {
assertCompound(compoundOne);
assertCompound(compoundTwo);
return compoundOne.equals(compoundTwo) || equivalentsMap.get(compoundOne).contains(compoundTwo);
}
@Override
public Set<C> getEquivalentCompounds(C compound) {
return equivalentsMap.get(compound);
}
public boolean compoundsEqual(C compoundOne, C compoundTwo) {
assertCompound(compoundOne);
assertCompound(compoundTwo);
return compoundOne.equalsIgnoreCase(compoundTwo);
}
@Override
public boolean isValidSequence(Sequence<C> sequence) {
for (C compound: sequence) {
if (!hasCompound(compound)) {
return false;
}
}
return true;
}
@Override
public List<C> getAllCompounds() {
return new ArrayList<C>(charSeqToCompound.values());
}
private void assertCompound(C compound) {
if (!hasCompound(compound)) {
// TODO this used to throw an error, now only warning, is this the best solution?
// dmyersturnbull: I think throwing a CompoundNotFoundException is far better
logger.warn("The CompoundSet {} knows nothing about the compound {}", getClass().getSimpleName(), compound);
//throw new CompoundNotFoundError("The CompoundSet "+
// getClass().getSimpleName()+" knows nothing about the compound "+
// compound);
}
}
@Override
public boolean isComplementable() {
return false;
}
@Override
public int hashCode() {
int s = Hashcoder.SEED;
s = Hashcoder.hash(s, charSeqToCompound);
s = Hashcoder.hash(s, equivalentsMap);
return s;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (! (o instanceof AbstractCompoundSet)) return false;
if(Equals.classEqual(this, o)) {
AbstractCompoundSet<C> that = (AbstractCompoundSet<C>)o;
return Equals.equal(charSeqToCompound, that.charSeqToCompound) &&
Equals.equal(equivalentsMap, that.equivalentsMap);
}
return false;
}
}