PIR file IO now preserves and parses Modeller style colon-separated fields
[jalview.git] / src / jalview / io / PIRFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 import java.util.*;\r
23 \r
24 import jalview.analysis.*;\r
25 import jalview.datamodel.*;\r
26 \r
27 public class PIRFile\r
28     extends AlignFile\r
29 {\r
30   Vector words = new Vector(); //Stores the words in a line after splitting\r
31 \r
32   public PIRFile()\r
33   {\r
34   }\r
35 \r
36   public PIRFile(String inStr)\r
37   {\r
38     super(inStr);\r
39   }\r
40 \r
41   public PIRFile(String inFile, String type)\r
42       throws IOException\r
43   {\r
44     super(inFile, type);\r
45   }\r
46 \r
47   public void parse()\r
48   {\r
49     try\r
50     {\r
51       StringBuffer sequence;\r
52       String line = null;\r
53 \r
54       while ( (line = nextLine()) != null)\r
55       {\r
56         if (line.length() == 0)\r
57         {\r
58           //System.out.println("blank line");\r
59           continue;\r
60         }\r
61         if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)\r
62         {\r
63           continue;\r
64         }\r
65         Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));\r
66 \r
67         sequence = new StringBuffer();\r
68 \r
69         newSeq.setDescription(nextLine()); // this is the title line\r
70 \r
71         boolean starFound = false;\r
72 \r
73         while(!starFound)\r
74         {\r
75           line = nextLine();\r
76           sequence.append(line);\r
77 \r
78           if (line == null)\r
79             break;\r
80 \r
81           if (line.indexOf("*") > -1)\r
82           {\r
83             starFound = true;\r
84           }\r
85         }\r
86 \r
87         if (sequence.length() > 0)\r
88         {\r
89           sequence.setLength(sequence.length() - 1);\r
90           newSeq.setSequence(sequence.toString());\r
91           seqs.addElement(newSeq);\r
92           ModellerDescription md = new ModellerDescription(newSeq.\r
93               getDescription());\r
94           md.updateSequenceI(newSeq);\r
95         }\r
96       }\r
97     }\r
98     catch (Exception ex)\r
99     {\r
100       ex.printStackTrace();\r
101     }\r
102   }\r
103 \r
104   public String print()\r
105   {\r
106     return print(getSeqsAsArray());\r
107   }\r
108 \r
109   public String print(SequenceI[] s)\r
110   {\r
111     boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
112     int len = 72;\r
113     StringBuffer out = new StringBuffer();\r
114     int i = 0;\r
115 \r
116     while ( (i < s.length) && (s[i] != null))\r
117     {\r
118       String seq = s[i].getSequence();\r
119       seq = seq + "*";\r
120 \r
121       if (is_NA)\r
122       {\r
123         // modeller doesn't really do nucleotides, so we don't do anything fancy\r
124         // Nucleotide sequence tags should have a >DL; prefix\r
125         out.append(">P1;" + s[i].getName() + "\n"); // JBPNote Should change >P to >N\r
126         if (s[i].getDescription() == null)\r
127         {\r
128           out.append(s[i].getName() + " " +\r
129                      (s[i].getEnd() - s[i].getStart() + 1));\r
130           out.append(is_NA ? " bases\n" : " residues\n");\r
131         }\r
132         else\r
133         {\r
134           out.append(s[i].getDescription()+"\n");\r
135         }\r
136       }\r
137       else\r
138       {\r
139         out.append(">P1;" + s[i].getName() + "\n");\r
140         ModellerDescription md = new ModellerDescription(s[i]);\r
141         out.append(md.getDescriptionLine() + "\n");\r
142       }\r
143       int nochunks = (seq.length() / len) + 1;\r
144 \r
145       for (int j = 0; j < nochunks; j++)\r
146       {\r
147         int start = j * len;\r
148         int end = start + len;\r
149 \r
150         if (end < seq.length())\r
151         {\r
152           out.append(seq.substring(start, end) + "\n");\r
153         }\r
154         else if (start < seq.length())\r
155         {\r
156           out.append(seq.substring(start) + "\n");\r
157         }\r
158       }\r
159 \r
160       i++;\r
161     }\r
162 \r
163     return out.toString();\r
164   }\r
165 \r
166 }\r