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