added unofficial support for concise output parsing from the JNet server to extract...
[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 inStr)\r
38   {\r
39     super(inStr);\r
40   }\r
41 \r
42   public PIRFile(String inFile, String type)\r
43       throws IOException\r
44   {\r
45     super(inFile, type);\r
46   }\r
47 \r
48   public void parse() throws IOException\r
49   {\r
50       StringBuffer sequence;\r
51       String line = null;\r
52       ModellerDescription md;\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           if (!isValidProteinSequence(newSeq.getSequence()))\r
92           {\r
93             throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
94                                   +" : "+ newSeq.getName()\r
95                                   +" : "+invalidCharacter);\r
96           }\r
97 \r
98           seqs.addElement(newSeq);\r
99 \r
100           md = new ModellerDescription(newSeq.\r
101                 getDescription());\r
102           md.updateSequenceI(newSeq);\r
103         }\r
104       }\r
105   }\r
106 \r
107   public String print()\r
108   {\r
109     return print(getSeqsAsArray());\r
110   }\r
111 \r
112   public String print(SequenceI[] s)\r
113   {\r
114     boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
115     int len = 72;\r
116     StringBuffer out = new StringBuffer();\r
117     int i = 0;\r
118     ModellerDescription md;\r
119 \r
120     while ( (i < s.length) && (s[i] != null))\r
121     {\r
122       String seq = s[i].getSequence();\r
123       seq = seq + "*";\r
124 \r
125 \r
126       if (is_NA)\r
127       {\r
128           // modeller doesn't really do nucleotides, so we don't do anything fancy\r
129           // Official tags area as follows, for now we'll use P1 and DL\r
130           // Protein (complete) P1\r
131           // Protein (fragment) F1\r
132           // DNA (linear) Dl\r
133           // DNA (circular) DC\r
134           // RNA (linear) RL\r
135           // RNA (circular) RC\r
136           // tRNA N3\r
137           // other functional RNA N1\r
138 \r
139         out.append(">N1;" + s[i].getName() + "\n");\r
140         if (s[i].getDescription() == null)\r
141         {\r
142           out.append(s[i].getName() + " " +\r
143                      (s[i].getEnd() - s[i].getStart() + 1));\r
144           out.append(is_NA ? " bases\n" : " residues\n");\r
145         }\r
146         else\r
147         {\r
148           out.append(s[i].getDescription()+"\n");\r
149         }\r
150       }\r
151       else\r
152       {\r
153 \r
154        if(useModellerOutput)\r
155        {\r
156          out.append(">P1;" + s[i].getName() + "\n");\r
157          md = new ModellerDescription(s[i]);\r
158          out.append(md.getDescriptionLine() + "\n");\r
159        }\r
160        else\r
161        {\r
162          out.append(">P1;" + printId(s[i]) + "\n");\r
163          if (s[i].getDescription() != null)\r
164            out.append(s[i].getDescription() + "\n");\r
165          else\r
166            out.append(s[i].getName() + " "\r
167                       + (s[i].getEnd() - s[i].getStart() + 1)\r
168                       + " residues\n");\r
169        }\r
170       }\r
171       int nochunks = (seq.length() / len) + 1;\r
172 \r
173       for (int j = 0; j < nochunks; j++)\r
174       {\r
175         int start = j * len;\r
176         int end = start + len;\r
177 \r
178         if (end < seq.length())\r
179         {\r
180           out.append(seq.substring(start, end) + "\n");\r
181         }\r
182         else if (start < seq.length())\r
183         {\r
184           out.append(seq.substring(start) + "\n");\r
185         }\r
186       }\r
187 \r
188       i++;\r
189     }\r
190 \r
191     return out.toString();\r
192   }\r
193 \r
194 }\r