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.XmlSeeAlso;
\r
32 import javax.xml.bind.annotation.XmlTransient;
\r
34 @XmlSeeAlso({RNAStructScoreManager.class})
\r
35 @XmlAccessorType(XmlAccessType.FIELD)
\r
36 public class ScoreManager {
\r
39 public static final String SINGLE_ENTRY_KEY = "Alignment";
\r
41 protected List<ScoreHolder> seqScores;
\r
43 // Do I have to change this to protected?
\r
44 protected ScoreManager() {
\r
45 // Default JAXB constructor
\r
48 private ScoreManager(String id, Set<Score> data) {
\r
49 seqScores = new ArrayList<ScoreManager.ScoreHolder>();
\r
50 seqScores.add(new ScoreHolder(id, data));
\r
53 private ScoreManager(Map<String, Set<Score>> data) {
\r
54 System.out.println("A ScoreManager was created!!");
\r
57 List<ScoreHolder> seqScores = new ArrayList<ScoreHolder>();
\r
58 for (Map.Entry<String, Set<Score>> singleSeqScores : data.entrySet()) {
\r
59 seqScores.add(new ScoreHolder(singleSeqScores.getKey(),
\r
60 singleSeqScores.getValue()));
\r
62 this.seqScores = seqScores;
\r
65 public static ScoreManager newInstance(Map<String, Set<Score>> data) {
\r
66 System.out.println("A ScoreManager was created!!");
\r
69 return new ScoreManager(data);
\r
72 public static ScoreManager newInstanceSingleScore(
\r
73 Map<String, Score> seqScoresMap) {
\r
75 System.out.println("A ScoreManager was created!!");
\r
78 Map<String, Set<Score>> multipleScoresMap = new TreeMap<String, Set<Score>>();
\r
79 for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {
\r
80 Set<Score> scores = new TreeSet<Score>();
\r
81 scores.add(seqScore.getValue());
\r
82 multipleScoresMap.put(seqScore.getKey(), scores);
\r
84 return new ScoreManager(multipleScoresMap);
\r
87 public static ScoreManager newInstanceSingleSequence(Set<Score> data) {
\r
88 System.out.println("A ScoreManager was created!!");
\r
91 return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY,
\r
95 public Map<String, TreeSet<Score>> asMap() {
\r
96 Map<String, TreeSet<Score>> seqScoresMap = new TreeMap<String, TreeSet<Score>>();
\r
97 for (ScoreHolder sch : this.seqScores) {
\r
98 TreeSet<Score> oldValue = seqScoresMap.put(sch.id, new TreeSet(
\r
100 if (oldValue != null) {
\r
101 throw new IllegalStateException(
\r
102 "Cannot represent this ScoreManager instance "
\r
103 + "as a Map as it contains duplicated keys: "
\r
107 return seqScoresMap;
\r
110 public Set<Score> asSet() {
\r
111 if (seqScores.size() == 0 || seqScores.size() > 1) {
\r
112 throw new IllegalStateException(
\r
113 "This ScoreManager has no or multiple sequence entries and thus "
\r
114 + "cannot be represented as a Set. Number of entries are: "
\r
115 + seqScores.size());
\r
117 ScoreHolder sch = seqScores.get(0);
\r
121 public int getNumberOfSeq() {
\r
122 return seqScores.size();
\r
125 public ScoreHolder getAnnotationForSequence(String seqId) {
\r
126 for (ScoreHolder sch : seqScores) {
\r
127 if (sch.id.equals(seqId)) {
\r
134 public void writeOut(Writer outStream) throws IOException {
\r
135 assert outStream != null : "Output steam is not defined!";
\r
136 if (seqScores == null) {
\r
139 for (ScoreHolder oneSeqScores : seqScores) {
\r
140 if (oneSeqScores == null)
\r
142 oneSeqScores.writeOut(outStream);
\r
147 public int hashCode() {
\r
148 final int prime = 31;
\r
150 result = prime * result
\r
151 + ((seqScores == null) ? 0 : seqScores.hashCode());
\r
156 public boolean equals(Object obj) {
\r
161 if (getClass() != obj.getClass())
\r
163 ScoreManager other = (ScoreManager) obj;
\r
164 if (seqScores == null) {
\r
165 if (other.seqScores != null)
\r
167 } else if (!seqScores.equals(other.seqScores))
\r
172 @XmlAccessorType(XmlAccessType.FIELD)
\r
173 public static class ScoreHolder {
\r
176 public TreeSet<Score> scores;
\r
178 private ScoreHolder() {
\r
179 // JAXB Default constructor should not be used otherwise
\r
182 ScoreHolder(String id, Set<Score> scores) {
\r
184 this.scores = new TreeSet<Score>(scores); }
\r
186 public void writeOut(Writer writer) throws IOException {
\r
187 writer.write(">" + id + "\n");
\r
188 Score.write(scores, writer);
\r
191 public Score getScoreByMethod(Enum<?> method) {
\r
192 for (Score sc : scores) {
\r
193 if (method.toString().equals(sc.getMethod())) {
\r
199 public Score getScoreByMethod(String method) {
\r
200 for (Score sc : scores) {
\r
201 if (method.toString().equals(sc.getMethod())) {
\r
207 public int getNumberOfScores() {
\r
208 return scores.size();
\r
212 public int hashCode() {
\r
213 final int prime = 17;
\r
215 result = prime * result + ((id == null) ? 0 : id.hashCode());
\r
216 result = prime * result
\r
217 + ((scores == null) ? 0 : scores.hashCode());
\r
222 public boolean equals(Object obj) {
\r
227 if (getClass() != obj.getClass())
\r
229 ScoreHolder other = (ScoreHolder) obj;
\r
231 if (other.id != null)
\r
233 } else if (!id.equals(other.id))
\r
235 if (scores == null) {
\r
236 if (other.scores != null)
\r
238 } else if (!scores.equals(other.scores))
\r