d4b818b4d64c554c8433b687e811fc1168985c00
[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 \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 \r
88           newSeq.setSequence(sequence.toString());\r
89           seqs.addElement(newSeq);\r
90         }\r
91       }\r
92     }\r
93     catch (Exception ex)\r
94     {\r
95       ex.printStackTrace();\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 \r
111     while ( (i < s.length) && (s[i] != null))\r
112     {\r
113       String seq = s[i].getSequence();\r
114       seq = seq + "*";\r
115 \r
116       out.append(">P1;" + printId(s[i]) + "\n");\r
117 \r
118       if(s[i].getDescription()!=null)\r
119         out.append(s[i].getDescription()+"\n");\r
120       else\r
121       {\r
122         out.append(s[i].getName()+" "+ (s[i].getEnd() - s[i].getStart() + 1));\r
123         out.append( is_NA ? " bases\n" : " residues\n");\r
124       }\r
125       int nochunks = (seq.length() / len) + 1;\r
126 \r
127       for (int j = 0; j < nochunks; j++)\r
128       {\r
129         int start = j * len;\r
130         int end = start + len;\r
131 \r
132         if (end < seq.length())\r
133         {\r
134           out.append(seq.substring(start, end) + "\n");\r
135         }\r
136         else if (start < seq.length())\r
137         {\r
138           out.append(seq.substring(start) + "\n");\r
139         }\r
140       }\r
141 \r
142       i++;\r
143     }\r
144 \r
145     return out.toString();\r
146   }\r
147 \r
148 }\r