applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / Seq.java
index 7c82d82..538d658 100644 (file)
-/*
- * Created on 17-May-2005
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
-package uk.ac.vamsas.objects.utils;
-
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.util.regex.Pattern;
-
-import uk.ac.vamsas.objects.core.AlignmentSequence;
-import uk.ac.vamsas.objects.core.Sequence;
-import uk.ac.vamsas.objects.core.SequenceType;
-
-/**
- * @author jimp
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
-public class Seq {
-
-       public static void write_PirSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
-               BufferedWriter pir_out = new BufferedWriter(new OutputStreamWriter(os));
-               pir_out.write(">P1;"+seq.getName()+"\n");
-               int width = (wid<1) ? 80 : wid;
-               for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
-                       if (j+width<k)
-                               pir_out.write(seq.getSequence().substring(j,j+width)+"\n");
-                       else
-                               pir_out.write(seq.getSequence().substring(j)+"\n");
-               pir_out.flush();
-         }
-  
-       public static void write_FastaSeq(OutputStream os, SequenceType seq) throws IOException {
-               BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
-               fasta_out.write(">"+seq.getName()+"\n");
-               fasta_out.write(seq.getSequence()+"\n");
-               fasta_out.flush();
-         }
-
-       public static void write_FastaSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
-               BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
-               fasta_out.write(">"+seq.getName()+"\n");
-               int width = (wid<1) ? 80 : wid;
-               for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
-                       if (j+width<k)
-                               fasta_out.write(seq.getSequence().substring(j,j+width)+"\n");
-                       else
-                               fasta_out.write(seq.getSequence().substring(j)+"\n");
-               fasta_out.flush();
-         }
-       /**
-   *validate a SequenceType Vobject as an info:iubmb.org/aminoacid SequenceType
-   *This version resolves references to Sequence objects from AlignmentSequence
-   *TODO: Define info: urn for dictionary string (could also be regex of valid characters!)
-   * @param s
-        * @param dict TODO
-   * @return true if a valid amino acid sequence Vobject
-        */
-       private static boolean valid_aadictionary_string(String s, String dict) {
-    if (s==null)
-      return false;
-    // validate against dictionary
-    // TODO generalise to resolve dictionary against info: urn for dictionary type
-      Pattern aa_repl = Pattern.compile("[ARNDCQEGHILKMFPSTWYVUX]+", Pattern.CASE_INSENSITIVE);
-      String remnants = aa_repl.matcher(s).replaceAll("");
-      return !remnants.matches("//S+");
-  }
-
-  public static Sequence newSequence(String Name, String Sequence, String Dictionary, int start, int end) {
-    //TODO: make hierarchy reflecting the SeqType Vobject.
-    Sequence seq= new Sequence();
-     seq.setDictionary(Dictionary);
-     seq.setName(Name);
-     seq.setSequence(Sequence);
-     seq.setStart(start);
-     if (start<=end) {
-       if ((end-start)!=Sequence.length())
-         seq.setEnd(start+Sequence.length());
-     } else {
-       // reverse topology mapping. TODO: VAMSAS: decide if allowed to do start>end on Sequence Vobject
-       if ((start-end)!=Sequence.length())
-         seq.setEnd(end+Sequence.length());
-     }
-     return seq;
-  }
-  public static AlignmentSequence newAlignmentSequence(String name, String alSequence, Sequence refseq, long start, long end) {
-    if (refseq!=null) {
-      AlignmentSequence asq = new AlignmentSequence();
-      asq.setName(name);
-      asq.setSequence(alSequence);
-      asq.setRefid(refseq);
-      if (end>refseq.getEnd() || end<start || end==-1)
-        end = refseq.getEnd();
-      asq.setEnd(end);
-      if (start<refseq.getStart())
-        start = refseq.getStart();
-      asq.setStart(start);
-      return asq;
-    }
-    return null;
-  }
-  public static boolean is_valid_aa_seq(SequenceType s) {
-    Sequence q;
-    boolean validref=false;
-    if (s instanceof Sequence) {
-      q=(Sequence) s;
-      if (q.getDictionary()!=null 
-    
-        && q.getDictionary().length()>0
-        || !q.getDictionary().equals(SymbolDictionary.STANDARD_AA))
-        return false;
-      return valid_aadictionary_string(q.getSequence(), SymbolDictionary.STANDARD_AA);
-    }
-    
-    // follow references
-    if (s instanceof AlignmentSequence) {
-      Object w = (((AlignmentSequence) s).getRefid());
-      if (w!=null && w!=s && w instanceof SequenceType)
-        return is_valid_aa_seq((SequenceType) w) 
-        && valid_aadictionary_string(((AlignmentSequence) s).getSequence(), SymbolDictionary.STANDARD_AA);
-    }
-    
-    return false;
-  }
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.objects.utils;\r
+\r
+import java.io.BufferedWriter;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
+import java.util.regex.Pattern;\r
+\r
+import uk.ac.vamsas.objects.core.AlignmentSequence;\r
+import uk.ac.vamsas.objects.core.Sequence;\r
+import uk.ac.vamsas.objects.core.SequenceType;\r
+\r
+/**\r
+ * @author jimp\r
+ * \r
+ *         TODO To change the template for this generated type comment go to\r
+ *         Window - Preferences - Java - Code Style - Code Templates\r
+ */\r
+public class Seq {\r
+\r
+  public static void write_PirSeq(OutputStream os, SequenceType seq, int wid)\r
+      throws IOException {\r
+    BufferedWriter pir_out = new BufferedWriter(new OutputStreamWriter(os));\r
+    pir_out.write(">P1;" + seq.getName() + "\n");\r
+    int width = (wid < 1) ? 80 : wid;\r
+    for (int j = 0, k = seq.getSequence().length(); j < k; j += width)\r
+      if (j + width < k)\r
+        pir_out.write(seq.getSequence().substring(j, j + width) + "\n");\r
+      else\r
+        pir_out.write(seq.getSequence().substring(j) + "\n");\r
+    pir_out.flush();\r
+  }\r
+\r
+  public static void write_FastaSeq(OutputStream os, SequenceType seq)\r
+      throws IOException {\r
+    BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));\r
+    fasta_out.write(">" + seq.getName() + "\n");\r
+    fasta_out.write(seq.getSequence() + "\n");\r
+    fasta_out.flush();\r
+  }\r
+\r
+  public static void write_FastaSeq(OutputStream os, SequenceType seq, int wid)\r
+      throws IOException {\r
+    BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));\r
+    fasta_out.write(">" + seq.getName() + "\n");\r
+    int width = (wid < 1) ? 80 : wid;\r
+    for (int j = 0, k = seq.getSequence().length(); j < k; j += width)\r
+      if (j + width < k)\r
+        fasta_out.write(seq.getSequence().substring(j, j + width) + "\n");\r
+      else\r
+        fasta_out.write(seq.getSequence().substring(j) + "\n");\r
+    fasta_out.flush();\r
+  }\r
+\r
+  /**\r
+   *validate a SequenceType Vobject as an info:iubmb.org/aminoacid SequenceType\r
+   * This version resolves references to Sequence objects from AlignmentSequence\r
+   * TODO: Define info: urn for dictionary string (could also be regex of valid\r
+   * characters!)\r
+   * \r
+   * @param s\r
+   * @param dict\r
+   *          TODO\r
+   * @return true if a valid amino acid sequence Vobject\r
+   */\r
+  private static boolean valid_aadictionary_string(String s, String dict) {\r
+    if (s == null)\r
+      return false;\r
+    // validate against dictionary\r
+    // TODO generalise to resolve dictionary against info: urn for dictionary\r
+    // type\r
+    Pattern aa_repl = Pattern.compile("[ARNDCQEGHILKMFPSTWYVUX]+",\r
+        Pattern.CASE_INSENSITIVE);\r
+    String remnants = aa_repl.matcher(s).replaceAll("");\r
+    return !remnants.matches("//S+");\r
+  }\r
+\r
+  public static Sequence newSequence(String Name, String Sequence,\r
+      String Dictionary, int start, int end) {\r
+    // TODO: make hierarchy reflecting the SeqType Vobject.\r
+    Sequence seq = new Sequence();\r
+    seq.setDictionary(Dictionary);\r
+    seq.setName(Name);\r
+    seq.setSequence(Sequence);\r
+    seq.setStart(start);\r
+    if (start <= end) {\r
+      if ((end - start) != Sequence.length())\r
+        seq.setEnd(start + Sequence.length());\r
+    } else {\r
+      // reverse topology mapping. TODO: VAMSAS: decide if allowed to do\r
+      // start>end on Sequence Vobject\r
+      if ((start - end) != Sequence.length())\r
+        seq.setEnd(end + Sequence.length());\r
+    }\r
+    return seq;\r
+  }\r
+\r
+  public static AlignmentSequence newAlignmentSequence(String name,\r
+      String alSequence, Sequence refseq, long start, long end) {\r
+    if (refseq != null) {\r
+      AlignmentSequence asq = new AlignmentSequence();\r
+      asq.setName(name);\r
+      asq.setSequence(alSequence);\r
+      asq.setRefid(refseq);\r
+      if (end > refseq.getEnd() || end < start || end == -1)\r
+        end = refseq.getEnd();\r
+      asq.setEnd(end);\r
+      if (start < refseq.getStart())\r
+        start = refseq.getStart();\r
+      asq.setStart(start);\r
+      return asq;\r
+    }\r
+    return null;\r
+  }\r
+\r
+  public static boolean is_valid_aa_seq(SequenceType s) {\r
+    Sequence q;\r
+    boolean validref = false;\r
+    if (s instanceof Sequence) {\r
+      q = (Sequence) s;\r
+      if (q.getDictionary() != null\r
+\r
+      && q.getDictionary().length() > 0\r
+          || !q.getDictionary().equals(SymbolDictionary.STANDARD_AA))\r
+        return false;\r
+      return valid_aadictionary_string(q.getSequence(),\r
+          SymbolDictionary.STANDARD_AA);\r
+    }\r
+\r
+    // follow references\r
+    if (s instanceof AlignmentSequence) {\r
+      Object w = (((AlignmentSequence) s).getRefid());\r
+      if (w != null && w != s && w instanceof SequenceType)\r
+        return is_valid_aa_seq((SequenceType) w)\r
+            && valid_aadictionary_string(((AlignmentSequence) s).getSequence(),\r
+                SymbolDictionary.STANDARD_AA);\r
+    }\r
+\r
+    return false;\r
+  }\r
+}\r