X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FScoreManager.java;h=d16a059ca7486311016c1ef2e0baf7a235554a20;hb=8ce6db56df6c0a1c2baca2fc2d33bcb10067c6cd;hp=f3843086f185a521358a708fde13f335902abdd9;hpb=3644426ec0e9e9ceb798bd426085d8d9dd8dbb56;p=jabaws.git diff --git a/datamodel/compbio/data/sequence/ScoreManager.java b/datamodel/compbio/data/sequence/ScoreManager.java index f384308..d16a059 100644 --- a/datamodel/compbio/data/sequence/ScoreManager.java +++ b/datamodel/compbio/data/sequence/ScoreManager.java @@ -1,22 +1,39 @@ +/* Copyright (c) 2011 Peter Troshin + * + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 + * + * This library is free software; you can redistribute it and/or modify it under the terms of the + * Apache License version 2 as published by the Apache Software Foundation + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache + * License for more details. + * + * A copy of the license is in apache_license.txt. It is also available here: + * @see: http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Any republication or derived work distributed in source code form + * must include this copyright and license notice. + */ package compbio.data.sequence; -import java.io.BufferedWriter; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlTransient; @XmlAccessorType(XmlAccessType.FIELD) public class ScoreManager { + @XmlTransient public static final String SINGLE_ENTRY_KEY = "Alignment"; private List seqScores; @@ -45,9 +62,9 @@ public class ScoreManager { public static ScoreManager newInstanceSingleScore( Map seqScoresMap) { - Map> multipleScoresMap = new HashMap>(); + Map> multipleScoresMap = new TreeMap>(); for (Map.Entry seqScore : seqScoresMap.entrySet()) { - Set scores = new HashSet(); + Set scores = new TreeSet(); scores.add(seqScore.getValue()); multipleScoresMap.put(seqScore.getKey(), scores); } @@ -55,13 +72,15 @@ public class ScoreManager { } public static ScoreManager newInstanceSingleSequence(Set data) { - return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, data); + return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, + new TreeSet(data)); } - public Map> asMap() { - Map> seqScoresMap = new HashMap>(); + public Map> asMap() { + Map> seqScoresMap = new TreeMap>(); for (ScoreHolder sch : this.seqScores) { - Set oldValue = seqScoresMap.put(sch.id, sch.scores); + TreeSet oldValue = seqScoresMap.put(sch.id, new TreeSet( + sch.scores)); if (oldValue != null) { throw new IllegalStateException( "Cannot represent this ScoreManager instance " @@ -96,15 +115,17 @@ public class ScoreManager { return null; } - public void writeOut(OutputStream outStream) throws IOException { - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - outStream)); + public void writeOut(Writer outStream) throws IOException { + assert outStream != null : "Output steam is not defined!"; + if (seqScores == null) { + return; + } for (ScoreHolder oneSeqScores : seqScores) { + if (oneSeqScores == null) + continue; oneSeqScores.writeOut(outStream); } - writer.flush(); } - @Override public int hashCode() { @@ -132,13 +153,11 @@ public class ScoreManager { return true; } - - @XmlAccessorType(XmlAccessType.FIELD) public static class ScoreHolder { public String id; - public Set scores; + public TreeSet scores; private ScoreHolder() { // JAXB Default constructor should not be used otherwise @@ -146,25 +165,30 @@ public class ScoreManager { ScoreHolder(String id, Set scores) { this.id = id; - this.scores = scores; + this.scores = new TreeSet(scores); } - public void writeOut(OutputStream outStream) throws IOException { - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - outStream)); + public void writeOut(Writer writer) throws IOException { writer.write(">" + id + "\n"); - Score.write(scores, outStream); + Score.write(scores, writer); } public Score getScoreByMethod(Enum method) { for (Score sc : scores) { - if (method == sc.getMethod()) { + if (method.toString().equals(sc.getMethod())) { + return sc; + } + } + return null; + } + public Score getScoreByMethod(String method) { + for (Score sc : scores) { + if (method.toString().equals(sc.getMethod())) { return sc; } } return null; } - public int getNumberOfScores() { return scores.size(); } @@ -200,7 +224,7 @@ public class ScoreManager { return false; return true; } - - + } + }