7961810c33c927c360700a62d7953173064d1167
[jalview.git] / src / jalview / io / BLCFile.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 \r
24 import java.io.*;\r
25 import java.util.*;\r
26 \r
27 public class BLCFile extends AlignFile {\r
28 \r
29   Vector titles;\r
30 \r
31   public BLCFile()\r
32   {}\r
33 \r
34   public BLCFile(String inStr) {\r
35     super(inStr);\r
36   }\r
37 \r
38   public void initData() {\r
39     super.initData();\r
40     titles = new Vector();\r
41   }\r
42 \r
43   public BLCFile(String inFile, String type) throws IOException {\r
44     super(inFile,type);\r
45   }\r
46 \r
47 \r
48   public void parse()\r
49   {\r
50     boolean idsFound=false;\r
51     Vector ids = new Vector();\r
52     StringBuffer seqstrings [];\r
53     Vector starts = new Vector();\r
54     Vector ends = new Vector();\r
55 \r
56    String line=null;\r
57    try{\r
58      do{\r
59       line = nextLine();\r
60 \r
61       // seek end of ids\r
62       if (line.indexOf("*") > -1)\r
63       {\r
64         idsFound=true;\r
65         break;\r
66       }\r
67 \r
68       int abracket = line.indexOf(">");\r
69 \r
70       if (abracket > -1)\r
71       {\r
72 \r
73         if(line.indexOf(" ")>-1)//\r
74         {\r
75           ///Colur it be this format?\r
76           //>54402046         0             1   137   137:\r
77           // or this??\r
78           //     1   >L1H14       30539  343\r
79           try{\r
80             ids.addElement(line.substring(abracket + 1,\r
81                                           line.indexOf(" ", abracket + 1)));\r
82 \r
83 \r
84             // remove p Value\r
85             line = line.substring(abracket + 1);\r
86             line = line.substring(line.indexOf(" ") + 1);\r
87             line = line.trim();\r
88             line = line.substring(line.indexOf(" ") + 1);\r
89             line = line.trim();\r
90             int value = Integer.parseInt(line.substring(0, line.indexOf(" ")));\r
91             starts.addElement(value+"");\r
92             line = line.substring(line.indexOf(" ") + 1);\r
93             line = line.trim();\r
94             value = Integer.parseInt(line.substring(0, line.indexOf(" ")));\r
95             ends.addElement(value+"");\r
96 \r
97           }catch(Exception ex)\r
98           {\r
99             System.err.println("Error during blockfile read.");\r
100             ex.printStackTrace();\r
101             starts.addElement("0");\r
102             ends.addElement("0");\r
103           }\r
104 \r
105         }\r
106         else\r
107         {\r
108           ids.addElement( line.substring(abracket + 1, line.indexOf("/")));\r
109           line = line.substring(line.indexOf("/") + 1);\r
110           starts.addElement(line.substring(0, line.indexOf("-")));\r
111           ends.addElement(line.substring(line.indexOf("-") + 1));\r
112         }\r
113       }\r
114     }while(!idsFound);\r
115 \r
116     int starCol = line.indexOf("*");\r
117     seqstrings = new StringBuffer[ids.size()];\r
118     for(int i=0; i<ids.size(); i++)\r
119     {\r
120       if(seqstrings[i]==null)\r
121         seqstrings[i] = new StringBuffer();\r
122     }\r
123 \r
124     while ((line = nextLine()).indexOf("*")==-1)\r
125     {\r
126       for(int i=0; i<ids.size(); i++)\r
127       {\r
128         if(line.length()>i+starCol)\r
129           seqstrings[i].append(line.charAt(i + starCol));\r
130       }\r
131     }\r
132 \r
133     for(int i=0; i<ids.size(); i++)\r
134     {\r
135       Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
136                                      seqstrings[i].toString(),\r
137                                      Integer.parseInt(starts.elementAt(i).toString()),\r
138                                      Integer.parseInt(ends.elementAt(i).toString()));\r
139       seqs.addElement(newSeq);\r
140     }\r
141 \r
142    }catch(Exception ex){ex.printStackTrace();}\r
143   }\r
144 \r
145   public String print() {\r
146     return print(getSeqsAsArray());\r
147   }\r
148   public static String print(SequenceI[] s) {\r
149     StringBuffer out = new StringBuffer();\r
150 \r
151     int i=0;\r
152     int max = -1;\r
153     while (i < s.length && s[i] != null) {\r
154       out.append(">" + s[i].getName() + "/" + s[i].getStart() + "-" + s[i].getEnd() + "\n");\r
155       if (s[i].getSequence().length() > max) { max = s[i].getSequence().length();}\r
156       i++;\r
157     }\r
158 \r
159       out.append("* iteration 1\n");\r
160       for (int j = 0; j < max; j++) {\r
161         i=0;\r
162         while (i < s.length && s[i] != null) {\r
163           if(s[i].getSequence().length()>j )\r
164             out.append(s[i].getSequence().substring(j,j+1));\r
165           else\r
166             out.append("-");\r
167           i++;\r
168         }\r
169         out.append("\n");\r
170       }\r
171     out.append("*\n");\r
172     return out.toString();\r
173 \r
174   }\r
175 }\r