2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
21 import jalview.datamodel.*;
\r
25 import jalview.schemes.UserColourScheme;
\r
32 * @version $Revision$
\r
34 public class FeaturesFile extends AlignFile
\r
37 * Creates a new FeaturesFile object.
\r
39 public FeaturesFile()
\r
44 * Creates a new FeaturesFile object.
\r
46 * @param inStr DOCUMENT ME!
\r
48 public FeaturesFile(String inStr)
\r
54 * Creates a new FeaturesFile object.
\r
56 * @param inFile DOCUMENT ME!
\r
57 * @param type DOCUMENT ME!
\r
59 * @throws IOException DOCUMENT ME!
\r
61 public FeaturesFile(String inFile, String type)
\r
64 super(inFile, type);
\r
68 * The Application can render HTML, but the applet will
\r
69 * remove HTML tags and replace links with %LINK%
\r
70 * Both need to read links in HTML however
\r
72 * @throws IOException DOCUMENT ME!
\r
74 public boolean parse(AlignmentI align,
\r
78 return parse(align, colours, null, removeHTML);
\r
81 * The Application can render HTML, but the applet will
\r
82 * remove HTML tags and replace links with %LINK%
\r
83 * Both need to read links in HTML however
\r
85 * @throws IOException DOCUMENT ME!
\r
87 public boolean parse(AlignmentI align,
\r
89 Hashtable featureLink,
\r
95 SequenceI seq = null;
\r
96 String type, desc, token=null;
\r
98 int index, start, end;
\r
100 StringTokenizer st;
\r
101 SequenceFeature sf;
\r
102 String featureGroup = null, groupLink = null;
\r
103 Hashtable typeLink = new Hashtable();
\r
105 boolean GFFFile = true;
\r
107 while ( (line = nextLine()) != null)
\r
109 if(line.startsWith("#"))
\r
112 st = new StringTokenizer(line, "\t");
\r
113 if (st.countTokens()>1 && st.countTokens() < 4 )
\r
116 type = st.nextToken();
\r
117 if (type.equalsIgnoreCase("startgroup"))
\r
119 featureGroup = st.nextToken();
\r
120 if (st.hasMoreElements())
\r
122 groupLink = st.nextToken();
\r
123 featureLink.put(featureGroup, groupLink);
\r
126 else if (type.equalsIgnoreCase("endgroup"))
\r
128 //We should check whether this is the current group,
\r
129 //but at present theres no way of showing more than 1 group
\r
131 featureGroup = null;
\r
136 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
137 colours.put(type, ucs.findColour("A"));
\r
138 if (st.hasMoreElements())
\r
140 String link = st.nextToken();
\r
141 typeLink.put(type, link);
\r
142 if(featureLink==null)
\r
143 featureLink = new Hashtable();
\r
144 featureLink.put(type, link);
\r
151 while (st.hasMoreElements())
\r
156 // Still possible this is an old Jalview file,
\r
157 // which does not have type colours at the beginning
\r
158 token = st.nextToken();
\r
159 seq = align.findName(token);
\r
162 desc = st.nextToken();
\r
163 type = st.nextToken();
\r
164 start = Integer.parseInt(st.nextToken());
\r
165 end = Integer.parseInt(st.nextToken());
\r
168 score = new Float(st.nextToken()).floatValue();
\r
170 catch (NumberFormatException ex)
\r
175 sf = new SequenceFeature(type, desc, start, end, score, null);
\r
179 sf.setValue("STRAND", st.nextToken());
\r
180 sf.setValue("FRAME", st.nextToken());
\r
182 catch (Exception ex)
\r
185 seq.getDatasetSequence().addSequenceFeature(sf);
\r
191 if(GFFFile && seq==null)
\r
196 desc = st.nextToken();
\r
199 token = st.nextToken();
\r
200 if (!token.equals("ID_NOT_SPECIFIED"))
\r
202 seq = align.findName(token);
\r
208 index = Integer.parseInt(st.nextToken());
\r
209 seq = align.getSequenceAt(index);
\r
211 catch(NumberFormatException ex)
\r
219 System.out.println("Sequence not found: "+line);
\r
223 start = Integer.parseInt(st.nextToken());
\r
224 end = Integer.parseInt(st.nextToken());
\r
226 type = st.nextToken();
\r
228 if (!colours.containsKey(type))
\r
230 // Probably the old style groups file
\r
231 UserColourScheme ucs = new UserColourScheme(type);
\r
232 colours.put(type, ucs.findColour("A"));
\r
235 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
237 seq.addSequenceFeature(sf);
\r
239 if(groupLink!=null && removeHTML)
\r
241 sf.addLink(groupLink);
\r
242 sf.description += "%LINK%";
\r
244 if(typeLink.containsKey(type) && removeHTML)
\r
246 sf.addLink(typeLink.get(type).toString());
\r
247 sf.description += "%LINK%";
\r
250 parseDescriptionHTML(sf, removeHTML);
\r
252 //If we got here, its not a GFFFile
\r
257 catch (Exception ex)
\r
259 System.out.println(line);
\r
260 ex.printStackTrace();
\r
261 System.out.println("Error parsing feature file: " + ex +"\n"+line);
\r
268 void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
270 StringBuffer sb = new StringBuffer();
\r
271 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
272 String token, link;
\r
273 while(st.hasMoreElements())
\r
275 token = st.nextToken("<>");
\r
276 if(token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
279 if(token.startsWith("a href="))
\r
281 link = token.substring(token.indexOf("\"")+1, token.length()-1);
\r
282 String label = st.nextToken("<>");
\r
283 sf.addLink(label+"|"+link);
\r
284 sb.append(label+"%LINK%");
\r
286 else if(token.equalsIgnoreCase("br"))
\r
293 sf.description = sb.toString();
\r
299 * @param s DOCUMENT ME!
\r
300 * @param len DOCUMENT ME!
\r
301 * @param gaps DOCUMENT ME!
\r
302 * @param displayId DOCUMENT ME!
\r
304 * @return DOCUMENT ME!
\r
306 public String printJalviewFormat(SequenceI [] seqs,
\r
309 StringBuffer out = new StringBuffer();
\r
310 SequenceFeature [] next;
\r
312 if(visible==null || visible.size()<1)
\r
313 return "No Features Visible";
\r
315 Enumeration en = visible.keys();
\r
318 while( en.hasMoreElements() )
\r
320 type = en.nextElement().toString();
\r
321 color = Integer.parseInt( visible.get(type).toString() );
\r
322 out.append(type + "\t"
\r
323 + jalview.util.Format.getHexString(
\r
324 new java.awt.Color(color) )
\r
328 //Work out which groups are both present and visible
\r
329 Vector groups = new Vector();
\r
330 int groupIndex = 0;
\r
332 for(int i=0; i<seqs.length; i++)
\r
334 next = seqs[i].getSequenceFeatures();
\r
337 for(int j=0; j<next.length; j++)
\r
339 if (!visible.containsKey(next[j].type))
\r
342 if ( next[j].featureGroup != null
\r
343 && !groups.contains(next[j].featureGroup))
\r
344 groups.addElement(next[j].featureGroup);
\r
349 String group = null;
\r
355 if (groups.size() > 0 && groupIndex < groups.size())
\r
357 group = groups.elementAt(groupIndex).toString();
\r
358 out.append("\nSTARTGROUP\t" + group + "\n");
\r
363 for (int i = 0; i < seqs.length; i++)
\r
365 next = seqs[i].getSequenceFeatures();
\r
368 for (int j = 0; j < next.length; j++)
\r
370 if (!visible.containsKey(next[j].type))
\r
374 && (next[j].featureGroup==null
\r
375 || !next[j].featureGroup.equals(group))
\r
379 if(group==null && next[j].featureGroup!=null)
\r
382 if(next[j].description==null || next[j].description.equals(""))
\r
383 out.append(next[j].type+"\t");
\r
385 out.append(next[j].description + "\t");
\r
387 out.append( seqs[i].getName() + "\t-1\t"
\r
388 + next[j].begin + "\t"
\r
389 + next[j].end + "\t"
\r
390 + next[j].type + "\n"
\r
398 out.append("ENDGROUP\t"+group+"\n");
\r
405 while(groupIndex < groups.size()+1);
\r
408 return out.toString();
\r
411 public String printGFFFormat(SequenceI [] seqs, Hashtable visible)
\r
413 StringBuffer out = new StringBuffer();
\r
414 SequenceFeature [] next;
\r
416 for(int i=0; i<seqs.length; i++)
\r
418 if(seqs[i].getSequenceFeatures()!=null)
\r
420 next = seqs[i].getSequenceFeatures();
\r
421 for(int j=0; j<next.length; j++)
\r
423 if(!visible.containsKey(next[j].type))
\r
426 out.append(seqs[i].getName() + "\t"
\r
427 + next[j].description + "\t"
\r
428 + next[j].type + "\t"
\r
429 + next[j].begin + "\t"
\r
430 + next[j].end + "\t"
\r
431 + next[j].score + "\t"
\r
434 if(next[j].getValue("STRAND")!=null)
\r
435 out.append(next[j].getValue("STRAND")+"\t");
\r
438 if(next[j].getValue("FRAME")!=null)
\r
439 out.append(next[j].getValue("FRAME")+"\n");
\r
447 return out.toString();
\r
450 public void parse()
\r
458 * @return DOCUMENT ME!
\r
460 public String print()
\r
462 return "USE printGFFFormat() or printJalviewFormat()";
\r