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