a15a4cbf0b29cbeae9f2ff9edb3665048ddcddf0
[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       String id;\r
52       String start;\r
53       String end;\r
54       StringBuffer sequence;\r
55       String line = null;\r
56 \r
57       while ( (line = nextLine()) != null)\r
58       {\r
59         try\r
60         {\r
61           id = line.substring(line.indexOf(";") + 1, line.indexOf("/"));\r
62           line = line.substring(line.indexOf("/") + 1);\r
63           start = line.substring(0, line.indexOf("-"));\r
64           end = line.substring(line.indexOf("-") + 1);\r
65         }\r
66         catch (Exception ex)\r
67         {\r
68           id = "No id";\r
69           start = "0";\r
70           end = "0";\r
71         }\r
72 \r
73         sequence = new StringBuffer();\r
74 \r
75         line = nextLine(); // this is the title line\r
76 \r
77         boolean starFound = false;\r
78 \r
79         do\r
80         {\r
81           line = nextLine();\r
82           sequence.append(line);\r
83 \r
84           if (line.indexOf("*") > -1)\r
85           {\r
86             starFound = true;\r
87           }\r
88         }\r
89         while (!starFound);\r
90 \r
91         sequence.setLength(sequence.length() - 1);\r
92 \r
93         Sequence newSeq = new Sequence(id, sequence.toString(),\r
94                                        Integer.parseInt(start),\r
95                                        Integer.parseInt(end));\r
96         seqs.addElement(newSeq);\r
97       }\r
98     }\r
99     catch (Exception ex)\r
100     {\r
101       ex.printStackTrace();\r
102     }\r
103   }\r
104 \r
105   public String print()\r
106   {\r
107     return print(getSeqsAsArray());\r
108   }\r
109 \r
110   public static String print(SequenceI[] s)\r
111   {\r
112     return print(s, 72, true);\r
113   }\r
114 \r
115   public static String print(SequenceI[] s, int len)\r
116   {\r
117     return print(s, len, true);\r
118   }\r
119 \r
120   public static String print(SequenceI[] s, int len, boolean gaps)\r
121   {\r
122     StringBuffer out = new StringBuffer();\r
123     int i = 0;\r
124 \r
125     while ( (i < s.length) && (s[i] != null))\r
126     {\r
127       String seq = "";\r
128 \r
129       if (gaps)\r
130       {\r
131         seq = s[i].getSequence() + "*";\r
132       }\r
133       else\r
134       {\r
135         seq = AlignSeq.extractGaps(s[i].getSequence(), "-");\r
136         seq = AlignSeq.extractGaps(seq, ".");\r
137         seq = AlignSeq.extractGaps(seq, " ");\r
138         seq = seq + "*";\r
139       }\r
140 \r
141       out.append(">P1;" + s[i].getName() + "/" + s[i].getStart() + "-" +\r
142                  s[i].getEnd() + "\n");\r
143       out.append(" Dummy title\n");\r
144 \r
145       int nochunks = (seq.length() / len) + 1;\r
146 \r
147       for (int j = 0; j < nochunks; j++)\r
148       {\r
149         int start = j * len;\r
150         int end = start + len;\r
151 \r
152         if (end < seq.length())\r
153         {\r
154           out.append(seq.substring(start, end) + "\n");\r
155         }\r
156         else if (start < seq.length())\r
157         {\r
158           out.append(seq.substring(start) + "\n");\r
159         }\r
160       }\r
161 \r
162       i++;\r
163     }\r
164 \r
165     return out.toString();\r
166   }\r
167 \r
168   public static void main(String[] args)\r
169   {\r
170     String inStr = ">P1;LCAT_MOUSE_90.35\nMGLPGSPWQRVLLLLGLLLPPATPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNRLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDFNLFLPLGVDCWIDNTRIVYNHSSGRVSNAPGVQIRVPGFGKTESVEYVDDNKLAGY\n\n>LCAT_PAPAN_95.78\nMGPPGSPWQWVPLLLGLLLPPAAPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNQLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDLNMFLPLGVDCWIDNTRVVYNRSSGLVSNAPGVQIRVPGFGKTYSVEYLDSSKLAGY\nLHTLVQNLVNNGYVRDETVRAAPYDWRLEPGQQEEYYHKLAGLVEEMHAAYGKPVFLIGHSLGCLHLLYFLL\n";\r
171     PIRFile fa = new PIRFile(inStr);\r
172   }\r
173 }\r