Jalview Imported Sources
[jalview.git] / src / jalview / io / PIRFile.java
1 /* Jalview - a java multiple alignment editor\r
2  * Copyright (C) 1998  Michele Clamp\r
3  *\r
4  * This program is free software; you can redistribute it and/or\r
5  * modify it under the terms of the GNU General Public License\r
6  * as published by the Free Software Foundation; either version 2\r
7  * of the License, or (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program; if not, write to the Free Software\r
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
17  */\r
18 package jalview.io;\r
19 \r
20 import jalview.datamodel.*;\r
21 import jalview.analysis.*;\r
22 \r
23 import java.io.*;\r
24 import java.util.*;\r
25 \r
26 public class PIRFile extends AlignFile {\r
27 \r
28   Vector words = new Vector();  //Stores the words in a line after splitting\r
29 \r
30   public PIRFile()\r
31   {}\r
32 \r
33   public PIRFile(String inStr) {\r
34     super(inStr);\r
35   }\r
36 \r
37   public PIRFile(String inFile, String type) throws IOException {\r
38     super(inFile,type);\r
39   }\r
40 \r
41   public void parse() {\r
42     try{\r
43       String id, start, end;\r
44       StringBuffer sequence;\r
45       String line = null;\r
46       while( (line = nextLine())!=null)\r
47       {\r
48         try{\r
49           id = line.substring(line.indexOf(";") + 1, line.indexOf("/"));\r
50           line = line.substring(line.indexOf("/") + 1);\r
51           start = line.substring(0, line.indexOf("-"));\r
52           end = line.substring(line.indexOf("-") + 1);\r
53         }catch(Exception ex)\r
54         {  id="No id"; start="0"; end="0"; }\r
55 \r
56         sequence = new StringBuffer();\r
57 \r
58         line = nextLine(); // this is the title line\r
59 \r
60         boolean starFound = false;\r
61         do\r
62         {\r
63           line = nextLine();\r
64           sequence.append( line );\r
65           if(line.indexOf("*")>-1)\r
66             starFound = true;\r
67 \r
68         }while(!starFound);\r
69 \r
70         sequence.setLength( sequence.length()-1);\r
71 \r
72        Sequence newSeq = new Sequence(id,\r
73                                sequence.toString(),\r
74                                Integer.parseInt(start),\r
75                                Integer.parseInt(end));\r
76          seqs.addElement(newSeq);\r
77       }\r
78 \r
79     }\r
80     catch(Exception ex){ex.printStackTrace();}\r
81   }\r
82 \r
83   public String print() {\r
84     return print(getSeqsAsArray());\r
85   }\r
86   public static String print(SequenceI[] s) {\r
87     return print(s,72,true);\r
88   }\r
89   public static String print(SequenceI[] s, int len) {\r
90     return print(s,len,true);\r
91   }\r
92   public static String print(SequenceI[] s, int len,boolean gaps) {\r
93     StringBuffer out = new StringBuffer();\r
94     int i = 0;\r
95 \r
96     while (i < s.length && s[i] != null) {\r
97       String seq = "";\r
98       if (gaps) {\r
99         seq = s[i].getSequence() + "*";\r
100       } else {\r
101         seq = AlignSeq.extractGaps(s[i].getSequence(),"-");\r
102         seq = AlignSeq.extractGaps(seq,".");\r
103         seq = AlignSeq.extractGaps(seq," ");\r
104         seq = seq + "*";\r
105       }\r
106 \r
107       out.append(">P1;" + s[i].getName() + "/" + s[i].getStart()+ "-" + s[i].getEnd() + "\n");\r
108       out.append(" Dummy title\n");\r
109       int nochunks = seq.length() / len + 1;\r
110 \r
111       for (int j = 0; j < nochunks; j++) {\r
112         int start = j*len;\r
113         int end = start + len;\r
114 \r
115         if (end < seq.length()) {\r
116           out.append(seq.substring(start,end) + "\n");\r
117         } else if (start < seq.length()) {\r
118           out.append(seq.substring(start) + "\n");\r
119         }\r
120       }\r
121       i++;\r
122     }\r
123     return out.toString();\r
124   }\r
125 \r
126   public static void main(String[] args) {\r
127     String inStr = ">P1;LCAT_MOUSE_90.35\nMGLPGSPWQRVLLLLGLLLPPATPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNRLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDFNLFLPLGVDCWIDNTRIVYNHSSGRVSNAPGVQIRVPGFGKTESVEYVDDNKLAGY\n\n>LCAT_PAPAN_95.78\nMGPPGSPWQWVPLLLGLLLPPAAPFWLLNVLFPPHTTPKAELSNHTRPVILVPGCLGNQLEAKLDKPDVVNW\nMCYRKTEDFFTIWLDLNMFLPLGVDCWIDNTRVVYNRSSGLVSNAPGVQIRVPGFGKTYSVEYLDSSKLAGY\nLHTLVQNLVNNGYVRDETVRAAPYDWRLEPGQQEEYYHKLAGLVEEMHAAYGKPVFLIGHSLGCLHLLYFLL\n";\r
128     PIRFile fa = new PIRFile(inStr);\r
129   }\r
130 }\r
131 \r
132 \r
133 \r