Adding AAConWS
[jabaws.git] / datamodel / compbio / data / sequence / FastaSequence.java
index 6072d29..2032fec 100644 (file)
@@ -1,19 +1,15 @@
-/* Copyright (c) 2009 Peter Troshin\r
- *  \r
- *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
- * \r
- *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
- *  Apache License version 2 as published by the Apache Software Foundation\r
- * \r
- *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
- *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
- *  License for more details.\r
- * \r
- *  A copy of the license is in apache_license.txt. It is also available here:\r
- * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
- * \r
- * Any republication or derived work distributed in source code form\r
- * must include this copyright and license notice.\r
+/*\r
+ * Copyright (c) 2009 Peter Troshin JAva Bioinformatics Analysis Web Services\r
+ * (JABAWS) @version: 1.0 This library is free software; you can redistribute it\r
+ * and/or modify it under the terms of the Apache License version 2 as published\r
+ * by the Apache Software Foundation This library is distributed in the hope\r
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * Apache License for more details. A copy of the license is in\r
+ * apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt Any republication or\r
+ * derived work distributed in source code form must include this copyright and\r
+ * license notice.\r
  */\r
 \r
 package compbio.data.sequence;\r
@@ -42,151 +38,155 @@ import compbio.util.annotation.Immutable;
 @Immutable\r
 public class FastaSequence {\r
 \r
-    /**\r
-     * Sequence id\r
-     */\r
-    private String id;\r
-\r
-    // TODO what about gapped sequence here! should be indicated\r
-    /**\r
-     * Returns the string representation of sequence\r
-     */\r
-    private String sequence;\r
-\r
-    private FastaSequence() {\r
-       // Default constructor for JaxB\r
-    }\r
-\r
-    /**\r
-     * Upon construction the any whitespace characters are removed from the\r
-     * sequence\r
-     * \r
-     * @param id\r
-     * @param sequence\r
-     */\r
-    public FastaSequence(String id, String sequence) {\r
-       this.id = id;\r
-       this.sequence = SequenceUtil.cleanSequence(sequence);\r
-    }\r
-\r
-    /**\r
-     * Gets the value of id\r
-     * \r
-     * @return the value of id\r
-     */\r
-    public String getId() {\r
-       return this.id;\r
-    }\r
-\r
-    /**\r
-     * Gets the value of sequence\r
-     * \r
-     * @return the value of sequence\r
-     */\r
-    public String getSequence() {\r
-       return this.sequence;\r
-    }\r
-\r
-    public static int countMatchesInSequence(final String theString,\r
-           final String theRegExp) {\r
-       final Pattern p = Pattern.compile(theRegExp);\r
-       final Matcher m = p.matcher(theString);\r
-       int cnt = 0;\r
-       while (m.find()) {\r
-           cnt++;\r
+       /**\r
+        * Sequence id\r
+        */\r
+       private String id;\r
+\r
+       // TODO what about gapped sequence here! should be indicated\r
+       /**\r
+        * Returns the string representation of sequence\r
+        */\r
+       private String sequence;\r
+\r
+       private FastaSequence() {\r
+               // Default constructor for JaxB\r
        }\r
-       return cnt;\r
-    }\r
-\r
-    public String getFormattedFasta() {\r
-       return getFormatedSequence(80);\r
-    }\r
-\r
-    /**\r
-     * \r
-     * @return one line name, next line sequence, no matter what the sequence\r
-     *         length is\r
-     */\r
-    public String getOnelineFasta() {\r
-       String fasta = ">" + getId() + SysPrefs.newlinechar;\r
-       fasta += getSequence() + SysPrefs.newlinechar;\r
-       return fasta;\r
-    }\r
-\r
-    /**\r
-     * Format sequence per width letter in one string. Without spaces.\r
-     * \r
-     * @return multiple line formated sequence, one line width letters length\r
-     * \r
-     */\r
-    public String getFormatedSequence(final int width) {\r
-       if (sequence == null) {\r
-           return "";\r
+\r
+       /**\r
+        * Upon construction the any whitespace characters are removed from the\r
+        * sequence\r
+        * \r
+        * @param id\r
+        * @param sequence\r
+        */\r
+       public FastaSequence(String id, String sequence) {\r
+               this.id = id;\r
+               this.sequence = SequenceUtil.cleanSequence(sequence);\r
        }\r
 \r
-       assert width >= 0 : "Wrong width parameter ";\r
-\r
-       final StringBuilder sb = new StringBuilder(sequence);\r
-       int nchunks = sequence.length() / width;\r
-       // add up inserted new line chars\r
-       nchunks = (nchunks + sequence.length()) / width;\r
-       int nlineCharcounter = 0;\r
-       for (int i = 1; i <= nchunks; i++) {\r
-           int insPos = width * i + nlineCharcounter;\r
-           // to prevent inserting new line in the very end of a sequence then\r
-           // it would have failed.\r
-           // Also covers the case when the sequences shorter than width\r
-           if (sb.length() <= insPos) {\r
-               break;\r
-           }\r
-           sb.insert(insPos, "\n");\r
-           nlineCharcounter++;\r
+       /**\r
+        * Gets the value of id\r
+        * \r
+        * @return the value of id\r
+        */\r
+       public String getId() {\r
+               return this.id;\r
        }\r
-       return sb.toString();\r
-    }\r
-\r
-    /**\r
-     * \r
-     * @return sequence length\r
-     */\r
-    public int getLength() {\r
-       return this.sequence.length();\r
-    }\r
-\r
-    /**\r
-     * Same as oneLineFasta\r
-     */\r
-    @Override\r
-    public String toString() {\r
-       return this.getOnelineFasta();\r
-    }\r
-\r
-    @Override\r
-    public int hashCode() {\r
-       final int prime = 17;\r
-       int result = 1;\r
-       result = prime * result + ((id == null) ? 0 : id.hashCode());\r
-       result = prime * result\r
-               + ((sequence == null) ? 0 : sequence.hashCode());\r
-       return result;\r
-    }\r
-\r
-    @Override\r
-    public boolean equals(Object obj) {\r
-       if (obj == null) {\r
-           return false;\r
+\r
+       /**\r
+        * Gets the value of sequence\r
+        * \r
+        * @return the value of sequence\r
+        */\r
+       public String getSequence() {\r
+               return this.sequence;\r
        }\r
-       if (!(obj instanceof FastaSequence)) {\r
-           return false;\r
+\r
+       public static int countMatchesInSequence(final String theString,\r
+                       final String theRegExp) {\r
+               final Pattern p = Pattern.compile(theRegExp);\r
+               final Matcher m = p.matcher(theString);\r
+               int cnt = 0;\r
+               while (m.find()) {\r
+                       cnt++;\r
+               }\r
+               return cnt;\r
        }\r
-       FastaSequence fs = (FastaSequence) obj;\r
-       if (!fs.getId().equals(this.getId())) {\r
-           return false;\r
+\r
+       public String getFormattedFasta() {\r
+               return getFormatedSequence(80);\r
        }\r
-       if (!fs.getSequence().equalsIgnoreCase(this.getSequence())) {\r
-           return false;\r
+\r
+       /**\r
+        * \r
+        * @return one line name, next line sequence, no matter what the sequence\r
+        *         length is\r
+        */\r
+       public String getOnelineFasta() {\r
+               String fasta = ">" + getId() + SysPrefs.newlinechar;\r
+               fasta += getSequence() + SysPrefs.newlinechar;\r
+               return fasta;\r
+       }\r
+\r
+       /**\r
+        * Format sequence per width letter in one string. Without spaces.\r
+        * \r
+        * @return multiple line formated sequence, one line width letters length\r
+        * \r
+        */\r
+       public String getFormatedSequence(final int width) {\r
+               if (sequence == null) {\r
+                       return "";\r
+               }\r
+\r
+               assert width >= 0 : "Wrong width parameter ";\r
+\r
+               final StringBuilder sb = new StringBuilder(sequence);\r
+               // int tail = nrOfWindows % WIN_SIZE;\r
+               // final int turns = (nrOfWindows - tail) / WIN_SIZE;\r
+\r
+               int tailLen = sequence.length() % width;\r
+               // add up inserted new line chars\r
+               int nchunks = (sequence.length() - tailLen) / width;\r
+               int nlineCharcounter = 0;\r
+               int insPos = 0;\r
+               for (int i = 1; i <= nchunks; i++) {\r
+                       insPos = width * i + nlineCharcounter;\r
+                       // to prevent inserting new line in the very end of a sequence then\r
+                       // it would have failed.\r
+                       if (sb.length() <= insPos) {\r
+                               break;\r
+                       }\r
+                       sb.insert(insPos, "\n");\r
+                       nlineCharcounter++;\r
+               }\r
+               // sb.insert(insPos + tailLen, "\n");\r
+               return sb.toString();\r
+       }\r
+\r
+       /**\r
+        * \r
+        * @return sequence length\r
+        */\r
+       public int getLength() {\r
+               return this.sequence.length();\r
+       }\r
+\r
+       /**\r
+        * Same as oneLineFasta\r
+        */\r
+       @Override\r
+       public String toString() {\r
+               return this.getOnelineFasta();\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               final int prime = 17;\r
+               int result = 1;\r
+               result = prime * result + ((id == null) ? 0 : id.hashCode());\r
+               result = prime * result\r
+                               + ((sequence == null) ? 0 : sequence.hashCode());\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj == null) {\r
+                       return false;\r
+               }\r
+               if (!(obj instanceof FastaSequence)) {\r
+                       return false;\r
+               }\r
+               FastaSequence fs = (FastaSequence) obj;\r
+               if (!fs.getId().equals(this.getId())) {\r
+                       return false;\r
+               }\r
+               if (!fs.getSequence().equalsIgnoreCase(this.getSequence())) {\r
+                       return false;\r
+               }\r
+               return true;\r
        }\r
-       return true;\r
-    }\r
 \r
 }\r