script/get_features.pl untested

filter: Statementstmt 0.0% 0 / 490 Branchbran 0.0% 0 / 252 Conditioncond 0.0% 0 / 103 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 935 CC 114 SCAR 63.0
CC
114
Cov
0%
adjust_coordinates
1122.0
filter_features
702.0
check_requirements
600.0
CRAP 543.4
1 #!/usr/local/bin/perl
2  
3 # documentation at end of file
4  
5 use warnings;
6 use strict;
7 use Getopt::Long qw(:config no_ignore_case bundling);
8 use Pod::Usage;
9 use Bio::ToolBox::Data;
10 use Bio::ToolBox::db_helper qw(verify_or_request_feature_types);
11 use Bio::ToolBox::GeneTools qw(
12 :export
13 :filter
14 :transcript
15 );
16 use Bio::ToolBox::utility qw(ask_user_for_index format_with_commas);
17  
18 our $VERSION = '2.03';
19  
20 print "\n This program will collect features from annotation sources\n\n";
21  
22 ### Quick help
23 unless (@ARGV) {
24  
25 # when no command line options are present
26 # print SYNOPSIS
27 pod2usage(
28 {
29 '-verbose' => 0,
30 '-exitval' => 1,
31 }
32 );
33 }
34  
35 ### Get command line options and initialize values
36 my (
37 $input, $database, $id_list,
38 $get_subfeatures, $include_coordinates, $start_adj,
39 $stop_adj, $position, $use_id_for_bed,
40 $tsl, $gencode, $tbiotype,
41 $collapse, $chromosome_exclude, $outfile,
42 $sort_data, $gz, $bgz,
43 $help, $print_version,
44 );
45 my $convert_to_bed = 0;
46 my $convert_to_gff = 0;
47 my $convert_to_gtf = 0;
48 my $convert_to_refflat = 0;
49 my @features;
50 my @output_tags;
51 my @exclude_tags;
52 my @include_tags;
53 my %exclude_tag2value;
54 my %include_tag2value;
55  
56 # Command line options
57 GetOptions(
58 'i|in=s' => \$input, # input table
59 'd|db=s' => \$database, # source annotation database
60 'l|list=s' => \$id_list, # file of IDs to keep
61 'f|feature=s' => \@features, # the features to collect from the database
62 'u|sub!' => \$get_subfeatures, # collect subfeatures
63 'coord!' => \$include_coordinates, # collect coordinates
64 'b|start=i' => \$start_adj, # start coordinate adjustment
65 'e|stop=i' => \$stop_adj, # stop coordinate adjustment
66 'p|pos=s' => \$position, # relative position to adjust coordinates
67 't|tag=s' => \@output_tags, # attributes to include in output
68 'x|exclude=s' => \@exclude_tags, # attribute and keys to exclude
69 'n|include=s' => \@include_tags, # attribute and keys to include
70 'tsl=s' => \$tsl, # filter on transcript support level
71 'gencode!' => \$gencode, # filter on gencode basic tag
72 'biotype=s' => \$tbiotype, # filter on transcript biotype
73 'collapse!' => \$collapse, # collapse multi-transcript genes
74 'K|chrskip=s' => \$chromosome_exclude, # skip chromosomes
75 'B|bed!' => \$convert_to_bed, # convert to bed format
76 'G|gff|gff3!' => \$convert_to_gff, # convert to GFF3 format
77 'g|gtf!' => \$convert_to_gtf, # convert to gtf format
78 'r|refflat!' => \$convert_to_refflat, # convert to refFlat format
79 'useid!' => \$use_id_for_bed, # use ID for bed name
80 'o|out=s' => \$outfile, # name of output file
81 'sort!' => \$sort_data, # sort the output file
82 'z|gz!' => \$gz, # compress output
83 'Z|bgz!' => \$bgz, # compress with bgzip
84 'h|help' => \$help, # request help
85 'v|version' => \$print_version, # print the version
86 ) or die " unrecognized option(s)!! please refer to the help documentation\n\n";
87  
88 # Print help
89 if ($help) {
90  
91 # print entire POD
92 pod2usage(
93 {
94 '-verbose' => 2,
95 '-exitval' => 1,
96 }
97 );
98 }
99  
100 # Print version
101 if ($print_version) {
102 print " Biotoolbox script get_features.pl, version $VERSION\n";
103 eval {
104 require Bio::ToolBox;
105 my $v = Bio::ToolBox->VERSION;
106 print " Biotoolbox package version $v\n";
107 };
108 exit;
109 }
110  
111 ### Check for requirements
112 check_requirements();
113  
114 ### Fill Data object
115 my $Data;
116 if ($input) {
117 $Data = load_from_infile();
118 }
119 else {
120 $Data = load_from_database();
121 }
122  
123 ### Filter
124 filter_features();
125  
126 ### Write out file
127 if ( $start_adj or $stop_adj ) {
128 printf " Adjusting start by %s bp and stop by %s bp relative to position %s\n",
129 $start_adj, $stop_adj,
130 $position eq '5' ? 'start'
131 : $position eq '3' ? 'end'
132 : ( $position eq '4' or $position eq 'm' ) ? 'middle'
133 : $position eq '53' ? 'both ends'
134 : 'neither';
135 }
136 if ($convert_to_bed) {
137 print " Writing to bed file...\n";
138 export_to_bed();
139 }
140 elsif ($convert_to_gff) {
141 print " Writing to GFF3 file...\n";
142 export_to_gff();
143 }
144 elsif ($convert_to_gtf) {
145 print " Writing to GTF file...\n";
146 export_to_gtf();
147 }
148 elsif ($convert_to_refflat) {
149 print " Writing to refFlat file...\n";
150 export_to_ucsc();
151 }
152 else {
153 print " Writing to text file...\n";
154 export_to_txt();
155 }
156  
157 ######################## Subroutines ###################################
158  
159 sub check_requirements {
160  
161 # check input
162 unless ( $database or $input ) {
163 print STDERR
164 " FATAL: Must provide an input file or database name! use --help for more information\n";
165 exit 1;
166 }
167 if ( $input and $input =~ /\. (?: sqlite | db )$/xi ) {
168  
169 # whoops! specifiying a database file as input
170 $database = $input;
171 undef $input;
172 }
173 if ($id_list) {
174 unless ( -e $id_list and -r _ ) {
175 print STDERR " FATAL: unable to read list file '$id_list'!\n";
176 exit 1;
177 }
178 }
179  
180 # check if feature is a comma delimited list
181 if ( scalar @features == 1 and $features[0] =~ /,/ ) {
182 @features = split /,/, shift @features;
183 }
184 if ( scalar(@features) > 1 and $input ) {
185 printf
186 " WARNING: Only one top feature allowed when parsing an input file!\n Subfeatures do not need to be specified. Using '%s'\n\n",
187 $features[0];
188 }
189  
190 # check conversions
191 my $conversions =
192 $convert_to_bed + $convert_to_gff + $convert_to_gtf + $convert_to_refflat;
193 if ( $conversions > 1 ) {
194 print STDERR " FATAL: Too many bed/gff/gtf/refFlat conversions specified!\n";
195 exit 1;
196 }
197 if ( $convert_to_gff or $convert_to_gtf or $convert_to_refflat ) {
198 $get_subfeatures = 1 if ( $input and not defined $get_subfeatures );
199 }
200  
201 # check collapse
202 if ($collapse) {
203 unless ($get_subfeatures) {
204 print STDERR
205 " FATAL: Cannot collapse transcript unless subfeatures are turned on!\n";
206 exit 1;
207 }
208 unless ( $convert_to_gff
209 or $convert_to_gtf
210 or $convert_to_refflat
211 or $convert_to_bed )
212 {
213 print STDERR
214 " FATAL: Cannot collapse transcripts unless writing to BED12/GFF/GTF/refFlat!\n";
215 exit 1;
216 }
217 }
218  
219 # check adjustments
220 if ( $start_adj or $stop_adj ) {
221  
222 # automatically include coordinates if we're adjusting them
223 if ($get_subfeatures) {
224 print STDERR
225 " FATAL: Cannot adjust coordinates when including subfeatures!\n";
226 exit 1;
227 }
228 $include_coordinates = 1;
229 }
230 if ($position) {
231 if ( $position !~ /^ (?: 5 | 3 | 53 | m | 4 | p) $/x ) {
232 print STDERR " FATAL: unrecognized position value '$position'! see help\n";
233 exit 1;
234 }
235 }
236 else {
237 # default is from both ends
238 $position = '53';
239 }
240  
241 # check tags to include output
242 if ( @output_tags and scalar(@output_tags) == 1 and $output_tags[0] =~ /,/ ) {
243 @output_tags = split /,/, shift @output_tags;
244 }
245  
246 # exclude tags
247 if (@exclude_tags) {
248 foreach my $et (@exclude_tags) {
249 my ( $k, $v ) = split /[,=]/, $et;
250 unless ( defined $k and defined $v ) {
251 print STDERR " FATAL: exclude tags must be a \"key=value\" pair!\n";
252 exit 1;
253 }
254 $exclude_tag2value{$k} = $v;
255 }
256 }
257  
258 # include tags
259 if (@include_tags) {
260 foreach my $it (@include_tags) {
261 my ( $k, $v ) = split /[,=]/, $it;
262 unless ( defined $k and defined $v ) {
263 print STDERR " FATAL: include tags must be a \"key=value\" pair!\n";
264 exit 1;
265 }
266 $include_tag2value{$k} = $v;
267 }
268 }
269  
270 # check output
271 unless ($outfile) {
272 print STDERR " FATAL: Must provide an output file!\n";
273 exit 1;
274 }
275 if ($bgz) {
276 $gz = 2;
277 $sort_data = 1;
278 }
279 }
280  
281 sub load_from_database {
282  
283 # validate and/or request features
284 @features = verify_or_request_feature_types(
285 'db' => $database,
286 'feature' => \@features,
287 'prompt' => " Enter the feature(s) to collect."
288 . " A comma de-limited list or range may be given\n",
289 ) or die " no valid features were provided! see help\n";
290  
291 # generate a list from the database
292 my $D = Bio::ToolBox::Data->new(
293 db => $database,
294 feature => join( ',', @features ),
295 chrskip => $chromosome_exclude,
296 ) or die " unable to generate new feature list\n";
297  
298 if ( $D->last_row ) {
299 printf " Loaded %s features from %s.\n", format_with_commas( $D->last_row ),
300 $input ? $input : $database;
301 }
302 else {
303 print STDERR " FATAL: No features loaded from database '$database'!\n";
304 exit 1;
305 }
306 return $D;
307 }
308  
309 sub load_from_infile {
310  
311 # parse file
312 my $D = Bio::ToolBox::Data->new(
313 file => $input,
314 parse => 1,
315 simplify => 0, # we want everything!
316 feature => $features[0],
317 subfeature => $get_subfeatures ? 'exon,cds,utr,codon' : undef,
318 chrskip => $chromosome_exclude,
319 ) or die " unable to load input file '$input'\n";
320  
321 if ( $D->last_row ) {
322 printf " Loaded %s features from $input.\n", format_with_commas( $D->last_row );
323 }
324 else {
325 print STDERR
326 " FATAL: No top features loaded from file '$input'!\n Check your feature type. If you are attempting to parse subfeatures\n"
327 . " like exon or CDS, try the program get_gene_regions instead.\n";
328 exit 1;
329 }
330 return $D;
331 }
332  
333 sub filter_features {
334  
335 # filter on specified list
336 if ($id_list) {
337 print " Filtering IDs based on provided list from '$id_list'...\n";
338  
339 # load the list file
340 my $List = Bio::ToolBox::Data->new( file => $id_list );
341 if ($List) {
342  
343 # we've loaded the list
344 my $i = $List->id_column;
345 unless ( defined $i ) {
346 $i = ask_user_for_index( $List,
347 "provide the index for the ID column to filter on " );
348 }
349  
350 # generate hash
351 my %wanted;
352 $List->iterate(
353 sub {
354 my $row = shift;
355 $wanted{ $row->value($i) } = 1;
356 }
357 );
358  
359 # now filter the genes
360 my @unwanted;
361 $Data->iterate(
362 sub {
363 my $row = shift;
364 my $v = $row->primary_id;
365 if ( not exists $wanted{$v} ) {
366  
367 # the tag doesn't match a wanted item, so discard
368 push @unwanted, $row->row_index;
369 }
370 }
371 );
372 if (@unwanted) {
373 $Data->delete_row(@unwanted);
374 }
375 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
376 }
377 else {
378 print " unable to load list!\n";
379 }
380 }
381  
382 # filter on specific tags
383 if (%exclude_tag2value) {
384 foreach my $k ( keys %exclude_tag2value ) {
385 my $check = $exclude_tag2value{$k};
386 print " Filtering out tag $k => $check...\n";
387 my @unwanted;
388 $Data->iterate(
389 sub {
390 my $row = shift;
391 my ($v) = $row->seqfeature(1)->get_tag_values($k);
392 if ( $v =~ /$check/i ) {
393  
394 # the tag matches, so discard
395 push @unwanted, $row->row_index;
396 }
397 }
398 );
399 if (@unwanted) {
400 $Data->delete_row(@unwanted);
401 }
402 }
403 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
404 }
405 if (%include_tag2value) {
406 foreach my $k ( keys %include_tag2value ) {
407 my $check = $include_tag2value{$k};
408 print " Filtering for tag $k => $check...\n";
409 my @unwanted;
410 $Data->iterate(
411 sub {
412 my $row = shift;
413 my ($v) = $row->seqfeature(1)->get_tag_values($k);
414 if ( $v !~ /$check/i ) {
415  
416 # the tag doesn't match, so discard
417 push @unwanted, $row->row_index;
418 }
419 }
420 );
421 if (@unwanted) {
422 $Data->delete_row(@unwanted);
423 }
424 }
425 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
426 }
427  
428 # filter on gencode
429 if ($tbiotype) {
430 print " Filtering transcript biotype for $tbiotype...\n";
431 my @unwanted;
432 $Data->iterate(
433 sub {
434 my $row = shift;
435 my $good =
436 filter_transcript_biotype( $row->seqfeature(1), $tbiotype );
437 unless ( defined $good ) {
438 push @unwanted, $row->row_index;
439 next;
440 }
441 my @t = get_transcripts($good); # verify we have transcripts
442 if ( scalar @t ) {
443 $Data->store_seqfeature( $row->row_index, $good );
444 }
445 else {
446 push @unwanted, $row->row_index;
447 }
448 }
449 );
450 if (@unwanted) {
451 $Data->delete_row(@unwanted);
452 }
453 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
454 }
455  
456 # filter on tsl
457 if ($tsl) {
458 print " Filtering for transcript support level of $tsl...\n";
459 my @unwanted;
460 $Data->iterate(
461 sub {
462 my $row = shift;
463 my $good =
464 filter_transcript_support_level( $row->seqfeature(1), $tsl );
465 unless ( defined $good ) {
466 push @unwanted, $row->row_index;
467 next;
468 }
469 my @t = get_transcripts($good); # verify we have transcripts
470 if ( scalar @t ) {
471 $Data->store_seqfeature( $row->row_index, $good );
472 }
473 else {
474 push @unwanted, $row->row_index;
475 }
476 }
477 );
478 if (@unwanted) {
479 $Data->delete_row(@unwanted);
480 }
481 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
482 }
483  
484 # filter on gencode
485 if ($gencode) {
486 print " Filtering for GENCODE transcripts...\n";
487 my @unwanted;
488 $Data->iterate(
489 sub {
490 my $row = shift;
491 my $good = filter_transcript_gencode_basic( $row->seqfeature(1) );
492 unless ( defined $good ) {
493 push @unwanted, $row->row_index;
494 next;
495 }
496 my @t = get_transcripts($good); # verify we have transcripts
497 if ( scalar @t ) {
498 $Data->store_seqfeature( $row->row_index, $good );
499 }
500 else {
501 push @unwanted, $row->row_index;
502 }
503 }
504 );
505 if (@unwanted) {
506 $Data->delete_row(@unwanted);
507 }
508 printf " Kept %s features.\n", format_with_commas( $Data->last_row );
509 }
510  
511 # collapse transcripts
512 if ($collapse) {
513 print " Collapsing alternate transcripts...\n";
514 $Data->iterate(
515 sub {
516 my $row = shift;
517 my $gene = collapse_transcripts( $row->seqfeature(1) );
518 if ($gene) {
519 $Data->store_seqfeature( $row->row_index, $gene );
520 }
521 }
522 );
523 }
524 }
525  
526 sub export_to_bed {
527  
528 # prepare output
529 my $outData = Bio::ToolBox::Data->new( bed => $get_subfeatures ? 12 : 6, )
530 or die "unable to create output Data structure!\n";
531  
532 # Write method based on subfeatures or coordinate adjustment
533 if ( $start_adj or $stop_adj ) {
534  
535 # adjust coordinates as necessary and write a BED6 file
536 $Data->iterate(
537 sub {
538 my $row = shift;
539 my $f = $row->seqfeature(1); # make sure we get the seqfeature
540 my ( $start, $stop ) = adjust_coordinates($f);
541 my $string = $row->bed_string(
542 start => $start,
543 end => $stop,
544 name => $use_id_for_bed ? $f->primary_id : $f->display_name
545 || $f->primary_id,
546 );
547 $outData->add_row($string);
548 $Data->delete_seqfeature( $row->row_index );
549 }
550 );
551 }
552 elsif ($get_subfeatures) {
553  
554 # write transcripts as BED12
555 if ( $features[0] =~ /gene/i ) {
556 print " NOTE: gene information is discarded when writing BED12\n";
557 }
558 $Data->iterate(
559 sub {
560 my $row = shift;
561 my $f = $row->seqfeature(1); # make sure we get the seqfeature
562 my $string = bed12_string( $f, $use_id_for_bed );
563 foreach ( split /\n/, $string ) {
564 $outData->add_row($_);
565 }
566 $Data->delete_seqfeature( $row->row_index );
567 }
568 );
569 }
570 else {
571 # write ordinary BED6
572 $Data->iterate(
573 sub {
574 my $row = shift;
575 my $f = $row->seqfeature(1); # make sure we get the seqfeature
576 my $string = $row->bed_string(
577 name => $use_id_for_bed ? $f->primary_id : $f->display_name
578 || $f->primary_id );
579 $outData->add_row($string);
580 $Data->delete_seqfeature( $row->row_index );
581 }
582 );
583 }
584 print " done\n";
585  
586 # sort as necessary
587 if ($sort_data) {
588 print " Sorting data...\n";
589 $outData->gsort_data;
590 }
591  
592 # write
593 unless ( $outfile =~ /\.bed (?:\.gz)? $/xi ) {
594 $outfile .= '.bed';
595 }
596 $outfile = $outData->write_file(
597 filename => $outfile,
598 gz => $gz,
599 );
600 print " wrote file $outfile\n";
601 }
602  
603 sub export_to_gff {
604  
605 # check output filename
606 unless ( $outfile =~ /\.gff 3? (?:\.gz)? $/xi ) {
607 $outfile .= '.gff3';
608 }
609  
610 # how we write the output depends on whether we need to sort the file or not
611 if ($sort_data) {
612  
613 # we need to keep all gff lines in memory so that we can sort the lines
614  
615 my $outData = Bio::ToolBox::Data->new( gff => 3, )
616 or die "unable to create output Data structure!\n";
617 $outData->add_comment(
618 sprintf( "exported from %s\n", $database ? $database : $input ) );
619  
620 # iterate
621 $Data->iterate(
622 sub {
623 my $row = shift;
624 my $string = $row->seqfeature(1)->gff_string(1);
625  
626 # force retrieving the seqfeature, and recurse through subfeature
627 foreach ( split /\n/, $string ) {
628 $outData->add_row($_);
629 }
630 $Data->delete_seqfeature( $row->row_index );
631 }
632 );
633 print " Sorting data...\n";
634 $outData->gsort_data;
635 $outfile = $outData->write_file(
636 filename => $outfile,
637 gz => $gz,
638 );
639 }
640 else {
641 # we can simply write out gff directly
642 $outfile .= '.gz' if ( $gz and $outfile !~ /\.gz$/i );
643 my $fh = Bio::ToolBox::Data->open_to_write_fh( $outfile, $gz )
644 or die "unable to open '$outfile' for writing!\n";
645 $fh->print("##gff-version 3\n");
646 $fh->printf( "# exported from %s\n", $database ? $database : $input );
647  
648 # write to GFF
649 $Data->iterate(
650 sub {
651 my $row = shift;
652 my $string = $row->seqfeature(1)->gff_string(1);
653  
654 # force retrieving the seqfeature, and recurse through subfeature
655 $fh->print( $string . "###\n" ); # include pragma close lines
656 }
657 );
658 $fh->close;
659 }
660 printf " wrote file $outfile\n";
661 }
662  
663 sub export_to_gtf {
664  
665 # check output filename
666 unless ( $outfile =~ /\.gtf (?:\.gz)? $/xi ) {
667 $outfile .= '.gtf';
668 }
669  
670 # how we write the output depends on whether we need to sort the file or not
671 if ($sort_data) {
672  
673 # we need to keep all gff lines in memory so that we can sort the lines
674  
675 my $outData = Bio::ToolBox::Data->new( gff => 2.5, )
676 or die "unable to create output Data structure!\n";
677 $outData->add_comment(
678 sprintf( "exported from %s\n", $database ? $database : $input ) );
679  
680 # iterate
681 $Data->iterate(
682 sub {
683 my $row = shift;
684 my $string = gtf_string( $row->seqfeature(1) );
685 foreach ( split /\n/, $string ) {
686 $outData->add_row($_);
687 }
688 $Data->delete_seqfeature( $row->row_index );
689 }
690 );
691 print " Sorting data...\n";
692 $outData->gsort_data;
693 $outfile = $outData->write_file(
694 filename => $outfile,
695 gz => $gz,
696 );
697 }
698 else {
699 # we can simply write out gff directly
700 $outfile .= '.gz' if ( $gz and $outfile !~ /\.gz$/i );
701 my $fh = Bio::ToolBox::Data->open_to_write_fh( $outfile, $gz )
702 or die "unable to open '$outfile' for writing!\n";
703 $fh->print("##gff-version 2.5\n");
704 $fh->printf( "# exported from %s\n", $database ? $database : $input );
705  
706 # write to GTF
707 $Data->iterate(
708 sub {
709 my $row = shift;
710 my $string = gtf_string( $row->seqfeature(1) );
711 $fh->print($string);
712 }
713 );
714 $fh->close;
715 }
716 printf " wrote file $outfile\n";
717 }
718  
719 sub export_to_txt {
720  
721 # adjust coordinates as necessary
722 if ( $start_adj or $stop_adj ) {
723  
724 # make sure we establish the feature type first, before we add
725 # coordinate columns, otherwise features might not be returned properly
726 my $ftype = $Data->feature_type;
727  
728 # add coordinate columns
729 my $seq_i = $Data->add_column('Chromosome');
730 my $start_i = $Data->add_column('Start');
731 my $stop_i = $Data->add_column('Stop');
732 my $strand_i = $Data->add_column('Strand');
733  
734 # iterate
735 $Data->iterate(
736 sub {
737 my $row = shift;
738 my $f = $row->seqfeature(1);
739 my ( $start, $stop ) = adjust_coordinates($f);
740 $row->value( $seq_i, $f->seq_id );
741 $row->value( $start_i, $start );
742 $row->value( $stop_i, $stop );
743 $row->value( $strand_i, $f->strand );
744 }
745 );
746 }
747 elsif ($include_coordinates) {
748  
749 # just include original coordinates
750  
751 # make sure we establish the feature type first, before we add
752 # coordinate columns, otherwise features might not be returned properly
753 my $ftype = $Data->feature_type;
754  
755 # add coordinate columns
756 my $seq_i = $Data->add_column('Chromosome');
757 my $start_i = $Data->add_column('Start');
758 my $stop_i = $Data->add_column('Stop');
759 my $strand_i = $Data->add_column('Strand');
760  
761 # iterate
762 $Data->iterate(
763 sub {
764 my $row = shift;
765 my $f = $row->seqfeature(1);
766 $row->value( $seq_i, $f->seq_id );
767 $row->value( $start_i, $f->start );
768 $row->value( $stop_i, $f->stop );
769 $row->value( $strand_i, $f->strand );
770 }
771 );
772 }
773  
774 # collect attribute tags, this does not recurse
775 if (@output_tags) {
776 foreach my $t (@output_tags) {
777 my $i = $Data->add_column($t);
778 $Data->iterate(
779 sub {
780 my $row = shift;
781  
782 # get the tag value from the feature and record it, null if not present
783 my @v =
784 $row->seqfeature->get_tag_values($t); # could be more than 1
785 $row->value( $i, @v ? join( ',', @v ) : '.' );
786 }
787 );
788 }
789 }
790  
791 # sort if requested
792 if ( $sort_data and $include_coordinates ) {
793 print " Sorting data...\n";
794 $Data->gsort_data;
795 }
796  
797 # write the output
798 my $success = $Data->write_file(
799 filename => $outfile,
800 gz => $gz,
801 );
802 printf " wrote file $success\n" if $success;
803 }
804  
805 sub export_to_ucsc {
806  
807 # check output filename
808 unless ( $outfile =~ /\. (?: refflat | ucsc ) (?:\.gz)? $/xi ) {
809 $outfile .= '.refFlat';
810 }
811  
812 # how we write the output depends on whether we need to sort the file or not
813 if ($sort_data) {
814  
815 # we need to keep all gff lines in memory so that we can sort the lines
816  
817 my $outData = Bio::ToolBox::Data->new( ucsc => 11, )
818 or die "unable to create output Data structure!\n";
819 $outData->add_comment(
820 sprintf( "exported from %s\n", $database ? $database : $input ) );
821  
822 # iterate
823 $Data->iterate(
824 sub {
825 my $row = shift;
826 my $string = ucsc_string( $row->seqfeature(1), $use_id_for_bed );
827 foreach ( split /\n/, $string ) {
828 $outData->add_row($_);
829 }
830 $Data->delete_seqfeature( $row->row_index );
831 }
832 );
833 print " Sorting data...\n";
834 $outData->gsort_data;
835 $outfile = $outData->write_file(
836 filename => $outfile,
837 gz => $gz,
838 );
839 }
840 else {
841 # we can simply write out gff directly
842 $outfile .= '.gz' if ( $gz and $outfile !~ /\.gz$/i );
843 my $fh = Bio::ToolBox::Data->open_to_write_fh( $outfile, $gz )
844 or die "unable to open '$outfile' for writing!\n";
845 $fh->printf( "# exported from %s\n", $database ? $database : $input );
846  
847 # write to UCSC file
848 $Data->iterate(
849 sub {
850 my $row = shift;
851 my $string = ucsc_string( $row->seqfeature(1), $use_id_for_bed );
852 $fh->print($string);
853 }
854 );
855 $fh->close;
856 }
857 printf " wrote file $outfile\n";
858 }
859  
860 sub adjust_coordinates {
861 my $feature = shift;
862  
863 # we will always adjust relative coordinates based on strand
864 # and not absolute coordinates
865 my ( $start, $end );
866  
867 # get the original coordinates
868 my $fstart = $feature->start;
869 my $fend = $feature->end;
870  
871 # adjust from 5' end
872 if ( $position eq '5' ) {
873  
874 if ( $feature->strand >= 0 ) {
875  
876 # forward strand
877 if ($start_adj) {
878 $start = $fstart + $start_adj;
879 }
880 else {
881 $start = $fstart;
882 }
883 if ($stop_adj) {
884 $end = $fstart + $stop_adj;
885 }
886 else {
887 $end = $fstart;
888 }
889 }
890 else {
891 # reverse strand
892 if ($start_adj) {
893 $end = $fend - $start_adj;
894 }
895 else {
896 $end = $fend;
897 }
898 if ($stop_adj) {
899 $start = $fend - $stop_adj;
900 }
901 else {
902 $start = $fend;
903 }
904 }
905 }
906  
907 # adjust from 3' end
908 elsif ( $position eq '3' ) {
909  
910 if ( $feature->strand >= 0 ) {
911  
912 # forward strand
913 if ($start_adj) {
914 $end = $fend + $start_adj;
915 }
916 else {
917 $end = $fend;
918 }
919 if ($stop_adj) {
920 $start = $fend + $stop_adj;
921 }
922 else {
923 $start = $fend;
924 }
925 }
926 else {
927 # reverse strand
928 if ($start_adj) {
929 $start = $fstart - $start_adj;
930 }
931 else {
932 $start = $fstart;
933 }
934 if ($stop_adj) {
935 $end = $fstart - $stop_adj;
936 }
937 else {
938 $end = $fstart;
939 }
940 }
941 }
942  
943 # adjust from middle position
944 elsif ( $position eq 'm' or $position eq '4' ) {
945  
946 my $midpoint = int( ( ( $fstart + $fend ) / 2 ) + 0.5 );
947 if ( $feature->strand >= 0 ) {
948  
949 # forward strand
950 if ($start_adj) {
951 $start = $midpoint + $start_adj;
952 }
953 else {
954 $start = $midpoint;
955 }
956 if ($stop_adj) {
957 $end = $midpoint + $stop_adj;
958 }
959 else {
960 $end = $midpoint;
961 }
962 }
963 else {
964 # reverse strand
965 if ($start_adj) {
966 $end = $midpoint - $start_adj;
967 }
968 else {
969 $end = $midpoint;
970 }
971 if ($stop_adj) {
972 $start = $midpoint - $stop_adj;
973 }
974 else {
975 $start = $midpoint;
976 }
977 }
978 }
979  
980 # adjust from both ends
981 elsif ( $position eq '53' ) {
982  
983 if ( $feature->strand >= 0 ) {
984  
985 # forward strand
986 if ($start_adj) {
987 $start = $fstart + $start_adj;
988 }
989 else {
990 $start = $fstart;
991 }
992 if ($stop_adj) {
993 $end = $fend + $stop_adj;
994 }
995 else {
996 $end = $fend;
997 }
998 }
999 else {
1000 # reverse strand
1001 if ($start_adj) {
1002 $end = $fend - $start_adj;
1003 }
1004 else {
1005 $end = $fend;
1006 }
1007 if ($stop_adj) {
1008 $start = $fstart - $stop_adj;
1009 }
1010 else {
1011 $start = $fstart;
1012 }
1013 }
1014 }
1015  
1016 # adjust from narrowPeak file peak or summit position, defaults to midpoint
1017 elsif ( $position eq 'p' ) {
1018  
1019 my $peak = $feature->peak;
1020 if ( $feature->strand >= 0 ) {
1021  
1022 # forward strand
1023 if ($start_adj) {
1024 $start = $peak + $start_adj;
1025 }
1026 else {
1027 $start = $peak;
1028 }
1029 if ($stop_adj) {
1030 $end = $peak + $stop_adj;
1031 }
1032 else {
1033 $end = $peak;
1034 }
1035 }
1036 else {
1037 # reverse strand
1038 if ($start_adj) {
1039 $end = $peak - $start_adj;
1040 }
1041 else {
1042 $end = $peak;
1043 }
1044 if ($stop_adj) {
1045 $start = $peak - $stop_adj;
1046 }
1047 else {
1048 $start = $peak;
1049 }
1050 }
1051 }
1052  
1053 # flip coordinates to make start and stop consistent with strand
1054 # sometimes when only one coordinate is changed, it flips the orientation
1055 # start must always be less than the stop coordinate
1056 # but always respect the given strand
1057 if ( $start > $end ) {
1058 my $newstart = $end;
1059 my $newend = $start;
1060 $start = $newstart;
1061 $end = $newend;
1062 }
1063  
1064 # make sure no negative coordinates sneak through
1065 $start = 1 if $start < 1;
1066  
1067 # return
1068 return ( $start, $end );
1069 }
1070  
1071 __END__