From 9d860c3793c6812f643caa01fc80a0036fa3fe2b Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Wed, 9 Feb 2011 17:17:43 +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@3731 e3abac25-378b-4346-85de-24260fe3988d --- datamodel/compbio/data/sequence/ScoreManager.java | 77 ++++++++++++++++++++ how_to_add_new_webservice.txt | 9 ++- .../compbio/ws/client/AAConWSClientExample.java | 6 +- testsrc/compbio/ws/client/TestAAConWS.java | 51 +++++++------ .../compbio/data/msa/SequenceAnnotation.java | 7 +- webservices/compbio/ws/client/AAConClient.java | 23 ++---- webservices/compbio/ws/client/IOHelper.java | 12 +-- webservices/compbio/ws/client/Jws2Client.java | 18 ++--- webservices/compbio/ws/server/AAConWS.java | 11 +-- webservices/compbio/ws/server/DisemblWS.java | 8 +- webservices/compbio/ws/server/GlobPlotWS.java | 8 +- webservices/compbio/ws/server/JronnWS.java | 8 +- 12 files changed, 143 insertions(+), 95 deletions(-) create mode 100644 datamodel/compbio/data/sequence/ScoreManager.java diff --git a/datamodel/compbio/data/sequence/ScoreManager.java b/datamodel/compbio/data/sequence/ScoreManager.java new file mode 100644 index 0000000..b0f87a6 --- /dev/null +++ b/datamodel/compbio/data/sequence/ScoreManager.java @@ -0,0 +1,77 @@ +package compbio.data.sequence; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.HashSet; +import java.util.List; + +public class ScoreManager { + + private List seqScores; + + private ScoreManager() { + // Default JAXB constructor + } + + public int getNumberOfSeq() { + return seqScores.size(); + } + + public ScoreHolder getFirstSequenceScores() { + if (seqScores.isEmpty()) + return null; + return seqScores.get(0); + } + + public ScoreHolder getAnnotationForSequence(String seqId) { + for (ScoreHolder sch : seqScores) { + if (sch.id.equals(seqId)) { + return sch; + } + } + return null; + } + + public void writeOut(OutputStream outStream) throws IOException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + outStream)); + for (ScoreHolder oneSeqScores : seqScores) { + oneSeqScores.writeOut(outStream); + } + writer.flush(); + } + + public static class ScoreHolder { + + public String id; + public HashSet scores; + + public void writeOut(OutputStream outStream) throws IOException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + outStream)); + writer.write(">" + id + "\n"); + 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()) { + return sc; + } + } + return null; + } + + public int getNumberOfScores() { + return scores.size(); + } + } +} diff --git a/how_to_add_new_webservice.txt b/how_to_add_new_webservice.txt index 7df641e..cb11ba4 100644 --- a/how_to_add_new_webservice.txt +++ b/how_to_add_new_webservice.txt @@ -35,8 +35,12 @@ Edit binaries/src setexecutableflag.sh and compilebin.sh scripts accordingly. 14) Run build-server task in wsbuild file. Watch for errors. If cannot compile that means that JAXB cannot serialize some of the data structures. Add appropriate annotations to your data types. Check that - - you do not have interfaces as fields - - you have a default no args constructor (can be private if you do not need it) + - you do not have interfaces to serialize. JAXB cannot serialize them. + - you have a default no args constructor (can be private if you do not need it) + - JAXB cannot serialize a Map, use custom data structure instead! +If you have the data on the server side, but nothing is coming through to the client, +this is a JAXB serialization problem. They tend to be very silent and thus hard to debug. Check your +data structure can be serialized! 13) Modify the client to work with your web service. @@ -46,7 +50,6 @@ Check that - If executable does not support parameters do not add a reference to the XXXParameter.xml file into the Executable.properties file. Do the same for Presets diff --git a/testsrc/compbio/ws/client/AAConWSClientExample.java b/testsrc/compbio/ws/client/AAConWSClientExample.java index 4178ada..307d144 100644 --- a/testsrc/compbio/ws/client/AAConWSClientExample.java +++ b/testsrc/compbio/ws/client/AAConWSClientExample.java @@ -3,13 +3,11 @@ package compbio.ws.client; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.HashSet; import java.util.List; -import java.util.Map; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.metadata.JobSubmissionException; import compbio.metadata.Preset; @@ -96,7 +94,7 @@ public class AAConWSClientExample { String jobId = client.presetAnalize(fastalist, preset); /* This method will block for the duration of the calculation */ - Map> result = client.getAnnotation(jobId); + ScoreManager result = client.getAnnotation(jobId); /* * This is a better way of obtaining results, it does not involve diff --git a/testsrc/compbio/ws/client/TestAAConWS.java b/testsrc/compbio/ws/client/TestAAConWS.java index 4fa90ac..6476311 100644 --- a/testsrc/compbio/ws/client/TestAAConWS.java +++ b/testsrc/compbio/ws/client/TestAAConWS.java @@ -8,10 +8,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @@ -20,7 +17,7 @@ import compbio.data.msa.JABAService; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.ConservationMethod; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.metadata.JobSubmissionException; import compbio.metadata.LimitExceededException; @@ -78,13 +75,14 @@ public class TestAAConWS { System.out.println("Pres: " + msaws.getPresets().getPresets()); String jobId = msaws.analize(fsl); System.out.println("J: " + jobId); - Map> result = msaws.getAnnotation(jobId); + ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 1); - assertEquals(result.values().iterator().next().iterator().next() + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); + + assertEquals(result.getFirstSequenceScores().getFirstScore() .getMethod(), ConservationMethod.SHENKIN); - List scores = result.values().iterator().next().iterator() - .next().getScores(); + List scores = result.getFirstSequenceScores() + .getFirstScore().getScores(); assertNotNull(scores); assertEquals(scores.size(), 568); @@ -94,19 +92,21 @@ public class TestAAConWS { presets.getPresetByName("Quick conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 13); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), + 13); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Slow conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 5); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 5); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Complete conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 18); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), + 18); } catch (UnsupportedRuntimeException e) { e.printStackTrace(); @@ -126,7 +126,6 @@ public class TestAAConWS { } } - @Test public void testPresetAnalize() throws FileNotFoundException, IOException { @@ -144,21 +143,23 @@ public class TestAAConWS { PresetManager presets = msaws.getPresets(); String jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Quick conservation")); - Map> result = msaws.getAnnotation(jobId); + ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 13); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), + 13); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Slow conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 5); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 5); jobId = msaws.presetAnalize(fsl, presets.getPresetByName("Complete conservation")); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 18); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), + 18); } catch (UnsupportedRuntimeException e) { e.printStackTrace(); @@ -199,12 +200,11 @@ public class TestAAConWS { // .setDefaultValue("MAX_SCORE"); // options.getArgument("SMERFS Gap Threshhold").setDefaultValue("1"); String jobId = msaws.customAnalize(fsl, options.getArguments()); - Map> result = msaws.getAnnotation(jobId); + ScoreManager result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 1); - assertEquals( - new ArrayList(result.values().iterator().next()) - .get(0).getScores().get(0), 0.698f); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); + assertEquals(result.getFirstSequenceScores().getFirstScore() + .getScores().get(0), 0.698f); options.getArgument("Calculation method").setDefaultValue("SMERFS"); options.removeArgument("Normalize"); @@ -212,10 +212,9 @@ public class TestAAConWS { jobId = msaws.customAnalize(fsl, options.getArguments()); result = msaws.getAnnotation(jobId); assertNotNull(result); - assertEquals(result.size(), 1); - assertEquals( - new ArrayList(result.values().iterator().next()) - .get(0).getScores().get(0), 0.401f); + assertEquals(result.getFirstSequenceScores().getNumberOfScores(), 1); + assertEquals(result.getFirstSequenceScores().getFirstScore() + .getScores().get(0), 0.401f); } catch (WrongParameterException e) { e.printStackTrace(); diff --git a/webservices/compbio/data/msa/SequenceAnnotation.java b/webservices/compbio/data/msa/SequenceAnnotation.java index d238d6b..c52c030 100644 --- a/webservices/compbio/data/msa/SequenceAnnotation.java +++ b/webservices/compbio/data/msa/SequenceAnnotation.java @@ -1,8 +1,6 @@ package compbio.data.msa; import java.security.InvalidParameterException; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import javax.jws.WebMethod; @@ -10,7 +8,7 @@ import javax.jws.WebParam; import javax.jws.WebService; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.metadata.JobSubmissionException; import compbio.metadata.LimitExceededException; import compbio.metadata.Option; @@ -194,8 +192,7 @@ public interface SequenceAnnotation * */ @WebMethod - HashMap> getAnnotation( - @WebParam(name = "jobId") String jobId) + ScoreManager getAnnotation(@WebParam(name = "jobId") String jobId) throws ResultNotAvailableException; /* * The method should really return Map and Set, but unfortunately JAXB diff --git a/webservices/compbio/ws/client/AAConClient.java b/webservices/compbio/ws/client/AAConClient.java index 57cf59f..4619c60 100644 --- a/webservices/compbio/ws/client/AAConClient.java +++ b/webservices/compbio/ws/client/AAConClient.java @@ -33,10 +33,7 @@ import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -46,7 +43,7 @@ import javax.xml.ws.WebServiceException; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.metadata.JobSubmissionException; @@ -115,7 +112,7 @@ public class AAConClient { customOptions = MetadataHelper.processParameters(prms, msaws.getRunnerOptions()); } - Map> result = null; + ScoreManager result = null; if (inputFile != null) { System.out.println("Calculating conservation..."); result = analize(inputFile, msaws, preset, customOptions); @@ -155,14 +152,9 @@ public class AAConClient { * @param result * the AACon scores to output */ - static void writeOut(OutputStream outStream, - Map> result) { + static void writeOut(OutputStream outStream, ScoreManager result) { try { - for (Map.Entry> entry : result - .entrySet()) { - System.out.println(">" + entry.getKey()); - Score.write(entry.getValue(), outStream); - } + result.writeOut(outStream); } catch (IOException e) { System.err .println("Problems writing output file! Stack trace is below: "); @@ -222,12 +214,11 @@ public class AAConClient { * @return Set the conservation scores * @throws UnknownFileFormatException */ - static Map> analize(File file, - SequenceAnnotation wsproxy, Preset preset, - List> customOptions) { + static ScoreManager analize(File file, SequenceAnnotation wsproxy, + Preset preset, List> customOptions) { List fastalist = null; - Map> scores = null; + ScoreManager scores = null; try { fastalist = SequenceUtil.openInputStream(file.getAbsolutePath()); diff --git a/webservices/compbio/ws/client/IOHelper.java b/webservices/compbio/ws/client/IOHelper.java index 414af9c..8fbf763 100644 --- a/webservices/compbio/ws/client/IOHelper.java +++ b/webservices/compbio/ws/client/IOHelper.java @@ -10,13 +10,11 @@ import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; import compbio.data.sequence.Alignment; import compbio.data.sequence.ClustalAlignmentUtil; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; public class IOHelper { @@ -119,13 +117,9 @@ public class IOHelper { * @param result * the AACon scores to output */ - static void writeOut(OutputStream outStream, - Map> result) { + static void writeOut(OutputStream outStream, ScoreManager results) { try { - for (Map.Entry> entry : result.entrySet()) { - System.out.println(">" + entry.getKey()); - Score.write(entry.getValue(), outStream); - } + results.writeOut(outStream); } catch (IOException e) { System.err .println("Problems writing output file! Stack trace is below: "); diff --git a/webservices/compbio/ws/client/Jws2Client.java b/webservices/compbio/ws/client/Jws2Client.java index ec3cf65..eda6533 100644 --- a/webservices/compbio/ws/client/Jws2Client.java +++ b/webservices/compbio/ws/client/Jws2Client.java @@ -37,10 +37,7 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,7 +50,7 @@ import compbio.data.msa.MsaWS; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.Alignment; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.metadata.JobSubmissionException; @@ -162,9 +159,9 @@ public class Jws2Client { outStream = System.out; } if (service.getServiceType() == SequenceAnnotation.class) { - Map> result = analize(inputFile, + ScoreManager result = analize(inputFile, ((SequenceAnnotation) msaws), preset, customOptions); - assert !result.values().isEmpty() : "No Result reported!"; + IOHelper.writeOut(outStream, result); } else { alignment = align(inputFile, (MsaWS) msaws, preset, @@ -204,12 +201,11 @@ public class Jws2Client { * @return Set the conservation scores * @throws UnknownFileFormatException */ - HashMap> analize(File file, - SequenceAnnotation wsproxy, Preset preset, - List> customOptions) { + ScoreManager analize(File file, SequenceAnnotation wsproxy, + Preset preset, List> customOptions) { List fastalist = null; - HashMap> scores = null; + ScoreManager scores = null; try { fastalist = SequenceUtil.openInputStream(file.getAbsolutePath()); assert !fastalist.isEmpty() : "Input is empty!"; @@ -230,8 +226,6 @@ public class Jws2Client { Thread.sleep(2000); scores = wsproxy.getAnnotation(jobId); - assert scores != null && !scores.values().isEmpty() : "Scores are NULL for job: " - + jobId; } catch (IOException e) { System.err diff --git a/webservices/compbio/ws/server/AAConWS.java b/webservices/compbio/ws/server/AAConWS.java index 433728b..095bc4e 100644 --- a/webservices/compbio/ws/server/AAConWS.java +++ b/webservices/compbio/ws/server/AAConWS.java @@ -2,7 +2,6 @@ package compbio.ws.server; import java.io.File; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -15,6 +14,7 @@ import org.apache.log4j.Logger; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.engine.AsyncExecutor; import compbio.engine.Configurator; import compbio.engine.client.ConfiguredExecutable; @@ -59,17 +59,18 @@ public class AAConWS implements SequenceAnnotation { } @Override - public HashMap> getAnnotation(String jobId) + public ScoreManager getAnnotation(String jobId) throws ResultNotAvailableException { WSUtil.validateJobId(jobId); AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId); ConfiguredExecutable aacon = (ConfiguredExecutable) asyncEngine .getResults(jobId); HashSet mas = aacon.getResults(); - HashMap> result = new HashMap>(); - result.put("Alignment", mas); + // ScoreManager result = new HashMap>(); + // result.put("Alignment", mas); log.trace(jobId + " getConservation : " + mas); - return result; + // FIXME + return null; } /* * @SuppressWarnings("unchecked") public JalviewAnnotation diff --git a/webservices/compbio/ws/server/DisemblWS.java b/webservices/compbio/ws/server/DisemblWS.java index 061f9a6..2eac30d 100644 --- a/webservices/compbio/ws/server/DisemblWS.java +++ b/webservices/compbio/ws/server/DisemblWS.java @@ -1,7 +1,5 @@ package compbio.ws.server; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import javax.annotation.Resource; @@ -12,7 +10,7 @@ import org.apache.log4j.Logger; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.engine.AsyncExecutor; import compbio.engine.Configurator; import compbio.engine.client.ConfiguredExecutable; @@ -57,13 +55,13 @@ public class DisemblWS implements SequenceAnnotation { } @Override - public HashMap> getAnnotation(String jobId) + public ScoreManager getAnnotation(String jobId) throws ResultNotAvailableException { WSUtil.validateJobId(jobId); AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId); ConfiguredExecutable disembl = (ConfiguredExecutable) asyncEngine .getResults(jobId); - HashMap> mas = disembl.getResults(); + ScoreManager mas = disembl.getResults(); log.trace(jobId + " getDisorder : " + mas); return mas; } diff --git a/webservices/compbio/ws/server/GlobPlotWS.java b/webservices/compbio/ws/server/GlobPlotWS.java index df402fa..cf266b0 100644 --- a/webservices/compbio/ws/server/GlobPlotWS.java +++ b/webservices/compbio/ws/server/GlobPlotWS.java @@ -1,7 +1,5 @@ package compbio.ws.server; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import javax.annotation.Resource; @@ -12,7 +10,7 @@ import org.apache.log4j.Logger; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.engine.AsyncExecutor; import compbio.engine.Configurator; import compbio.engine.client.ConfiguredExecutable; @@ -57,13 +55,13 @@ public class GlobPlotWS implements SequenceAnnotation { } @Override - public HashMap> getAnnotation(String jobId) + public ScoreManager getAnnotation(String jobId) throws ResultNotAvailableException { WSUtil.validateJobId(jobId); AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId); ConfiguredExecutable globPlot = (ConfiguredExecutable) asyncEngine .getResults(jobId); - HashMap> mas = globPlot.getResults(); + ScoreManager mas = globPlot.getResults(); log.trace(jobId + " getConservation : " + mas); return mas; } diff --git a/webservices/compbio/ws/server/JronnWS.java b/webservices/compbio/ws/server/JronnWS.java index 13fe708..e9070d5 100644 --- a/webservices/compbio/ws/server/JronnWS.java +++ b/webservices/compbio/ws/server/JronnWS.java @@ -1,8 +1,6 @@ package compbio.ws.server; import java.io.File; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import javax.annotation.Resource; @@ -13,7 +11,7 @@ import org.apache.log4j.Logger; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; import compbio.engine.AsyncExecutor; import compbio.engine.Configurator; import compbio.engine.client.ConfiguredExecutable; @@ -59,13 +57,13 @@ public class JronnWS implements SequenceAnnotation { } @Override - public HashMap> getAnnotation(String jobId) + public ScoreManager getAnnotation(String jobId) throws ResultNotAvailableException { WSUtil.validateJobId(jobId); AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId); ConfiguredExecutable jronn = (ConfiguredExecutable) asyncEngine .getResults(jobId); - HashMap> mas = jronn.getResults(); + ScoreManager mas = jronn.getResults(); log.trace(jobId + " getConservation : " + mas); return mas; } -- 1.7.10.2