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
60 * Next constructor added by Daniel
\r
61 * a constructor for using ScoreManager to represent RNAalifold output
\r
65 public ScoreManager(RNAStruct rnaStruct) {
\r
66 assert(rnaStruct.getStructs().size() == rnaStruct.getData().size());
\r
67 // This had better be used to create a RNAalifold output structure
\r
68 assert(java.util.regex.Pattern.matches(
\r
69 "^[\\.)(]*$", rnaStruct.getStructs().get(1)));
\r
70 List<ScoreHolder> seqScores = new ArrayList<ScoreHolder>();
\r
72 for (int i = 0; i < rnaStruct.getStructs().size(); i++) {
\r
73 seqScores.add(new ScoreHolder(rnaStruct.getStructs().get(i),
\r
74 rnaStruct.getData().get(i)));
\r
76 this.seqScores = seqScores;
\r
80 public static ScoreManager newInstance(Map<String, Set<Score>> data) {
\r
81 return new ScoreManager(data);
\r
84 public static ScoreManager newInstanceSingleScore(
\r
85 Map<String, Score> seqScoresMap) {
\r
86 Map<String, Set<Score>> multipleScoresMap = new TreeMap<String, Set<Score>>();
\r
87 for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {
\r
88 Set<Score> scores = new TreeSet<Score>();
\r
89 scores.add(seqScore.getValue());
\r
90 multipleScoresMap.put(seqScore.getKey(), scores);
\r
92 return new ScoreManager(multipleScoresMap);
\r
95 public static ScoreManager newInstanceSingleSequence(Set<Score> data) {
\r
96 return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY,
\r
100 // Also Daniel's. Check this with Jim to make sure that I am not compromising
\r
101 // the security of this class
\r
102 public RNAStruct asRNAStruct() {
\r
103 List<String> structs = new ArrayList<String>();
\r
104 List<TreeSet<Score>> data = new ArrayList<TreeSet<Score>>();
\r
105 for (ScoreHolder sch : this.seqScores) {
\r
106 structs.add(sch.id);
\r
107 data.add(sch.scores);
\r
109 return new RNAStruct(structs, data);
\r
113 public Map<String, TreeSet<Score>> asMap() {
\r
114 Map<String, TreeSet<Score>> seqScoresMap = new TreeMap<String, TreeSet<Score>>();
\r
115 for (ScoreHolder sch : this.seqScores) {
\r
116 TreeSet<Score> oldValue = seqScoresMap.put(sch.id, new TreeSet(
\r
118 if (oldValue != null) {
\r
119 throw new IllegalStateException(
\r
120 "Cannot represent this ScoreManager instance "
\r
121 + "as a Map as it contains duplicated keys: "
\r
125 return seqScoresMap;
\r
128 public Set<Score> asSet() {
\r
129 if (seqScores.size() == 0 || seqScores.size() > 1) {
\r
130 throw new IllegalStateException(
\r
131 "This ScoreManager has no or multiple sequence entries and thus "
\r
132 + "cannot be represented as a Set. Number of entries are: "
\r
133 + seqScores.size());
\r
135 ScoreHolder sch = seqScores.get(0);
\r
139 public int getNumberOfSeq() {
\r
140 return seqScores.size();
\r
143 public ScoreHolder getAnnotationForSequence(String seqId) {
\r
144 for (ScoreHolder sch : seqScores) {
\r
145 if (sch.id.equals(seqId)) {
\r
152 public void writeOut(Writer outStream) throws IOException {
\r
153 assert outStream != null : "Output steam is not defined!";
\r
154 if (seqScores == null) {
\r
157 for (ScoreHolder oneSeqScores : seqScores) {
\r
158 if (oneSeqScores == null)
\r
160 oneSeqScores.writeOut(outStream);
\r
165 public int hashCode() {
\r
166 final int prime = 31;
\r
168 result = prime * result
\r
169 + ((seqScores == null) ? 0 : seqScores.hashCode());
\r
174 public boolean equals(Object obj) {
\r
179 if (getClass() != obj.getClass())
\r
181 ScoreManager other = (ScoreManager) obj;
\r
182 if (seqScores == null) {
\r
183 if (other.seqScores != null)
\r
185 } else if (!seqScores.equals(other.seqScores))
\r
190 @XmlAccessorType(XmlAccessType.FIELD)
\r
191 public static class ScoreHolder {
\r
194 public TreeSet<Score> scores;
\r
196 private ScoreHolder() {
\r
197 // JAXB Default constructor should not be used otherwise
\r
200 ScoreHolder(String id, Set<Score> scores) {
\r
202 this.scores = new TreeSet<Score>(scores);
\r
205 public void writeOut(Writer writer) throws IOException {
\r
206 writer.write(">" + id + "\n");
\r
207 Score.write(scores, writer);
\r
210 public Score getScoreByMethod(Enum<?> method) {
\r
211 for (Score sc : scores) {
\r
212 if (method.toString().equals(sc.getMethod())) {
\r
218 public Score getScoreByMethod(String method) {
\r
219 for (Score sc : scores) {
\r
220 if (method.toString().equals(sc.getMethod())) {
\r
226 public int getNumberOfScores() {
\r
227 return scores.size();
\r
231 public int hashCode() {
\r
232 final int prime = 17;
\r
234 result = prime * result + ((id == null) ? 0 : id.hashCode());
\r
235 result = prime * result
\r
236 + ((scores == null) ? 0 : scores.hashCode());
\r
241 public boolean equals(Object obj) {
\r
246 if (getClass() != obj.getClass())
\r
248 ScoreHolder other = (ScoreHolder) obj;
\r
250 if (other.id != null)
\r
252 } else if (!id.equals(other.id))
\r
254 if (scores == null) {
\r
255 if (other.scores != null)
\r
257 } else if (!scores.equals(other.scores))
\r