1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.data.sequence;
\r
20 import java.io.IOException;
\r
21 import java.io.Writer;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
25 import java.util.Set;
\r
26 import java.util.TreeMap;
\r
27 import java.util.TreeSet;
\r
29 import javax.xml.bind.annotation.XmlAccessType;
\r
30 import javax.xml.bind.annotation.XmlAccessorType;
\r
31 import javax.xml.bind.annotation.XmlTransient;
\r
33 @XmlAccessorType(XmlAccessType.FIELD)
\r
34 public class ScoreManager {
\r
37 public static final String SINGLE_ENTRY_KEY = "Alignment";
\r
39 private List<ScoreHolder> seqScores;
\r
41 private ScoreManager() {
\r
42 // Default JAXB constructor
\r
45 private ScoreManager(String id, Set<Score> data) {
\r
46 seqScores = new ArrayList<ScoreManager.ScoreHolder>();
\r
47 seqScores.add(new ScoreHolder(id, data));
\r
50 private ScoreManager(Map<String, Set<Score>> data) {
\r
51 List<ScoreHolder> seqScores = new ArrayList<ScoreHolder>();
\r
52 for (Map.Entry<String, Set<Score>> singleSeqScores : data.entrySet()) {
\r
53 seqScores.add(new ScoreHolder(singleSeqScores.getKey(),
\r
54 singleSeqScores.getValue()));
\r
56 this.seqScores = seqScores;
\r
59 public static ScoreManager newInstance(Map<String, Set<Score>> data) {
\r
60 return new ScoreManager(data);
\r
63 public static ScoreManager newInstanceSingleScore(
\r
64 Map<String, Score> seqScoresMap) {
\r
65 Map<String, Set<Score>> multipleScoresMap = new TreeMap<String, Set<Score>>();
\r
66 for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {
\r
67 Set<Score> scores = new TreeSet<Score>();
\r
68 scores.add(seqScore.getValue());
\r
69 multipleScoresMap.put(seqScore.getKey(), scores);
\r
71 return new ScoreManager(multipleScoresMap);
\r
74 public static ScoreManager newInstanceSingleSequence(Set<Score> data) {
\r
75 return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY,
\r
79 public Map<String, TreeSet<Score>> asMap() {
\r
80 Map<String, TreeSet<Score>> seqScoresMap = new TreeMap<String, TreeSet<Score>>();
\r
81 for (ScoreHolder sch : this.seqScores) {
\r
82 TreeSet<Score> oldValue = seqScoresMap.put(sch.id, new TreeSet(
\r
84 if (oldValue != null) {
\r
85 throw new IllegalStateException(
\r
86 "Cannot represent this ScoreManager instance "
\r
87 + "as a Map as it contains duplicated keys: "
\r
91 return seqScoresMap;
\r
94 public Set<Score> asSet() {
\r
95 if (seqScores.size() == 0 || seqScores.size() > 1) {
\r
96 throw new IllegalStateException(
\r
97 "This ScoreManager has no or multiple sequence entries and thus "
\r
98 + "cannot be represented as a Set. Number of entries are: "
\r
99 + seqScores.size());
\r
101 ScoreHolder sch = seqScores.get(0);
\r
105 public int getNumberOfSeq() {
\r
106 return seqScores.size();
\r
109 public ScoreHolder getAnnotationForSequence(String seqId) {
\r
110 for (ScoreHolder sch : seqScores) {
\r
111 if (sch.id.equals(seqId)) {
\r
118 public void writeOut(Writer outStream) throws IOException {
\r
119 assert outStream != null : "Output steam is not defined!";
\r
120 if (seqScores == null) {
\r
123 for (ScoreHolder oneSeqScores : seqScores) {
\r
124 if (oneSeqScores == null)
\r
126 oneSeqScores.writeOut(outStream);
\r
131 public int hashCode() {
\r
132 final int prime = 31;
\r
134 result = prime * result
\r
135 + ((seqScores == null) ? 0 : seqScores.hashCode());
\r
140 public boolean equals(Object obj) {
\r
145 if (getClass() != obj.getClass())
\r
147 ScoreManager other = (ScoreManager) obj;
\r
148 if (seqScores == null) {
\r
149 if (other.seqScores != null)
\r
151 } else if (!seqScores.equals(other.seqScores))
\r
156 @XmlAccessorType(XmlAccessType.FIELD)
\r
157 public static class ScoreHolder {
\r
160 public TreeSet<Score> scores;
\r
162 private ScoreHolder() {
\r
163 // JAXB Default constructor should not be used otherwise
\r
166 ScoreHolder(String id, Set<Score> scores) {
\r
168 this.scores = new TreeSet<Score>(scores);
\r
171 public void writeOut(Writer writer) throws IOException {
\r
172 writer.write(">" + id + "\n");
\r
173 Score.write(scores, writer);
\r
176 public Score getScoreByMethod(Enum<?> method) {
\r
177 for (Score sc : scores) {
\r
178 if (method.toString().equals(sc.getMethod())) {
\r
184 public Score getScoreByMethod(String method) {
\r
185 for (Score sc : scores) {
\r
186 if (method.toString().equals(sc.getMethod())) {
\r
192 public int getNumberOfScores() {
\r
193 return scores.size();
\r
197 public int hashCode() {
\r
198 final int prime = 17;
\r
200 result = prime * result + ((id == null) ? 0 : id.hashCode());
\r
201 result = prime * result
\r
202 + ((scores == null) ? 0 : scores.hashCode());
\r
207 public boolean equals(Object obj) {
\r
212 if (getClass() != obj.getClass())
\r
214 ScoreHolder other = (ScoreHolder) obj;
\r
216 if (other.id != null)
\r
218 } else if (!id.equals(other.id))
\r
220 if (scores == null) {
\r
221 if (other.scores != null)
\r
223 } else if (!scores.equals(other.scores))
\r