Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* GFFStreamFeature.java
*
* created: Tue Sep 14 1999
*
* This file is part of Artemis
*
* Copyright (C) 1999,2000,2001 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/io/GFFStreamFeature.java,v 1.1 2004-06-09 09:49:33 tjc Exp $
*/
package uk.ac.sanger.artemis.io;
import uk.ac.sanger.artemis.util.*;
import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
/**
* A StreamFeature that thinks it is a GFF feature.
*
* @author Kim Rutherford
* @version $Id: GFFStreamFeature.java,v 1.1 2004-06-09 09:49:33 tjc Exp $
**/
public class GFFStreamFeature
extends SimpleDocumentFeature
implements DocumentFeature, StreamFeature, ComparableFeature {
/**
* Create a new GFFStreamFeature object. The feature should be added
* to an Entry (with Entry.add ()).
* @param key The new feature key
* @param location The Location object for the new feature
* @param qualifiers The qualifiers for the new feature
**/
public GFFStreamFeature (final Key key,
final Location location,
final QualifierVector qualifiers) {
super (null);
try {
setKey (key);
setLocation (location);
setQualifiers (qualifiers);
if (getQualifierByName ("score") == null) {
setQualifier (new Qualifier ("score", "."));
}
if (getQualifierByName ("gff_source") == null) {
setQualifier (new Qualifier ("gff_source", "artemis"));
}
if (getQualifierByName ("gff_seqname") == null) {
setQualifier (new Qualifier ("gff_seqname", "."));
}
} catch (EntryInformationException e) {
// this should never happen because the feature will not be in an Entry
throw new Error ("internal error - unexpected exception: " + e);
} catch (ReadOnlyException e) {
// this should never happen because the feature will not be in an Entry
throw new Error ("internal error - unexpected exception: " + e);
} catch (OutOfRangeException e) {
// this should never happen because the feature will not be in an Entry
throw new Error ("internal error - unexpected exception: " + e);
}
}
/**
* Create a new GFFStreamFeature with the same key, location and
* qualifiers as the given feature. The feature should be added to an
* Entry (with Entry.add ()).
* @param feature The feature to copy.
**/
public GFFStreamFeature (final Feature feature) {
this (feature.getKey (), feature.getLocation (), feature.getQualifiers ());
if (feature instanceof GFFStreamFeature) {
gff_lines = new StringVector (((GFFStreamFeature)feature).gff_lines);
}
}
/**
* Return the reference of a new copy of this Feature.
**/
public Feature copy () {
final Feature return_value = new GFFStreamFeature (this);
return return_value;
}
/**
* Create a new GFFStreamFeature from the given line. The String should be
* in gene finder format.
**/
private GFFStreamFeature (final String line)
throws ReadFormatException {
super (null);
final StringVector line_bits = StringVector.getStrings (line, "\t", true);
if (line_bits.size () < 8) {
throw new ReadFormatException ("invalid GFF line: 8 fields needed " +
"(got " + line_bits.size () +
" fields) from: " + line);
}
final String start_base_string = line_bits.elementAt (3).trim ();
final String end_base_string = line_bits.elementAt (4).trim ();
final int start_base;
final int end_base;
try {
start_base = Integer.parseInt (start_base_string);
} catch (NumberFormatException e) {
throw new ReadFormatException ("Could not understand the start base " +
"of a GFF feature: " + start_base_string);
}
try {
end_base = Integer.parseInt (end_base_string);
} catch (NumberFormatException e) {
throw new ReadFormatException ("Could not understand the end base " +
"of a GFF feature: " + end_base_string);
}
// start of qualifier parsing and setting
try {
final boolean complement_flag;
if (line_bits.elementAt (6).equals ("+")) {
complement_flag = false;
} else {
if (line_bits.elementAt (6).equals ("-")) {
complement_flag = true;
} else {
// must be unstranded
complement_flag = false;
// best we can do
final String note_string = "this feature is unstranded";
setQualifier (new Qualifier ("note", note_string));
}
}
if (line_bits.size () == 9) {
final String rest_of_line = line_bits.elementAt (8);
// parse the rest of the line as ACeDB format attributes
final Hashtable attributes = parseAttributes (rest_of_line);
for (final java.util.Enumeration attribute_enum = attributes.keys ();
attribute_enum.hasMoreElements();) {
final String name = (String) attribute_enum.nextElement();
final StringVector values = (StringVector) attributes.get (name);
if (values.size () == 0) {
setQualifier (new Qualifier (name));
} else {
setQualifier (new Qualifier (name, values));
}
}
}
final Qualifier gff_seqname =
new Qualifier ("gff_seqname", line_bits.elementAt (0));
setQualifier (gff_seqname);
final Key key = new Key (line_bits.elementAt (2));
setKey (key);
final Qualifier source_qualifier =
new Qualifier ("gff_source", line_bits.elementAt (1));
setQualifier (source_qualifier);
final Qualifier score_qualifier =
new Qualifier ("score", line_bits.elementAt (5));
setQualifier (score_qualifier);
String frame = line_bits.elementAt (7);
if (frame.equals ("0")) {
frame = "1";
} else {
if (frame.equals ("1")) {
frame = "2";
} else {
if (frame.equals ("2")) {
frame = "3";
} else {
frame = ".";
}
}
}
if (!frame.equals ("1") && !frame.equals (".")) {
final Qualifier codon_start_qualifier =
new Qualifier ("codon_start", frame);
setQualifier (codon_start_qualifier);
}
if (start_base > end_base) {
throw new ReadFormatException ("start position is greater than end " +
"position: " + start_base + " > " +
end_base);
}
if (start_base < 0) {
throw new ReadFormatException ("start position must be positive: " +
start_base);
}
final Range location_range = new Range (start_base, end_base);
final RangeVector location_ranges = new RangeVector (location_range);
setLocation (new Location (location_ranges, complement_flag));
} catch (ReadOnlyException e) {
throw new Error ("internal error - unexpected exception: " + e);
} catch (EntryInformationException e) {
throw new Error ("internal error - unexpected exception: " + e);
} catch (OutOfRangeException e) {
throw new Error ("internal error - unexpected exception: " + e);
}
this.gff_lines = new StringVector (line);
}
/**
* Helper method for the constructor - returns a String that is the
* concatenation of the Strings in the given StringVector. The strings
* will be separated by four spaces
**/
private String joinStringVector (final StringVector string_vector) {
final StringBuffer return_buffer = new StringBuffer ();
for (int i = 0 ; i < string_vector.size () ; ++i) {
if (i != 0) {
return_buffer.append (" ");
}
return_buffer.append (string_vector.elementAt (i));
}
return return_buffer.toString ();
}
/**
* Read and return a GFFStreamFeature from a stream. A feature must be the
* next thing in the stream.
* @param stream the Feature is read from this stream
* @exception IOException thrown if there is a problem reading the Feature -
* most likely ReadFormatException.
* @exception InvalidRelationException Thrown if this Feature cannot contain
* the given Qualifier.
* @return null if in_stream is at the end of file when the method is
* called
*/
protected static GFFStreamFeature readFromStream (LinePushBackReader stream)
throws IOException, InvalidRelationException {
String line = stream.readLine ();
if (line == null) {
return null;
}
try {
final GFFStreamFeature new_feature = new GFFStreamFeature (line);
return new_feature;
} catch (ReadFormatException exception) {
// re-throw the exception with the line number added
final String new_error_string = exception.getMessage ();
throw new ReadFormatException (new_error_string,
stream.getLineNumber ());
}
}
/**
* Read the details of a feature from an EMBL stream into the current
* object.
* @param entry_information The EntryInformation object of the Entry that
* will contain the Feature.
* @param in_stream the Feature is read from this stream
* @exception IOException thrown if there is a problem reading the Feature -
* most likely ReadFormatException if the stream does not contain GFF
* feature.
**/
public void setFromStream (final EntryInformation entry_information,
final LinePushBackReader in_stream)
throws IOException, InvalidRelationException, ReadOnlyException {
throw new ReadOnlyException ();
}
/**
* Write this Feature to the given stream.
* @param writer The stream to write to.
* @exception IOException thrown if there is an io problem while writing
* the Feature.
**/
public void writeToStream (final Writer writer)
throws IOException {
// for now GFF features are read-only so just write what we read
if (gff_lines == null) {
final RangeVector ranges = getLocation ().getRanges ();
for (int i = 0 ; i < ranges.size () ; ++i) {
final Range this_range = ranges.elementAt (i);
Qualifier seqname = getQualifierByName ("gff_seqname");
Qualifier source = getQualifierByName ("gff_source");
Qualifier score = getQualifierByName ("score");
Qualifier group = getQualifierByName ("group");
if (seqname == null) {
seqname = new Qualifier ("gff_seqname", "");
}
if (source == null) {
source = new Qualifier ("source", "");
}
if (score == null) {
score = new Qualifier ("score", "");
}
if (group == null || group.getValues () == null ||
group.getValues ().elementAt (0).equals ("")) {
final Qualifier gene = getQualifierByName ("gene");
if (gene == null) {
group = new Qualifier ("group", "");
} else {
group = gene;
}
}
String frame = ".";
final Qualifier codon_start = getQualifierByName ("codon_start");
if (codon_start != null && i == 0) {
frame = codon_start.getValues ().elementAt (0);
if (frame.equals ("1")) {
frame = "0";
} else {
if (frame.equals ("2")) {
frame = "1";
} else {
if (frame.equals ("3")) {
frame = "2";
} else {
frame = ".";
}
}
}
}
final String attribute_string = unParseAttributes ();
writer.write (seqname.getValues ().elementAt (0) + "\t" +
source.getValues ().elementAt (0) + "\t" +
getKey () + "\t" +
this_range.getStart () + "\t" +
this_range.getEnd () + "\t" +
score.getValues () .elementAt (0)+ "\t" +
(getLocation ().isComplement () ? "-\t" : "+\t") +
frame + "\t" +
attribute_string + "\n");
}
} else {
for (int i = 0 ; i < gff_lines.size () ; ++i) {
writer.write (gff_lines.elementAt (i) + "\n");
}
}
}
/**
* Return a String containing the qualifiers of this feature in a form
* suitable for using as the last field of a GFF line. The codon_start
* attribute is not included since GFF has a frame field. gff_seqname,
* gff_source and score aren't included since they have corresponding
* fields.
**/
private String unParseAttributes () {
final StringBuffer buffer = new StringBuffer ();
final QualifierVector qualifiers = getQualifiers ();
final QualifierVector qualifiers_to_write = new QualifierVector ();
for (int i = 0 ; i < qualifiers.size () ; ++i) {
final Qualifier this_qualifier = qualifiers.elementAt (i);
final String name = this_qualifier.getName ();
if (name.equals ("codon_start") || name.equals ("gff_source") ||
name.equals ("gff_seqname") || name.equals ("score")) {
continue;
}
if (i != 0) {
buffer.append (" ; ");
}
final StringVector values = this_qualifier.getValues ();
buffer.append (name);
if (values != null) {
for (int value_index = 0 ;
value_index < values.size () ;
++value_index) {
final String this_value = values.elementAt (value_index);
buffer.append (' ');
try {
buffer.append (Integer.valueOf (this_value));
} catch (NumberFormatException _) {
// not an integer
try {
buffer.append (Double.valueOf (this_value));
} catch (NumberFormatException __) {
// not a double or integer so quote it
buffer.append ('"' + this_value + '"');
}
}
}
}
}
return buffer.toString ();
}
/**
* Parse the given String as ACeDB format attributes.
* Adapted from code by Matthew Pocock for the BioJava project.
* @return Return a Hashtable. Each key is an attribute name and each value
* of the Hashtable is a StringVector containing the attribute values.
* If the attribute has no value then the Hashtable value will be a zero
* length vector.
**/
private Hashtable parseAttributes (final String att_val_list) {
Hashtable attributes = new Hashtable ();
StringTokenizer tokeniser = new StringTokenizer (att_val_list, ";", false);
while (tokeniser.hasMoreTokens()) {
final String this_token = tokeniser.nextToken().trim();
final int index_of_first_space = this_token.indexOf(" ");
final String att_name;
final StringVector att_values = new StringVector ();
if (index_of_first_space == -1) {
att_name = this_token;
} else {
att_name = this_token.substring (0, index_of_first_space);
String rest_of_token =
this_token.substring (index_of_first_space).trim();
while (rest_of_token.length () > 0) {
if (rest_of_token.startsWith ("\"")) {
int quote_index = 0;
do {
quote_index++;
quote_index = rest_of_token.indexOf ("\"", quote_index);
} while (quote_index > -1 &&
rest_of_token.charAt(quote_index - 1) == '\\');
if (quote_index < 0) {
// no closing quote - panic
final Hashtable panic_attributes = new Hashtable ();
final StringVector notes = new StringVector ();
notes.add (att_val_list);
panic_attributes.put ("note", notes);
return panic_attributes;
}
final String next_bit = rest_of_token.substring (1, quote_index);
att_values.add (next_bit);
rest_of_token = rest_of_token.substring (quote_index + 1).trim();
} else {
final int index_of_next_space = rest_of_token.indexOf (" ");
if (index_of_next_space == -1) {
att_values.add (rest_of_token);
rest_of_token = "";
} else {
final String next_bit =
rest_of_token.substring (0, index_of_next_space);
att_values.add (next_bit);
rest_of_token =
rest_of_token.substring (index_of_next_space).trim ();
}
}
}
}
if (attributes.get (att_name) != null) {
((StringVector) attributes.get (att_name)).add (att_values);
} else {
attributes.put (att_name, att_values);
}
}
return attributes;
}
/**
* The DocumentEntry object that contains this Feature as passed to the
* constructor.
**/
private DocumentEntry entry;
/**
* This is the line of GFF input that was read to get this
* GFFStreamFeature. A GFFStreamFeature that was created from multiple GFF
* lines will have a gff_lines variable that contains multiple line.
**/
StringVector gff_lines = null;
}