Formatting
[jalview.git] / src / jalview / io / PIRFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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.datamodel.*;\r
25 \r
26 public class PIRFile\r
27     extends AlignFile\r
28 {\r
29   public static boolean useModellerOutput = false;\r
30 \r
31   Vector words = new Vector(); //Stores the words in a line after splitting\r
32 \r
33   public PIRFile()\r
34   {\r
35   }\r
36 \r
37   public PIRFile(String inFile, String type)\r
38       throws IOException\r
39   {\r
40     super(inFile, type);\r
41   }\r
42 \r
43   public void parse()\r
44       throws IOException\r
45   {\r
46     StringBuffer sequence;\r
47     String line = null;\r
48     ModellerDescription md;\r
49 \r
50     while ( (line = nextLine()) != null)\r
51     {\r
52       if (line.length() == 0)\r
53       {\r
54         //System.out.println("blank line");\r
55         continue;\r
56       }\r
57       if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)\r
58       {\r
59         continue;\r
60       }\r
61       Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));\r
62 \r
63       sequence = new StringBuffer();\r
64 \r
65       newSeq.setDescription(nextLine()); // this is the title line\r
66 \r
67       boolean starFound = false;\r
68 \r
69       while (!starFound)\r
70       {\r
71         line = nextLine();\r
72         sequence.append(line);\r
73 \r
74         if (line == null)\r
75         {\r
76           break;\r
77         }\r
78 \r
79         if (line.indexOf("*") > -1)\r
80         {\r
81           starFound = true;\r
82         }\r
83       }\r
84 \r
85       if (sequence.length() > 0)\r
86       {\r
87         sequence.setLength(sequence.length() - 1);\r
88         newSeq.setSequence(sequence.toString());\r
89 \r
90         seqs.addElement(newSeq);\r
91 \r
92         md = new ModellerDescription(newSeq.\r
93                                      getDescription());\r
94         md.updateSequenceI(newSeq);\r
95       }\r
96     }\r
97   }\r
98 \r
99   public String print()\r
100   {\r
101     return print(getSeqsAsArray());\r
102   }\r
103 \r
104   public String print(SequenceI[] s)\r
105   {\r
106     boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
107     int len = 72;\r
108     StringBuffer out = new StringBuffer();\r
109     int i = 0;\r
110     ModellerDescription md;\r
111 \r
112     while ( (i < s.length) && (s[i] != null))\r
113     {\r
114       String seq = s[i].getSequenceAsString();\r
115       seq = seq + "*";\r
116 \r
117       if (is_NA)\r
118       {\r
119         // modeller doesn't really do nucleotides, so we don't do anything fancy\r
120         // Official tags area as follows, for now we'll use P1 and DL\r
121         // Protein (complete) P1\r
122         // Protein (fragment) F1\r
123         // DNA (linear) Dl\r
124         // DNA (circular) DC\r
125         // RNA (linear) RL\r
126         // RNA (circular) RC\r
127         // tRNA N3\r
128         // other functional RNA N1\r
129 \r
130         out.append(">N1;" + s[i].getName() + "\n");\r
131         if (s[i].getDescription() == null)\r
132         {\r
133           out.append(s[i].getName() + " " +\r
134                      (s[i].getEnd() - s[i].getStart() + 1));\r
135           out.append(is_NA ? " bases\n" : " residues\n");\r
136         }\r
137         else\r
138         {\r
139           out.append(s[i].getDescription() + "\n");\r
140         }\r
141       }\r
142       else\r
143       {\r
144 \r
145         if (useModellerOutput)\r
146         {\r
147           out.append(">P1;" + s[i].getName() + "\n");\r
148           md = new ModellerDescription(s[i]);\r
149           out.append(md.getDescriptionLine() + "\n");\r
150         }\r
151         else\r
152         {\r
153           out.append(">P1;" + printId(s[i]) + "\n");\r
154           if (s[i].getDescription() != null)\r
155           {\r
156             out.append(s[i].getDescription() + "\n");\r
157           }\r
158           else\r
159           {\r
160             out.append(s[i].getName() + " "\r
161                        + (s[i].getEnd() - s[i].getStart() + 1)\r
162                        + " residues\n");\r
163           }\r
164         }\r
165       }\r
166       int nochunks = (seq.length() / len) + 1;\r
167 \r
168       for (int j = 0; j < nochunks; j++)\r
169       {\r
170         int start = j * len;\r
171         int end = start + len;\r
172 \r
173         if (end < seq.length())\r
174         {\r
175           out.append(seq.substring(start, end) + "\n");\r
176         }\r
177         else if (start < seq.length())\r
178         {\r
179           out.append(seq.substring(start) + "\n");\r
180         }\r
181       }\r
182 \r
183       i++;\r
184     }\r
185 \r
186     return out.toString();\r
187   }\r
188 \r
189 }\r