1 package compbio.data.sequence;
\r
3 import java.io.BufferedWriter;
\r
4 import java.io.IOException;
\r
5 import java.io.OutputStream;
\r
6 import java.io.OutputStreamWriter;
\r
7 import java.util.ArrayList;
\r
8 import java.util.HashMap;
\r
9 import java.util.HashSet;
\r
10 import java.util.List;
\r
11 import java.util.Map;
\r
12 import java.util.Set;
\r
14 import javax.xml.bind.annotation.XmlAccessType;
\r
15 import javax.xml.bind.annotation.XmlAccessorType;
\r
17 @XmlAccessorType(XmlAccessType.FIELD)
\r
18 public class ScoreManager {
\r
20 public static final String SINGLE_ENTRY_KEY = "Alignment";
\r
22 private List<ScoreHolder> seqScores;
\r
24 private ScoreManager() {
\r
25 // Default JAXB constructor
\r
28 private ScoreManager(String id, Set<Score> data) {
\r
29 seqScores = new ArrayList<ScoreManager.ScoreHolder>();
\r
30 seqScores.add(new ScoreHolder(id, data));
\r
33 private ScoreManager(Map<String, Set<Score>> data) {
\r
34 List<ScoreHolder> seqScores = new ArrayList<ScoreHolder>();
\r
35 for (Map.Entry<String, Set<Score>> singleSeqScores : data.entrySet()) {
\r
36 seqScores.add(new ScoreHolder(singleSeqScores.getKey(),
\r
37 singleSeqScores.getValue()));
\r
39 this.seqScores = seqScores;
\r
42 public static ScoreManager newInstance(Map<String, Set<Score>> data) {
\r
43 return new ScoreManager(data);
\r
46 public static ScoreManager newInstanceSingleScore(
\r
47 Map<String, Score> seqScoresMap) {
\r
48 Map<String, Set<Score>> multipleScoresMap = new HashMap<String, Set<Score>>();
\r
49 for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {
\r
50 Set<Score> scores = new HashSet<Score>();
\r
51 scores.add(seqScore.getValue());
\r
52 multipleScoresMap.put(seqScore.getKey(), scores);
\r
54 return new ScoreManager(multipleScoresMap);
\r
57 public static ScoreManager newInstanceSingleSequence(Set<Score> data) {
\r
58 return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, data);
\r
61 public Map<String, Set<Score>> asMap() {
\r
62 Map<String, Set<Score>> seqScoresMap = new HashMap<String, Set<Score>>();
\r
63 for (ScoreHolder sch : this.seqScores) {
\r
64 Set<Score> oldValue = seqScoresMap.put(sch.id, sch.scores);
\r
65 if (oldValue != null) {
\r
66 throw new IllegalStateException(
\r
67 "Cannot represent this ScoreManager instance "
\r
68 + "as a Map as it contains duplicated keys: "
\r
72 return seqScoresMap;
\r
75 public Set<Score> asSet() {
\r
76 if (seqScores.size() == 0 || seqScores.size() > 1) {
\r
77 throw new IllegalStateException(
\r
78 "This ScoreManager has no or multiple sequence entries and thus "
\r
79 + "cannot be represented as a Set. Number of entries are: "
\r
80 + seqScores.size());
\r
82 ScoreHolder sch = seqScores.get(0);
\r
86 public int getNumberOfSeq() {
\r
87 return seqScores.size();
\r
90 public ScoreHolder getAnnotationForSequence(String seqId) {
\r
91 for (ScoreHolder sch : seqScores) {
\r
92 if (sch.id.equals(seqId)) {
\r
99 public void writeOut(OutputStream outStream) throws IOException {
\r
100 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
\r
102 for (ScoreHolder oneSeqScores : seqScores) {
\r
103 oneSeqScores.writeOut(outStream);
\r
110 public int hashCode() {
\r
111 final int prime = 31;
\r
113 result = prime * result
\r
114 + ((seqScores == null) ? 0 : seqScores.hashCode());
\r
119 public boolean equals(Object obj) {
\r
124 if (getClass() != obj.getClass())
\r
126 ScoreManager other = (ScoreManager) obj;
\r
127 if (seqScores == null) {
\r
128 if (other.seqScores != null)
\r
130 } else if (!seqScores.equals(other.seqScores))
\r
137 @XmlAccessorType(XmlAccessType.FIELD)
\r
138 public static class ScoreHolder {
\r
141 public Set<Score> scores;
\r
143 private ScoreHolder() {
\r
144 // JAXB Default constructor should not be used otherwise
\r
147 ScoreHolder(String id, Set<Score> scores) {
\r
149 this.scores = scores;
\r
152 public void writeOut(OutputStream outStream) throws IOException {
\r
153 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
\r
155 writer.write(">" + id + "\n");
\r
156 Score.write(scores, outStream);
\r
159 public Score getScoreByMethod(Enum<?> method) {
\r
160 for (Score sc : scores) {
\r
161 if (method == sc.getMethod()) {
\r
168 public int getNumberOfScores() {
\r
169 return scores.size();
\r
173 public int hashCode() {
\r
174 final int prime = 17;
\r
176 result = prime * result + ((id == null) ? 0 : id.hashCode());
\r
177 result = prime * result
\r
178 + ((scores == null) ? 0 : scores.hashCode());
\r
183 public boolean equals(Object obj) {
\r
188 if (getClass() != obj.getClass())
\r
190 ScoreHolder other = (ScoreHolder) obj;
\r
192 if (other.id != null)
\r
194 } else if (!id.equals(other.id))
\r
196 if (scores == null) {
\r
197 if (other.scores != null)
\r
199 } else if (!scores.equals(other.scores))
\r