42334e3cc60b169995f1184a5bb5afc46820266a
[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 jalview.analysis.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 import java.io.*;\r
26 \r
27 import java.util.*;\r
28 \r
29 \r
30 public class PIRFile extends AlignFile {\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     public PIRFile(String inStr) {\r
37         super(inStr);\r
38     }\r
39 \r
40     public PIRFile(String inFile, String type) throws IOException {\r
41         super(inFile, type);\r
42     }\r
43 \r
44     public void parse() {\r
45         try {\r
46             String id;\r
47             String start;\r
48             String end;\r
49             StringBuffer sequence;\r
50             String line = null;\r
51 \r
52             while ((line = nextLine()) != null) {\r
53                 try {\r
54                     id = line.substring(line.indexOf(";") + 1, line.indexOf("/"));\r
55                     line = line.substring(line.indexOf("/") + 1);\r
56                     start = line.substring(0, line.indexOf("-"));\r
57                     end = line.substring(line.indexOf("-") + 1);\r
58                 } catch (Exception ex) {\r
59                     id = "No id";\r
60                     start = "0";\r
61                     end = "0";\r
62                 }\r
63 \r
64                 sequence = new StringBuffer();\r
65 \r
66                 line = nextLine(); // this is the title line\r
67 \r
68                 boolean starFound = false;\r
69 \r
70                 do {\r
71                     line = nextLine();\r
72                     sequence.append(line);\r
73 \r
74                     if (line.indexOf("*") > -1) {\r
75                         starFound = true;\r
76                     }\r
77                 } while (!starFound);\r
78 \r
79                 sequence.setLength(sequence.length() - 1);\r
80 \r
81                 Sequence newSeq = new Sequence(id, sequence.toString(),\r
82                         Integer.parseInt(start), Integer.parseInt(end));\r
83                 seqs.addElement(newSeq);\r
84             }\r
85         } catch (Exception ex) {\r
86             ex.printStackTrace();\r
87         }\r
88     }\r
89 \r
90     public String print() {\r
91         return print(getSeqsAsArray());\r
92     }\r
93 \r
94     public static String print(SequenceI[] s) {\r
95         return print(s, 72, true);\r
96     }\r
97 \r
98     public static String print(SequenceI[] s, int len) {\r
99         return print(s, len, true);\r
100     }\r
101 \r
102     public static String print(SequenceI[] s, int len, boolean gaps) {\r
103         StringBuffer out = new StringBuffer();\r
104         int i = 0;\r
105 \r
106         while ((i < s.length) && (s[i] != null)) {\r
107             String seq = "";\r
108 \r
109             if (gaps) {\r
110                 seq = s[i].getSequence() + "*";\r
111             } else {\r
112                 seq = AlignSeq.extractGaps(s[i].getSequence(), "-");\r
113                 seq = AlignSeq.extractGaps(seq, ".");\r
114                 seq = AlignSeq.extractGaps(seq, " ");\r
115                 seq = seq + "*";\r
116             }\r
117 \r
118             out.append(">P1;" + s[i].getName() + "/" + s[i].getStart() + "-" +\r
119                 s[i].getEnd() + "\n");\r
120             out.append(" Dummy title\n");\r
121 \r
122             int nochunks = (seq.length() / len) + 1;\r
123 \r
124             for (int j = 0; j < nochunks; j++) {\r
125                 int start = j * len;\r
126                 int end = start + len;\r
127 \r
128                 if (end < seq.length()) {\r
129                     out.append(seq.substring(start, end) + "\n");\r
130                 } else if (start < seq.length()) {\r
131                     out.append(seq.substring(start) + "\n");\r
132                 }\r
133             }\r
134 \r
135             i++;\r
136         }\r
137 \r
138         return out.toString();\r
139     }\r
140 \r
141     public static void main(String[] args) {\r
142         String inStr = ">P1;LCAT_MOUSE_90.35\nMGLPGSPWQRVLLLLGLLLPPATPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNRLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDFNLFLPLGVDCWIDNTRIVYNHSSGRVSNAPGVQIRVPGFGKTESVEYVDDNKLAGY\n\n>LCAT_PAPAN_95.78\nMGPPGSPWQWVPLLLGLLLPPAAPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNQLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDLNMFLPLGVDCWIDNTRVVYNRSSGLVSNAPGVQIRVPGFGKTYSVEYLDSSKLAGY\nLHTLVQNLVNNGYVRDETVRAAPYDWRLEPGQQEEYYHKLAGLVEEMHAAYGKPVFLIGHSLGCLHLLYFLL\n";\r
143         PIRFile fa = new PIRFile(inStr);\r
144     }\r
145 }\r