Formatting
[jalview.git] / src / jalview / io / PIRFile.java
index 0a24601..910d189 100755 (executable)
@@ -1,6 +1,6 @@
 /*\r
  * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
  *\r
  * This program is free software; you can redistribute it and/or\r
  * modify it under the terms of the GNU General Public License\r
@@ -21,23 +21,19 @@ package jalview.io;
 import java.io.*;\r
 import java.util.*;\r
 \r
-import jalview.analysis.*;\r
 import jalview.datamodel.*;\r
 \r
 public class PIRFile\r
     extends AlignFile\r
 {\r
+  public static boolean useModellerOutput = false;\r
+\r
   Vector words = new Vector(); //Stores the words in a line after splitting\r
 \r
   public PIRFile()\r
   {\r
   }\r
 \r
-  public PIRFile(String inStr)\r
-  {\r
-    super(inStr);\r
-  }\r
-\r
   public PIRFile(String inFile, String type)\r
       throws IOException\r
   {\r
@@ -45,79 +41,58 @@ public class PIRFile
   }\r
 \r
   public void parse()\r
+      throws IOException\r
   {\r
-    try\r
-    {\r
-      String id;\r
-      String start;\r
-      String end;\r
-      StringBuffer sequence;\r
-      String line = null;\r
+    StringBuffer sequence;\r
+    String line = null;\r
+    ModellerDescription md;\r
 \r
-      while ( (line = nextLine()) != null)\r
+    while ( (line = nextLine()) != null)\r
+    {\r
+      if (line.length() == 0)\r
       {\r
-        if(line.length()==0)\r
-        {\r
-          //System.out.println("blank line");\r
-          continue;\r
-        }\r
+        //System.out.println("blank line");\r
+        continue;\r
+      }\r
+      if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)\r
+      {\r
+        continue;\r
+      }\r
+      Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));\r
 \r
-        id = "No id";\r
-        start = "1";\r
-        end = "-1";\r
+      sequence = new StringBuffer();\r
 \r
-        try\r
-        {\r
-          int slashIndex = line.indexOf("/");\r
-          if(slashIndex!=-1)\r
-          {\r
-            id = line.substring(line.indexOf(";") + 1, line.indexOf("/"));\r
-            line = line.substring(line.indexOf("/") + 1);\r
-            start = line.substring(0, line.indexOf("-"));\r
-            end = line.substring(line.indexOf("-") + 1);\r
-          }\r
-          else\r
-          {\r
-            id = line.substring(line.indexOf(";")+1);\r
-          }\r
-        }\r
-        catch (Exception ex)\r
-        {    }// Default id, start and end unchanged\r
+      newSeq.setDescription(nextLine()); // this is the title line\r
 \r
-        sequence = new StringBuffer();\r
+      boolean starFound = false;\r
 \r
-        line = nextLine(); // this is the title line\r
-\r
-        boolean starFound = false;\r
+      while (!starFound)\r
+      {\r
+        line = nextLine();\r
+        sequence.append(line);\r
 \r
-        while(!starFound)\r
+        if (line == null)\r
         {\r
-          line = nextLine();\r
-          sequence.append(line);\r
-\r
-          if (line == null)\r
-            break;\r
-\r
-          if (line.indexOf("*") > -1)\r
-          {\r
-            starFound = true;\r
-          }\r
+          break;\r
         }\r
 \r
-        if(sequence.length()>0)\r
+        if (line.indexOf("*") > -1)\r
         {\r
-          sequence.setLength(sequence.length() - 1);\r
-\r
-          Sequence newSeq = new Sequence(id, sequence.toString(),\r
-                                         Integer.parseInt(start),\r
-                                         Integer.parseInt(end));\r
-          seqs.addElement(newSeq);\r
+          starFound = true;\r
         }\r
       }\r
-    }\r
-    catch (Exception ex)\r
-    {\r
-      ex.printStackTrace();\r
+\r
+      if (sequence.length() > 0)\r
+      {\r
+        sequence.setLength(sequence.length() - 1);\r
+        newSeq.setSequence(sequence.toString());\r
+\r
+        seqs.addElement(newSeq);\r
+\r
+        md = new ModellerDescription(newSeq.\r
+                                     getDescription());\r
+        md.updateSequenceI(newSeq);\r
+      }\r
     }\r
   }\r
 \r
@@ -126,41 +101,68 @@ public class PIRFile
     return print(getSeqsAsArray());\r
   }\r
 \r
-  public static String print(SequenceI[] s)\r
-  {\r
-    return print(s, 72, true);\r
-  }\r
-\r
-  public static String print(SequenceI[] s, int len)\r
-  {\r
-    return print(s, len, true);\r
-  }\r
-\r
-  public static String print(SequenceI[] s, int len, boolean gaps)\r
+  public String print(SequenceI[] s)\r
   {\r
+    boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
+    int len = 72;\r
     StringBuffer out = new StringBuffer();\r
     int i = 0;\r
+    ModellerDescription md;\r
 \r
     while ( (i < s.length) && (s[i] != null))\r
     {\r
-      String seq = "";\r
+      String seq = s[i].getSequenceAsString();\r
+      seq = seq + "*";\r
 \r
-      if (gaps)\r
+      if (is_NA)\r
       {\r
-        seq = s[i].getSequence() + "*";\r
+        // modeller doesn't really do nucleotides, so we don't do anything fancy\r
+        // Official tags area as follows, for now we'll use P1 and DL\r
+        // Protein (complete) P1\r
+        // Protein (fragment) F1\r
+        // DNA (linear) Dl\r
+        // DNA (circular) DC\r
+        // RNA (linear) RL\r
+        // RNA (circular) RC\r
+        // tRNA N3\r
+        // other functional RNA N1\r
+\r
+        out.append(">N1;" + s[i].getName() + "\n");\r
+        if (s[i].getDescription() == null)\r
+        {\r
+          out.append(s[i].getName() + " " +\r
+                     (s[i].getEnd() - s[i].getStart() + 1));\r
+          out.append(is_NA ? " bases\n" : " residues\n");\r
+        }\r
+        else\r
+        {\r
+          out.append(s[i].getDescription() + "\n");\r
+        }\r
       }\r
       else\r
       {\r
-        seq = AlignSeq.extractGaps(s[i].getSequence(), "-");\r
-        seq = AlignSeq.extractGaps(seq, ".");\r
-        seq = AlignSeq.extractGaps(seq, " ");\r
-        seq = seq + "*";\r
-      }\r
-\r
-      out.append(">P1;" + s[i].getName() + "/" + s[i].getStart() + "-" +\r
-                 s[i].getEnd() + "\n");\r
-      out.append(" Dummy title\n");\r
 \r
+        if (useModellerOutput)\r
+        {\r
+          out.append(">P1;" + s[i].getName() + "\n");\r
+          md = new ModellerDescription(s[i]);\r
+          out.append(md.getDescriptionLine() + "\n");\r
+        }\r
+        else\r
+        {\r
+          out.append(">P1;" + printId(s[i]) + "\n");\r
+          if (s[i].getDescription() != null)\r
+          {\r
+            out.append(s[i].getDescription() + "\n");\r
+          }\r
+          else\r
+          {\r
+            out.append(s[i].getName() + " "\r
+                       + (s[i].getEnd() - s[i].getStart() + 1)\r
+                       + " residues\n");\r
+          }\r
+        }\r
+      }\r
       int nochunks = (seq.length() / len) + 1;\r
 \r
       for (int j = 0; j < nochunks; j++)\r
@@ -184,9 +186,4 @@ public class PIRFile
     return out.toString();\r
   }\r
 \r
-  public static void main(String[] args)\r
-  {\r
-    String inStr = ">P1;LCAT_MOUSE_90.35\nMGLPGSPWQRVLLLLGLLLPPATPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNRLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDFNLFLPLGVDCWIDNTRIVYNHSSGRVSNAPGVQIRVPGFGKTESVEYVDDNKLAGY\n\n>LCAT_PAPAN_95.78\nMGPPGSPWQWVPLLLGLLLPPAAPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNQLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDLNMFLPLGVDCWIDNTRVVYNRSSGLVSNAPGVQIRVPGFGKTYSVEYLDSSKLAGY\nLHTLVQNLVNNGYVRDETVRAAPYDWRLEPGQQEEYYHKLAGLVEEMHAAYGKPVFLIGHSLGCLHLLYFLL\n";\r
-    PIRFile fa = new PIRFile(inStr);\r
-  }\r
 }\r