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
16 import javax.xml.bind.annotation.XmlTransient;
\r
18 @XmlAccessorType(XmlAccessType.FIELD)
\r
19 public class ScoreManager {
\r
22 public static final String SINGLE_ENTRY_KEY = "Alignment";
\r
24 private List<ScoreHolder> seqScores;
\r
26 private ScoreManager() {
\r
27 // Default JAXB constructor
\r
30 private ScoreManager(String id, Set<Score> data) {
\r
31 seqScores = new ArrayList<ScoreManager.ScoreHolder>();
\r
32 seqScores.add(new ScoreHolder(id, data));
\r
35 private ScoreManager(Map<String, Set<Score>> data) {
\r
36 List<ScoreHolder> seqScores = new ArrayList<ScoreHolder>();
\r
37 for (Map.Entry<String, Set<Score>> singleSeqScores : data.entrySet()) {
\r
38 seqScores.add(new ScoreHolder(singleSeqScores.getKey(),
\r
39 singleSeqScores.getValue()));
\r
41 this.seqScores = seqScores;
\r
44 public static ScoreManager newInstance(Map<String, Set<Score>> data) {
\r
45 return new ScoreManager(data);
\r
48 public static ScoreManager newInstanceSingleScore(
\r
49 Map<String, Score> seqScoresMap) {
\r
50 Map<String, Set<Score>> multipleScoresMap = new HashMap<String, Set<Score>>();
\r
51 for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {
\r
52 Set<Score> scores = new HashSet<Score>();
\r
53 scores.add(seqScore.getValue());
\r
54 multipleScoresMap.put(seqScore.getKey(), scores);
\r
56 return new ScoreManager(multipleScoresMap);
\r
59 public static ScoreManager newInstanceSingleSequence(Set<Score> data) {
\r
60 return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, data);
\r
63 public Map<String, Set<Score>> asMap() {
\r
64 Map<String, Set<Score>> seqScoresMap = new HashMap<String, Set<Score>>();
\r
65 for (ScoreHolder sch : this.seqScores) {
\r
66 Set<Score> oldValue = seqScoresMap.put(sch.id, sch.scores);
\r
67 if (oldValue != null) {
\r
68 throw new IllegalStateException(
\r
69 "Cannot represent this ScoreManager instance "
\r
70 + "as a Map as it contains duplicated keys: "
\r
74 return seqScoresMap;
\r
77 public Set<Score> asSet() {
\r
78 if (seqScores.size() == 0 || seqScores.size() > 1) {
\r
79 throw new IllegalStateException(
\r
80 "This ScoreManager has no or multiple sequence entries and thus "
\r
81 + "cannot be represented as a Set. Number of entries are: "
\r
82 + seqScores.size());
\r
84 ScoreHolder sch = seqScores.get(0);
\r
88 public int getNumberOfSeq() {
\r
89 return seqScores.size();
\r
92 public ScoreHolder getAnnotationForSequence(String seqId) {
\r
93 for (ScoreHolder sch : seqScores) {
\r
94 if (sch.id.equals(seqId)) {
\r
101 public void writeOut(OutputStream outStream) throws IOException {
\r
102 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
\r
104 for (ScoreHolder oneSeqScores : seqScores) {
\r
105 oneSeqScores.writeOut(outStream);
\r
111 public int hashCode() {
\r
112 final int prime = 31;
\r
114 result = prime * result
\r
115 + ((seqScores == null) ? 0 : seqScores.hashCode());
\r
120 public boolean equals(Object obj) {
\r
125 if (getClass() != obj.getClass())
\r
127 ScoreManager other = (ScoreManager) obj;
\r
128 if (seqScores == null) {
\r
129 if (other.seqScores != null)
\r
131 } else if (!seqScores.equals(other.seqScores))
\r
136 @XmlAccessorType(XmlAccessType.FIELD)
\r
137 public static class ScoreHolder {
\r
140 public HashSet<Score> scores;
\r
142 private ScoreHolder() {
\r
143 // JAXB Default constructor should not be used otherwise
\r
146 ScoreHolder(String id, Set<Score> scores) {
\r
148 this.scores = new HashSet<Score>(scores);
\r
151 public void writeOut(OutputStream outStream) throws IOException {
\r
152 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
\r
154 writer.write(">" + id + "\n");
\r
155 Score.write(scores, outStream);
\r
158 public Score getScoreByMethod(Enum<?> method) {
\r
159 for (Score sc : scores) {
\r
160 if (method.toString().equals(sc.getMethod())) {
\r
166 public int getNumberOfScores() {
\r
167 return scores.size();
\r
171 public int hashCode() {
\r
172 final int prime = 17;
\r
174 result = prime * result + ((id == null) ? 0 : id.hashCode());
\r
175 result = prime * result
\r
176 + ((scores == null) ? 0 : scores.hashCode());
\r
181 public boolean equals(Object obj) {
\r
186 if (getClass() != obj.getClass())
\r
188 ScoreHolder other = (ScoreHolder) obj;
\r
190 if (other.id != null)
\r
192 } else if (!id.equals(other.id))
\r
194 if (scores == null) {
\r
195 if (other.scores != null)
\r
197 } else if (!scores.equals(other.scores))
\r