Sequence is char []
[jalview.git] / src / jalview / io / FeaturesFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 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.datamodel.*;\r
22 \r
23 import java.io.*;\r
24 import java.util.*;\r
25 import jalview.schemes.UserColourScheme;\r
26 \r
27 \r
28 /**\r
29  * DOCUMENT ME!\r
30  *\r
31  * @author $author$\r
32  * @version $Revision$\r
33  */\r
34 public class FeaturesFile extends AlignFile\r
35 {\r
36     /**\r
37      * Creates a new FeaturesFile object.\r
38      */\r
39     public FeaturesFile()\r
40     {\r
41     }\r
42 \r
43 \r
44     /**\r
45      * Creates a new FeaturesFile object.\r
46      *\r
47      * @param inFile DOCUMENT ME!\r
48      * @param type DOCUMENT ME!\r
49      *\r
50      * @throws IOException DOCUMENT ME!\r
51      */\r
52     public FeaturesFile(String inFile, String type)\r
53        throws IOException\r
54     {\r
55         super(inFile, type);\r
56     }\r
57 \r
58     /**\r
59      * The Application can render HTML, but the applet will\r
60      * remove HTML tags and replace links with %LINK%\r
61      * Both need to read links in HTML however\r
62      *\r
63      * @throws IOException DOCUMENT ME!\r
64      */\r
65     public boolean parse(AlignmentI align,\r
66                          Hashtable colours,\r
67                          boolean removeHTML)\r
68     {\r
69       return parse(align, colours, null, removeHTML);\r
70     }\r
71     /**\r
72      * The Application can render HTML, but the applet will\r
73      * remove HTML tags and replace links with %LINK%\r
74      * Both need to read links in HTML however\r
75      *\r
76      * @throws IOException DOCUMENT ME!\r
77      */\r
78     public boolean parse(AlignmentI align,\r
79                          Hashtable colours,\r
80                          Hashtable featureLink,\r
81                          boolean removeHTML)\r
82     {\r
83       String line = null;\r
84       try\r
85       {\r
86         SequenceI seq = null;\r
87         String type, desc, token=null;\r
88 \r
89         int index, start, end;\r
90         float score;\r
91         StringTokenizer st;\r
92         SequenceFeature sf;\r
93         String featureGroup = null, groupLink = null;\r
94         Hashtable typeLink = new Hashtable();\r
95 \r
96         boolean GFFFile = true;\r
97 \r
98         while ( (line = nextLine()) != null)\r
99         {\r
100           if(line.startsWith("#"))\r
101             continue;\r
102 \r
103           st = new StringTokenizer(line, "\t");\r
104           if (st.countTokens()>1 && st.countTokens() < 4 )\r
105           {\r
106             GFFFile = false;\r
107             type = st.nextToken();\r
108             if (type.equalsIgnoreCase("startgroup"))\r
109             {\r
110               featureGroup = st.nextToken();\r
111               if (st.hasMoreElements())\r
112               {\r
113                 groupLink = st.nextToken();\r
114                 featureLink.put(featureGroup, groupLink);\r
115               }\r
116             }\r
117             else if (type.equalsIgnoreCase("endgroup"))\r
118             {\r
119               //We should check whether this is the current group,\r
120               //but at present theres no way of showing more than 1 group\r
121               st.nextToken();\r
122               featureGroup = null;\r
123               groupLink = null;\r
124             }\r
125             else\r
126             {\r
127               UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
128               colours.put(type, ucs.findColour('A'));\r
129               if (st.hasMoreElements())\r
130               {\r
131                 String link = st.nextToken();\r
132                 typeLink.put(type, link);\r
133                 if(featureLink==null)\r
134                   featureLink = new Hashtable();\r
135                 featureLink.put(type, link);\r
136               }\r
137 \r
138             }\r
139             continue;\r
140           }\r
141 \r
142           while (st.hasMoreElements())\r
143           {\r
144 \r
145             if(GFFFile)\r
146             {\r
147               // Still possible this is an old Jalview file,\r
148               // which does not have type colours at the beginning\r
149               token = st.nextToken();\r
150               seq = align.findName(token);\r
151               if(seq != null)\r
152               {\r
153                 desc = st.nextToken();\r
154                 type = st.nextToken();\r
155                 start = Integer.parseInt(st.nextToken());\r
156                 end = Integer.parseInt(st.nextToken());\r
157                 try\r
158                 {\r
159                   score =  new Float(st.nextToken()).floatValue();\r
160                 }\r
161                 catch (NumberFormatException ex)\r
162                 {\r
163                   score = 0;\r
164                 }\r
165 \r
166                 sf = new SequenceFeature(type, desc, start, end, score, null);\r
167 \r
168                 try\r
169                 {\r
170                   sf.setValue("STRAND", st.nextToken());\r
171                   sf.setValue("FRAME", st.nextToken());\r
172                 }\r
173                 catch (Exception ex)\r
174                 {}\r
175 \r
176                 if(st.hasMoreTokens())\r
177                 {\r
178                   StringBuffer attributes = new StringBuffer();\r
179                   while (st.hasMoreTokens())\r
180                   {\r
181                     attributes.append("\t"+st.nextElement());\r
182                   }\r
183                   sf.setValue("ATTRIBUTES", attributes.toString());\r
184                 }\r
185 \r
186                 seq.addSequenceFeature(sf);\r
187 \r
188                 break;\r
189               }\r
190             }\r
191 \r
192             if(GFFFile && seq==null)\r
193             {\r
194               desc = token;\r
195             }\r
196             else\r
197               desc = st.nextToken();\r
198 \r
199 \r
200             token = st.nextToken();\r
201             if (!token.equals("ID_NOT_SPECIFIED"))\r
202             {\r
203               seq = align.findName(token);\r
204               st.nextToken();\r
205             }\r
206             else\r
207             {\r
208               try{\r
209                 index = Integer.parseInt(st.nextToken());\r
210                 seq = align.getSequenceAt(index);\r
211               }\r
212               catch(NumberFormatException ex)\r
213               {\r
214                 seq = null;\r
215               }\r
216             }\r
217 \r
218             if(seq==null)\r
219             {\r
220               System.out.println("Sequence not found: "+line);\r
221               break;\r
222             }\r
223 \r
224             start = Integer.parseInt(st.nextToken());\r
225             end = Integer.parseInt(st.nextToken());\r
226 \r
227             type = st.nextToken();\r
228 \r
229             if (!colours.containsKey(type))\r
230             {\r
231               // Probably the old style groups file\r
232               UserColourScheme ucs = new UserColourScheme(type);\r
233               colours.put(type, ucs.findColour('A'));\r
234             }\r
235 \r
236             sf = new SequenceFeature(type, desc, "", start, end, featureGroup);\r
237 \r
238             seq.addSequenceFeature(sf);\r
239 \r
240             if(groupLink!=null && removeHTML)\r
241             {\r
242               sf.addLink(groupLink);\r
243               sf.description += "%LINK%";\r
244             }\r
245             if(typeLink.containsKey(type) && removeHTML)\r
246             {\r
247               sf.addLink(typeLink.get(type).toString());\r
248               sf.description += "%LINK%";\r
249             }\r
250 \r
251             parseDescriptionHTML(sf, removeHTML);\r
252 \r
253             //If we got here, its not a GFFFile\r
254             GFFFile = false;\r
255           }\r
256         }\r
257       }\r
258       catch (Exception ex)\r
259       {\r
260         System.out.println(line);\r
261         System.out.println("Error parsing feature file: " + ex +"\n"+line);\r
262         return false;\r
263       }\r
264 \r
265       return true;\r
266     }\r
267 \r
268     public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)\r
269     {\r
270       if(removeHTML && sf.getDescription().toUpperCase().indexOf("<HTML>")==-1)\r
271         removeHTML = false;\r
272 \r
273       StringBuffer sb = new StringBuffer();\r
274       StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");\r
275       String token,  link;\r
276       int startTag;\r
277       String tag=null;\r
278       while(st.hasMoreElements())\r
279       {\r
280         token = st.nextToken("&>");\r
281         if(token.equalsIgnoreCase("html") || token.startsWith("/"))\r
282         {\r
283           continue;\r
284         }\r
285 \r
286         tag = null;\r
287         startTag = token.indexOf("<");\r
288 \r
289         if (startTag > -1)\r
290         {\r
291           tag = token.substring(startTag+1);\r
292           token = token.substring(0, startTag);\r
293         }\r
294 \r
295         if (tag != null && tag.toUpperCase().startsWith("A HREF="))\r
296         {\r
297           if(token.length()>0)\r
298             sb.append(token);\r
299           link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);\r
300           String label = st.nextToken("<>");\r
301           sf.addLink(label + "|" + link);\r
302           sb.append(label + "%LINK%");\r
303         }\r
304         else if (tag != null && tag.equalsIgnoreCase("br"))\r
305           sb.append("\n");\r
306         else if (token.startsWith("lt;"))\r
307           sb.append("<" + token.substring(3));\r
308         else if (token.startsWith("gt;"))\r
309           sb.append(">" + token.substring(3));\r
310         else if (token.startsWith("amp;"))\r
311           sb.append("&" + token.substring(4));\r
312         else\r
313         {\r
314           sb.append(token);\r
315         }\r
316       }\r
317 \r
318       if(removeHTML)\r
319         sf.description = sb.toString();\r
320 \r
321   }\r
322 \r
323     /**\r
324      * DOCUMENT ME!\r
325      *\r
326      * @param s DOCUMENT ME!\r
327      * @param len DOCUMENT ME!\r
328      * @param gaps DOCUMENT ME!\r
329      * @param displayId DOCUMENT ME!\r
330      *\r
331      * @return DOCUMENT ME!\r
332      */\r
333     public String printJalviewFormat(SequenceI [] seqs,\r
334                                      Hashtable visible)\r
335     {\r
336         StringBuffer out = new StringBuffer();\r
337         SequenceFeature [] next;\r
338 \r
339         if(visible==null || visible.size()<1)\r
340           return "No Features Visible";\r
341 \r
342         Enumeration en = visible.keys();\r
343         String type;\r
344         int color;\r
345         while( en.hasMoreElements() )\r
346         {\r
347           type = en.nextElement().toString();\r
348           color = Integer.parseInt( visible.get(type).toString() );\r
349           out.append(type + "\t"\r
350                      + jalview.util.Format.getHexString(\r
351                          new java.awt.Color(color)  )\r
352                      +"\n");\r
353         }\r
354 \r
355         //Work out which groups are both present and visible\r
356         Vector groups = new Vector();\r
357         int groupIndex = 0;\r
358 \r
359         for(int i=0; i<seqs.length; i++)\r
360         {\r
361           next = seqs[i].getSequenceFeatures();\r
362           if(next!=null)\r
363           {\r
364             for(int j=0; j<next.length; j++)\r
365             {\r
366               if (!visible.containsKey(next[j].type))\r
367                 continue;\r
368 \r
369               if (    next[j].featureGroup != null\r
370                   && !groups.contains(next[j].featureGroup))\r
371                 groups.addElement(next[j].featureGroup);\r
372             }\r
373           }\r
374         }\r
375 \r
376         String group = null;\r
377 \r
378         do\r
379         {\r
380 \r
381 \r
382           if (groups.size() > 0 && groupIndex < groups.size())\r
383           {\r
384             group = groups.elementAt(groupIndex).toString();\r
385             out.append("\nSTARTGROUP\t" + group + "\n");\r
386           }\r
387           else\r
388             group = null;\r
389 \r
390           for (int i = 0; i < seqs.length; i++)\r
391           {\r
392             next = seqs[i].getSequenceFeatures();\r
393             if (next != null)\r
394             {\r
395               for (int j = 0; j < next.length; j++)\r
396               {\r
397                 if (!visible.containsKey(next[j].type))\r
398                   continue;\r
399 \r
400                 if (group != null\r
401                     && (next[j].featureGroup==null\r
402                         || !next[j].featureGroup.equals(group))\r
403                   )\r
404                   continue;\r
405 \r
406                 if(group==null && next[j].featureGroup!=null)\r
407                   continue;\r
408 \r
409                 if(next[j].description==null || next[j].description.equals(""))\r
410                   out.append(next[j].type+"\t");\r
411                 else\r
412                 {\r
413                   if(next[j].links!=null\r
414                      && next[j].getDescription().indexOf("<html>")==-1)\r
415                     out.append("<html>");\r
416 \r
417                   out.append(next[j].description+" ");\r
418                   if(next[j].links!=null)\r
419                     {\r
420                       for(int l=0; l<next[j].links.size(); l++)\r
421                       {\r
422                         String label = next[j].links.elementAt(l).toString();\r
423                         String href = label.substring(label.indexOf("|")+1);\r
424                         label = label.substring(0, label.indexOf("|"));\r
425 \r
426                         if(next[j].description.indexOf(href)==-1)\r
427                         {\r
428                           out.append("<a href=\""\r
429                                      + href\r
430                                      + "\">"\r
431                                      + label\r
432                                      + "</a>");\r
433                         }\r
434                       }\r
435 \r
436                       if (next[j].getDescription().indexOf("</html>") == -1)\r
437                         out.append("</html>");\r
438                     }\r
439 \r
440 \r
441                    out.append("\t");\r
442                 }\r
443 \r
444 \r
445                 out.append(  seqs[i].getName() + "\t-1\t"\r
446                            + next[j].begin + "\t"\r
447                            + next[j].end + "\t"\r
448                            + next[j].type + "\n"\r
449                     );\r
450               }\r
451             }\r
452           }\r
453 \r
454           if(group!=null)\r
455           {\r
456             out.append("ENDGROUP\t"+group+"\n");\r
457             groupIndex++;\r
458           }\r
459           else\r
460             break;\r
461 \r
462         }\r
463         while(groupIndex < groups.size()+1);\r
464 \r
465 \r
466       return out.toString();\r
467     }\r
468 \r
469     public String printGFFFormat(SequenceI [] seqs, Hashtable visible)\r
470     {\r
471       StringBuffer out = new StringBuffer();\r
472       SequenceFeature [] next;\r
473       String source;\r
474 \r
475       for(int i=0; i<seqs.length; i++)\r
476       {\r
477         if(seqs[i].getSequenceFeatures()!=null)\r
478         {\r
479           next = seqs[i].getSequenceFeatures();\r
480           for(int j=0; j<next.length; j++)\r
481           {\r
482             if(!visible.containsKey(next[j].type))\r
483               continue;\r
484 \r
485             source = next[j].featureGroup;\r
486             if(source==null)\r
487               source = next[j].getDescription();\r
488 \r
489             out.append(seqs[i].getName() + "\t"\r
490                        + source + "\t"\r
491                        + next[j].type  + "\t"\r
492                        + next[j].begin + "\t"\r
493                        + next[j].end   + "\t"\r
494                        + next[j].score + "\t"\r
495                       );\r
496 \r
497             if(next[j].getValue("STRAND")!=null)\r
498               out.append(next[j].getValue("STRAND")+"\t");\r
499             else\r
500               out.append(".\t");\r
501 \r
502             if(next[j].getValue("FRAME")!=null)\r
503               out.append(next[j].getValue("FRAME"));\r
504             else\r
505               out.append(".");\r
506 \r
507             if(next[j].getValue("ATTRIBUTES")!=null)\r
508               out.append(next[j].getValue("ATTRIBUTES"));\r
509 \r
510             out.append("\n");\r
511 \r
512           }\r
513         }\r
514       }\r
515 \r
516       return out.toString();\r
517     }\r
518 \r
519     public void parse()\r
520     {\r
521       //IGNORED\r
522     }\r
523 \r
524     /**\r
525      * DOCUMENT ME!\r
526      *\r
527      * @return DOCUMENT ME!\r
528      */\r
529     public String print()\r
530     {\r
531         return "USE printGFFFormat() or printJalviewFormat()";\r
532     }\r
533 }\r