From cce351a09ce6e405143192d80b414423f0fb7c06 Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Wed, 24 Nov 2010 12:43:52 +0000 Subject: [PATCH] More work on AAConWS not finished yet! git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@3376 e3abac25-378b-4346-85de-24260fe3988d --- .../compbio/data/sequence/AnnotatedSequence.java | 99 ++++++++++---------- datamodel/compbio/data/sequence/FastaSequence.java | 2 +- .../data/sequence/MultiAnnotatedSequence.java | 17 +++- datamodel/compbio/data/sequence/SequenceUtil.java | 45 ++++++++- 4 files changed, 108 insertions(+), 55 deletions(-) diff --git a/datamodel/compbio/data/sequence/AnnotatedSequence.java b/datamodel/compbio/data/sequence/AnnotatedSequence.java index 5d09534..65dbce9 100644 --- a/datamodel/compbio/data/sequence/AnnotatedSequence.java +++ b/datamodel/compbio/data/sequence/AnnotatedSequence.java @@ -4,53 +4,58 @@ import java.util.Arrays; public class AnnotatedSequence extends FastaSequence { - private final float[] annotation; - - public AnnotatedSequence(String id, String sequence, float[] annotation) { - super(id, sequence); - this.annotation = annotation; - if (annotation == null || annotation.length != sequence.length()) { - throw new IllegalArgumentException("The length of the annotation (" - + ((annotation != null) ? annotation.length : "0") - + ") does not match the length of the sequence (" - + sequence.length() + ")!"); + private float[] annotation; + + private AnnotatedSequence() { + super(); + // JAXB default constructor + } + + public AnnotatedSequence(String id, String sequence, float[] annotation) { + super(id, sequence); + this.annotation = annotation; + if (annotation == null || annotation.length != sequence.length()) { + throw new IllegalArgumentException("The length of the annotation (" + + ((annotation != null) ? annotation.length : "0") + + ") does not match the length of the sequence (" + + sequence.length() + ")!"); + } + } + + public AnnotatedSequence(FastaSequence fsequence, float[] annotation) { + this(fsequence.getId(), fsequence.getSequence(), annotation); + } + + public float[] getAnnotation() { + return annotation; + } + + @Override + public int hashCode() { + final int prime = 7; + int result = super.hashCode(); + result = prime * result + Arrays.hashCode(annotation); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + AnnotatedSequence other = (AnnotatedSequence) obj; + if (!Arrays.equals(annotation, other.annotation)) + return false; + return true; + } + + @Override + public String toString() { + return super.toString() + "Annotation:\n " + + Arrays.toString(annotation) + "\n"; } - } - - public AnnotatedSequence(FastaSequence fsequence, float[] annotation) { - this(fsequence.getId(), fsequence.getSequence(), annotation); - } - - public float[] getAnnotation() { - return annotation; - } - - @Override - public int hashCode() { - final int prime = 7; - int result = super.hashCode(); - result = prime * result + Arrays.hashCode(annotation); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - AnnotatedSequence other = (AnnotatedSequence) obj; - if (!Arrays.equals(annotation, other.annotation)) - return false; - return true; - } - - @Override - public String toString() { - return super.toString() + "Annotation:\n " - + Arrays.toString(annotation) + "\n"; - } } diff --git a/datamodel/compbio/data/sequence/FastaSequence.java b/datamodel/compbio/data/sequence/FastaSequence.java index 1da5900..a5c92c1 100644 --- a/datamodel/compbio/data/sequence/FastaSequence.java +++ b/datamodel/compbio/data/sequence/FastaSequence.java @@ -50,7 +50,7 @@ public class FastaSequence { */ private String sequence; - private FastaSequence() { + FastaSequence() { // Default constructor for JaxB } diff --git a/datamodel/compbio/data/sequence/MultiAnnotatedSequence.java b/datamodel/compbio/data/sequence/MultiAnnotatedSequence.java index 1a889e3..d5942b3 100644 --- a/datamodel/compbio/data/sequence/MultiAnnotatedSequence.java +++ b/datamodel/compbio/data/sequence/MultiAnnotatedSequence.java @@ -1,9 +1,13 @@ package compbio.data.sequence; +import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; + import compbio.util.annotation.NotThreadSafe; /** @@ -15,15 +19,20 @@ import compbio.util.annotation.NotThreadSafe; * enum type */ @NotThreadSafe +@XmlAccessorType(XmlAccessType.FIELD) public class MultiAnnotatedSequence> { - private final Map> annotations; + private EnumMap> annotations; + + MultiAnnotatedSequence() { + // default constructor for JAXB + } public MultiAnnotatedSequence(Class enumeration) { - this.annotations = new EnumMap>(enumeration); + this.annotations = new EnumMap>(enumeration); } - public void addAnnotation(T type, List annotation) { + public void addAnnotation(T type, ArrayList annotation) { assert type != null : "Type is expected"; assert annotation != null : "Not empty value is expected!"; if (!annotations.isEmpty()) { @@ -66,7 +75,7 @@ public class MultiAnnotatedSequence> { @Override public String toString() { String value = ""; - for (Map.Entry> annt : annotations.entrySet()) { + for (Map.Entry> annt : annotations.entrySet()) { value += annt.getKey() + " "; value += annt.getValue() + "\n"; } diff --git a/datamodel/compbio/data/sequence/SequenceUtil.java b/datamodel/compbio/data/sequence/SequenceUtil.java index 149e0e0..5946e78 100644 --- a/datamodel/compbio/data/sequence/SequenceUtil.java +++ b/datamodel/compbio/data/sequence/SequenceUtil.java @@ -31,6 +31,8 @@ import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; +import compbio.conservation.Method; + /** * Utility class for operations on sequences * @@ -421,9 +423,9 @@ public final class SequenceUtil { String singleSeq = scan.next(); Scanner scansingle = new Scanner(singleSeq); StringBuffer seqbuffer = new StringBuffer(); - List coils = new ArrayList(); - List rem = new ArrayList(); - List hotloops = new ArrayList(); + ArrayList coils = new ArrayList(); + ArrayList rem = new ArrayList(); + ArrayList hotloops = new ArrayList(); MultiAnnotatedSequence disemblRes = new MultiAnnotatedSequence( DisemblResultAnnot.class); @@ -450,4 +452,41 @@ public final class SequenceUtil { return results; } + /** + * Read AACon result with no alignment files. This method leaves incoming + * the InputStream results open! + * + * @param results + * output file of AAConservation + * @return {@link MultiAnnotatedSequence} + */ + public static MultiAnnotatedSequence readResults(InputStream results) { + if (results == null) { + throw new NullPointerException( + "InputStream with results must be provided"); + } + MultiAnnotatedSequence annotations = new MultiAnnotatedSequence( + Method.class); + Scanner sc = new Scanner(results); + sc.useDelimiter("#"); + while (sc.hasNext()) { + String line = sc.next(); + int spacePos = line.indexOf(" "); + assert spacePos > 0 : "Space is expected as delimited between method " + + "name and values!"; + String methodLine = line.substring(0, spacePos); + Method method = Method.getMethod(methodLine); + assert method != null : "Method " + methodLine + + " is not recognized! "; + Scanner valuesScanner = new Scanner(line.substring(spacePos)); + ArrayList values = new ArrayList(); + while (valuesScanner.hasNextDouble()) { + Double value = valuesScanner.nextDouble(); + values.add(value.floatValue()); + } + annotations.addAnnotation(method, values); + } + return annotations; + } + } -- 1.7.10.2