P1 changed to N1
[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   Vector words = new Vector(); //Stores the words in a line after splitting\r
30 \r
31   public PIRFile()\r
32   {\r
33   }\r
34 \r
35   public PIRFile(String inStr)\r
36   {\r
37     super(inStr);\r
38   }\r
39 \r
40   public PIRFile(String inFile, String type)\r
41       throws IOException\r
42   {\r
43     super(inFile, type);\r
44   }\r
45 \r
46   public void parse() throws IOException\r
47   {\r
48       StringBuffer sequence;\r
49       String line = null;\r
50 \r
51       while ( (line = nextLine()) != null)\r
52       {\r
53         if (line.length() == 0)\r
54         {\r
55           //System.out.println("blank line");\r
56           continue;\r
57         }\r
58         if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)\r
59         {\r
60           continue;\r
61         }\r
62         Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));\r
63 \r
64         sequence = new StringBuffer();\r
65 \r
66         newSeq.setDescription(nextLine()); // this is the title line\r
67 \r
68         boolean starFound = false;\r
69 \r
70         while(!starFound)\r
71         {\r
72           line = nextLine();\r
73           sequence.append(line);\r
74 \r
75           if (line == null)\r
76             break;\r
77 \r
78           if (line.indexOf("*") > -1)\r
79           {\r
80             starFound = true;\r
81           }\r
82         }\r
83 \r
84         if (sequence.length() > 0)\r
85         {\r
86           sequence.setLength(sequence.length() - 1);\r
87           newSeq.setSequence(sequence.toString());\r
88           if (!isValidProteinSequence(newSeq.getSequence()))\r
89           {\r
90             throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
91                                   +" : "+ newSeq.getName()\r
92                                   +" : "+invalidCharacter);\r
93           }\r
94 \r
95           seqs.addElement(newSeq);\r
96           ModellerDescription md = new ModellerDescription(newSeq.\r
97               getDescription());\r
98           md.updateSequenceI(newSeq);\r
99         }\r
100       }\r
101   }\r
102 \r
103   public String print()\r
104   {\r
105     return print(getSeqsAsArray());\r
106   }\r
107 \r
108   public String print(SequenceI[] s)\r
109   {\r
110     boolean is_NA = jalview.util.Comparison.isNucleotide(s);\r
111     int len = 72;\r
112     StringBuffer out = new StringBuffer();\r
113     int i = 0;\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       if (is_NA)\r
121       {\r
122         // modeller doesn't really do nucleotides, so we don't do anything fancy\r
123         // Nucleotide sequence tags should have a >DL; prefix\r
124         out.append(">N1;" + s[i].getName() + "\n"); // JBPNote Should change >P to >N\r
125         if (s[i].getDescription() == null)\r
126         {\r
127           out.append(s[i].getName() + " " +\r
128                      (s[i].getEnd() - s[i].getStart() + 1));\r
129           out.append(is_NA ? " bases\n" : " residues\n");\r
130         }\r
131         else\r
132         {\r
133           out.append(s[i].getDescription()+"\n");\r
134         }\r
135       }\r
136       else\r
137       {\r
138         out.append(">P1;" + s[i].getName() + "\n");\r
139         ModellerDescription md = new ModellerDescription(s[i]);\r
140         out.append(md.getDescriptionLine() + "\n");\r
141       }\r
142       int nochunks = (seq.length() / len) + 1;\r
143 \r
144       for (int j = 0; j < nochunks; j++)\r
145       {\r
146         int start = j * len;\r
147         int end = start + len;\r
148 \r
149         if (end < seq.length())\r
150         {\r
151           out.append(seq.substring(start, end) + "\n");\r
152         }\r
153         else if (start < seq.length())\r
154         {\r
155           out.append(seq.substring(start) + "\n");\r
156         }\r
157       }\r
158 \r
159       i++;\r
160     }\r
161 \r
162     return out.toString();\r
163   }\r
164 \r
165 }\r