4c07ac934f44918417a49dd354bf96e73137bd40
[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.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() throws IOException\r
44   {\r
45       StringBuffer sequence;\r
46       String line = null;\r
47       ModellerDescription md;\r
48 \r
49       while ( (line = nextLine()) != null)\r
50       {\r
51         if (line.length() == 0)\r
52         {\r
53           //System.out.println("blank line");\r
54           continue;\r
55         }\r
56         if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)\r
57         {\r
58           continue;\r
59         }\r
60         Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));\r
61 \r
62         sequence = new StringBuffer();\r
63 \r
64         newSeq.setDescription(nextLine()); // this is the title line\r
65 \r
66         boolean starFound = false;\r
67 \r
68         while(!starFound)\r
69         {\r
70           line = nextLine();\r
71           sequence.append(line);\r
72 \r
73           if (line == null)\r
74             break;\r
75 \r
76           if (line.indexOf("*") > -1)\r
77           {\r
78             starFound = true;\r
79           }\r
80         }\r
81 \r
82         if (sequence.length() > 0)\r
83         {\r
84           sequence.setLength(sequence.length() - 1);\r
85           newSeq.setSequence(sequence.toString());\r
86           if (!isValidProteinSequence(newSeq.getSequence()))\r
87           {\r
88             throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
89                                   +" : "+ newSeq.getName()\r
90                                   +" : "+invalidCharacter);\r
91           }\r
92 \r
93           seqs.addElement(newSeq);\r
94 \r
95           md = new ModellerDescription(newSeq.\r
96                 getDescription());\r
97           md.updateSequenceI(newSeq);\r
98         }\r
99       }\r
100   }\r
101 \r
102   public String print()\r
103   {\r
104     return print(getSeqsAsArray());\r
105   }\r
106 \r
107   public String print(SequenceI[] s)\r
108   {\r
109     boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
110     int len = 72;\r
111     StringBuffer out = new StringBuffer();\r
112     int i = 0;\r
113     ModellerDescription md;\r
114 \r
115     while ( (i < s.length) && (s[i] != null))\r
116     {\r
117       String seq = s[i].getSequence();\r
118       seq = seq + "*";\r
119 \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           // Official tags area as follows, for now we'll use P1 and DL\r
125           // Protein (complete) P1\r
126           // Protein (fragment) F1\r
127           // DNA (linear) Dl\r
128           // DNA (circular) DC\r
129           // RNA (linear) RL\r
130           // RNA (circular) RC\r
131           // tRNA N3\r
132           // other functional RNA N1\r
133 \r
134         out.append(">N1;" + s[i].getName() + "\n");\r
135         if (s[i].getDescription() == null)\r
136         {\r
137           out.append(s[i].getName() + " " +\r
138                      (s[i].getEnd() - s[i].getStart() + 1));\r
139           out.append(is_NA ? " bases\n" : " residues\n");\r
140         }\r
141         else\r
142         {\r
143           out.append(s[i].getDescription()+"\n");\r
144         }\r
145       }\r
146       else\r
147       {\r
148 \r
149        if(useModellerOutput)\r
150        {\r
151          out.append(">P1;" + s[i].getName() + "\n");\r
152          md = new ModellerDescription(s[i]);\r
153          out.append(md.getDescriptionLine() + "\n");\r
154        }\r
155        else\r
156        {\r
157          out.append(">P1;" + printId(s[i]) + "\n");\r
158          if (s[i].getDescription() != null)\r
159            out.append(s[i].getDescription() + "\n");\r
160          else\r
161            out.append(s[i].getName() + " "\r
162                       + (s[i].getEnd() - s[i].getStart() + 1)\r
163                       + " residues\n");\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