From 9ba9d7fd62b1f98b09ed4097c4b38ae94e25a72a Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Thu, 10 Feb 2011 11:36:12 +0000 Subject: [PATCH] Replace Map> with ScoreManager and ScoreHolder classes to compensate for JAXB inability to serialize Maps. Propagate the changes git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@3733 e3abac25-378b-4346-85de-24260fe3988d --- datamodel/compbio/data/sequence/ScoreManager.java | 92 +++++++++++++++++--- datamodel/compbio/data/sequence/SequenceUtil.java | 37 ++++---- runner/compbio/runner/conservation/AACon.java | 10 +-- runner/compbio/runner/disorder/Disembl.java | 11 +-- runner/compbio/runner/disorder/GlobPlot.java | 10 +-- runner/compbio/runner/disorder/Jronn.java | 10 +-- .../compbio/data/sequence/SequenceUtilTester.java | 16 ++-- testsrc/compbio/ws/client/TestAAConWS.java | 37 ++++---- 8 files changed, 136 insertions(+), 87 deletions(-) diff --git a/datamodel/compbio/data/sequence/ScoreManager.java b/datamodel/compbio/data/sequence/ScoreManager.java index b0f87a6..b0305e6 100644 --- a/datamodel/compbio/data/sequence/ScoreManager.java +++ b/datamodel/compbio/data/sequence/ScoreManager.java @@ -4,25 +4,87 @@ import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +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 javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; + +@XmlAccessorType(XmlAccessType.FIELD) public class ScoreManager { + public static final String SINGLE_ENTRY_KEY = "Alignment"; + private List seqScores; private ScoreManager() { // Default JAXB constructor } - public int getNumberOfSeq() { - return seqScores.size(); + private ScoreManager(String id, Set data) { + seqScores = new ArrayList(); + seqScores.add(new ScoreHolder(id, data)); } - public ScoreHolder getFirstSequenceScores() { - if (seqScores.isEmpty()) - return null; - return seqScores.get(0); + private ScoreManager(Map> data) { + List seqScores = new ArrayList(); + for (Map.Entry> singleSeqScores : data.entrySet()) { + seqScores.add(new ScoreHolder(singleSeqScores.getKey(), + singleSeqScores.getValue())); + } + this.seqScores = seqScores; + } + + public static ScoreManager newInstance(Map> data) { + return new ScoreManager(data); + } + + public static ScoreManager newInstanceSingleScore( + Map seqScoresMap) { + Map> multipleScoresMap = new HashMap>(); + for (Map.Entry seqScore : seqScoresMap.entrySet()) { + Set scores = new HashSet(); + scores.add(seqScore.getValue()); + multipleScoresMap.put(seqScore.getKey(), scores); + } + return new ScoreManager(multipleScoresMap); + } + + public static ScoreManager newInstanceSingleSequence(Set data) { + return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, data); + } + + public Map> asMap() { + Map> seqScoresMap = new HashMap>(); + for (ScoreHolder sch : this.seqScores) { + Set oldValue = seqScoresMap.put(sch.id, sch.scores); + if (oldValue != null) { + throw new IllegalStateException( + "Cannot represent this ScoreManager instance " + + "as a Map as it contains duplicated keys: " + + sch.id); + } + } + return seqScoresMap; + } + + public Set asSet() { + if (seqScores.size() == 0 || seqScores.size() > 1) { + throw new IllegalStateException( + "This ScoreManager has no or multiple sequence entries and thus " + + "cannot be represented as a Set. Number of entries are: " + + seqScores.size()); + } + ScoreHolder sch = seqScores.get(0); + return sch.scores; + } + + public int getNumberOfSeq() { + return seqScores.size(); } public ScoreHolder getAnnotationForSequence(String seqId) { @@ -43,10 +105,20 @@ public class ScoreManager { writer.flush(); } + @XmlAccessorType(XmlAccessType.FIELD) public static class ScoreHolder { public String id; - public HashSet scores; + public Set scores; + + private ScoreHolder() { + // JAXB Default constructor should not be used otherwise + } + + ScoreHolder(String id, Set scores) { + this.id = id; + this.scores = scores; + } public void writeOut(OutputStream outStream) throws IOException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( @@ -55,12 +127,6 @@ public class ScoreManager { Score.write(scores, outStream); } - public Score getFirstScore() { - if (scores.isEmpty()) - return null; - return scores.iterator().next(); - } - public Score getScoreByMethod(Enum method) { for (Score sc : scores) { if (method == sc.getMethod()) { diff --git a/datamodel/compbio/data/sequence/SequenceUtil.java b/datamodel/compbio/data/sequence/SequenceUtil.java index c4e1def..d0a6cd7 100644 --- a/datamodel/compbio/data/sequence/SequenceUtil.java +++ b/datamodel/compbio/data/sequence/SequenceUtil.java @@ -30,6 +30,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; +import java.util.Set; import java.util.TreeSet; import java.util.logging.Level; import java.util.regex.Matcher; @@ -429,7 +430,7 @@ public final class SequenceUtil { * @throws IOException * @throws UnknownFileFormatException */ - public static HashMap> readDisembl( + public static HashMap> readDisembl( final InputStream input) throws IOException, UnknownFileFormatException { Scanner scan = new Scanner(input); @@ -441,7 +442,7 @@ public final class SequenceUtil { + " No such line was found!"); } - HashMap> results = new HashMap>(); + HashMap> results = new HashMap>(); int seqCounter = 0; while (scan.hasNext()) { seqCounter++; @@ -474,13 +475,15 @@ public final class SequenceUtil { rem.add(scansingle.nextFloat()); hotloops.add(scansingle.nextFloat()); } - FastaSequence fs = new FastaSequence(sequenceName, - seqbuffer.toString()); + /* + * Also possible FastaSequence fs = new FastaSequence(sequenceName, + * seqbuffer.toString()); + */ HashSet scores = new HashSet(); scores.add(new Score(DisemblResult.COILS, coils, coilsR)); scores.add(new Score(DisemblResult.HOTLOOPS, hotloops, rem465R)); scores.add(new Score(DisemblResult.REM465, rem, loopsR)); - results.put(fs, scores); + results.put(sequenceName, scores); scansingle.close(); } @@ -525,16 +528,6 @@ public final class SequenceUtil { return ranges; } - public static HashMap> removeSequences( - HashMap> disemblResults) { - HashMap> seqNameScores = new HashMap>(); - for (Map.Entry> dres : disemblResults - .entrySet()) { - seqNameScores.put(dres.getKey().getId(), dres.getValue()); - } - return seqNameScores; - } - /** * > Foobar_dundeefriends @@ -559,7 +552,7 @@ public final class SequenceUtil { * @throws IOException * @throws UnknownFileFormatException */ - public static HashMap> readGlobPlot( + public static HashMap> readGlobPlot( final InputStream input) throws IOException, UnknownFileFormatException { Scanner scan = new Scanner(input); @@ -571,7 +564,7 @@ public final class SequenceUtil { + " No such line was found!"); } - HashMap> results = new HashMap>(); + HashMap> results = new HashMap>(); int seqCounter = 0; while (scan.hasNext()) { seqCounter++; @@ -602,15 +595,17 @@ public final class SequenceUtil { rawScore.add(scansingle.nextFloat()); smoothedScore.add(scansingle.nextFloat()); } - FastaSequence fs = new FastaSequence(sequenceName, - seqbuffer.toString()); + /* + * Also possible FastaSequence fs = new FastaSequence(sequenceName, + * seqbuffer.toString()); + */ HashSet scores = new HashSet(); scores.add(new Score(GlobProtResult.Disorder, disorderR)); scores.add(new Score(GlobProtResult.GlobDoms, domsR)); scores.add(new Score(GlobProtResult.Dydx, dydxScore)); scores.add(new Score(GlobProtResult.RawScore, rawScore)); scores.add(new Score(GlobProtResult.SmoothedScore, smoothedScore)); - results.put(fs, scores); + results.put(sequenceName, scores); scansingle.close(); } @@ -620,7 +615,7 @@ public final class SequenceUtil { } /** * Read AACon result with no alignment files. This method leaves incoming - * the InputStream results open! + * InputStream open! * * @param results * output file of AAConservation diff --git a/runner/compbio/runner/conservation/AACon.java b/runner/compbio/runner/conservation/AACon.java index f43db89..fd76e55 100644 --- a/runner/compbio/runner/conservation/AACon.java +++ b/runner/compbio/runner/conservation/AACon.java @@ -20,13 +20,12 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import java.util.HashSet; import java.util.List; import org.apache.log4j.Logger; import compbio.data.sequence.SMERFSConstraints; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.engine.client.CommandBuilder; import compbio.engine.client.Executable; @@ -72,13 +71,14 @@ public class AACon extends SkeletalExecutable { } // HashMap @Override - public HashSet getResults(String workDirectory) + public ScoreManager getResults(String workDirectory) throws ResultNotAvailableException { - HashSet annotations = null; + ScoreManager annotations = null; try { InputStream inStream = new FileInputStream(new File(workDirectory, getOutput())); - annotations = SequenceUtil.readAAConResults(inStream); + annotations = ScoreManager.newInstanceSingleSequence(SequenceUtil + .readAAConResults(inStream)); inStream.close(); } catch (FileNotFoundException e) { log.error(e.getMessage(), e.getCause()); diff --git a/runner/compbio/runner/disorder/Disembl.java b/runner/compbio/runner/disorder/Disembl.java index 616b552..6b014ba 100644 --- a/runner/compbio/runner/disorder/Disembl.java +++ b/runner/compbio/runner/disorder/Disembl.java @@ -19,12 +19,10 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; -import java.util.HashSet; import org.apache.log4j.Logger; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.engine.client.Executable; @@ -85,15 +83,15 @@ public class Disembl extends SkeletalExecutable @SuppressWarnings("unchecked") @Override - public HashMap> getResults(String workDirectory) + public ScoreManager getResults(String workDirectory) throws ResultNotAvailableException { InputStream inStream = null; - HashMap> results = null; + ScoreManager results = null; try { inStream = new FileInputStream(new File(workDirectory, getOutput())); - results = SequenceUtil.removeSequences(SequenceUtil + results = ScoreManager.newInstance(SequenceUtil .readDisembl(inStream)); inStream.close(); } catch (FileNotFoundException e) { @@ -112,7 +110,6 @@ public class Disembl extends SkeletalExecutable log.trace("DRESULTS: " + results); return results; } - @Override public Disembl setInput(String inFile) { super.setInput(inFile); diff --git a/runner/compbio/runner/disorder/GlobPlot.java b/runner/compbio/runner/disorder/GlobPlot.java index 99420f5..ce08077 100644 --- a/runner/compbio/runner/disorder/GlobPlot.java +++ b/runner/compbio/runner/disorder/GlobPlot.java @@ -19,12 +19,10 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; -import java.util.HashSet; import org.apache.log4j.Logger; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.engine.client.Executable; @@ -70,15 +68,15 @@ public class GlobPlot extends SkeletalExecutable } @SuppressWarnings("unchecked") - public HashMap> getResults(String workDirectory) + public ScoreManager getResults(String workDirectory) throws ResultNotAvailableException { InputStream inStream = null; - HashMap> results = null; + ScoreManager results = null; // How about getting ranges? try { inStream = new FileInputStream(new File(workDirectory, getOutput())); - results = SequenceUtil.removeSequences(SequenceUtil + results = ScoreManager.newInstance(SequenceUtil .readGlobPlot(inStream)); inStream.close(); } catch (FileNotFoundException e) { diff --git a/runner/compbio/runner/disorder/Jronn.java b/runner/compbio/runner/disorder/Jronn.java index a436456..617d3bb 100644 --- a/runner/compbio/runner/disorder/Jronn.java +++ b/runner/compbio/runner/disorder/Jronn.java @@ -25,11 +25,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.engine.client.CommandBuilder; @@ -73,13 +72,14 @@ public class Jronn extends SkeletalExecutable { @SuppressWarnings("unchecked") @Override - public Map getResults(String workDirectory) + public ScoreManager getResults(String workDirectory) throws ResultNotAvailableException { - Map sequences = null; + ScoreManager sequences = null; try { InputStream inStream = new FileInputStream(new File(workDirectory, getOutput())); - sequences = SequenceUtil.readJRonn(inStream); + sequences = ScoreManager.newInstanceSingleScore(SequenceUtil + .readJRonn(inStream)); inStream.close(); } catch (FileNotFoundException e) { log.error(e.getMessage(), e.getCause()); diff --git a/testsrc/compbio/data/sequence/SequenceUtilTester.java b/testsrc/compbio/data/sequence/SequenceUtilTester.java index a4bf4c9..682bcad 100644 --- a/testsrc/compbio/data/sequence/SequenceUtilTester.java +++ b/testsrc/compbio/data/sequence/SequenceUtilTester.java @@ -165,14 +165,13 @@ public class SequenceUtilTester { try { fio = new FileInputStream(AllTestSuit.TEST_DATA_PATH + "disembl.out"); - Map> aseqs = SequenceUtil - .readDisembl(fio); + Map> aseqs = SequenceUtil.readDisembl(fio); assertNotNull(aseqs); assertEquals(aseqs.size(), 3); // System.out.println(aseqs); - for (FastaSequence fs : aseqs.keySet()) { + for (String fs : aseqs.keySet()) { assertTrue(" Foobar_dundeefriends Foobar dundeefriends " - .contains(fs.getId())); + .contains(fs)); Set scores = aseqs.get(fs); assertEquals(scores.size(), 3); } @@ -218,15 +217,14 @@ public class SequenceUtilTester { try { fio = new FileInputStream(AllTestSuit.TEST_DATA_PATH + "globplot.out"); - HashMap> aseqs = SequenceUtil - .readGlobPlot(fio); + HashMap> aseqs = SequenceUtil.readGlobPlot(fio); assertNotNull(aseqs); assertEquals(aseqs.size(), 3); - FastaSequence fsdf = null; + String fsdf = null; Set scores = null; - for (FastaSequence fs : aseqs.keySet()) { - if ("Foobar_dundeefriends".contains(fs.getId())) { + for (String fs : aseqs.keySet()) { + if ("Foobar_dundeefriends".contains(fs)) { fsdf = fs; scores = aseqs.get(fs); } diff --git a/testsrc/compbio/ws/client/TestAAConWS.java b/testsrc/compbio/ws/client/TestAAConWS.java index 6476311..0954a2b 100644 --- a/testsrc/compbio/ws/client/TestAAConWS.java +++ b/testsrc/compbio/ws/client/TestAAConWS.java @@ -77,12 +77,11 @@ public class TestAAConWS { System.out.println("J: " + jobId); ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); + assertEquals(result.asSet().size(), 1); - assertEquals(result.getFirstSequenceScores().getFirstScore() - .getMethod(), ConservationMethod.SHENKIN); - List scores = result.getFirstSequenceScores() - .getFirstScore().getScores(); + assertEquals(result.asSet().iterator().next().getMethod(), + ConservationMethod.SHENKIN); + List scores = result.asSet().iterator().next().getScores(); assertNotNull(scores); assertEquals(scores.size(), 568); @@ -92,21 +91,19 @@ public class TestAAConWS { presets.getPresetByName("Quick conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), - 13); + assertEquals(result.asSet().size(), 13); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Slow conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 5); + assertEquals(result.asSet().size(), 5); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Complete conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), - 18); + assertEquals(result.asSet().size(), 18); } catch (UnsupportedRuntimeException e) { e.printStackTrace(); @@ -145,21 +142,19 @@ public class TestAAConWS { presets.getPresetByName("Quick conservation")); ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), - 13); + assertEquals(result.asSet().size(), 13); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Slow conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 5); + assertEquals(result.asSet().size(), 5); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Complete conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), - 18); + assertEquals(result.asSet().size(), 18); } catch (UnsupportedRuntimeException e) { e.printStackTrace(); @@ -202,9 +197,9 @@ public class TestAAConWS { String jobId = msaws.customAnalize(fsl, options.getArguments()); ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); - assertEquals(result.getFirstSequenceScores().getFirstScore() - .getScores().get(0), 0.698f); + assertEquals(result.asSet().size(), 1); + assertEquals(result.asSet().iterator().next().getScores().get(0), + 0.698f); options.getArgument("Calculation method").setDefaultValue("SMERFS"); options.removeArgument("Normalize"); @@ -212,9 +207,9 @@ public class TestAAConWS { jobId = msaws.customAnalize(fsl, options.getArguments()); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); - assertEquals(result.getFirstSequenceScores().getFirstScore() - .getScores().get(0), 0.401f); + assertEquals(result.asSet().size(), 1); + assertEquals(result.asSet().iterator().next().getScores().get(0), + 0.401f); } catch (WrongParameterException e) { e.printStackTrace(); -- 1.7.10.2