af4dfc56bee39a473d18de230b445acb2dc2a649
[jalview.git] / utils / gff2annot.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my %annotLines;
7 my %featureids;
8 my @fields;
9 while (<>) {
10     ($_=~/^\#/) and next;
11     my @fields = split /\s+/, $_;
12     if (scalar @fields) {
13         (defined $annotLines{$fields[1]}) or $annotLines{$fields[1]}=[];
14         # this is the tab-separated set of fields forming a jalview annotation line
15         # we only use sequence IDs, not numbers
16         my $line = [$fields[2],$fields[0],"-1", $fields[3], $fields[4], $fields[2]];
17         $featureids{$fields[2]}="FF0000"; # red is the colour.
18         my $attribs = {};
19         if (scalar @fields>5) {
20             $attribs->{"gff:score"}=$fields[5];
21             (scalar @fields>6) and $attribs->{"gff:strand"}=$fields[6];
22             (scalar @fields>7) and $attribs->{"gff:frame"}=$fields[7];
23             if (scalar @fields>8) {
24                 for (my $i=7; ($i+1)<(scalar @fields); $i+=2) {
25                     $attribs->{"gff:".$fields[$i]} = $fields[$i+1];
26                 }
27             }
28         }
29         push @{$annotLines{$fields[1]}}, [$line, $attribs];
30     }
31 }
32 foreach my $labels (keys %featureids) {
33     print "$labels\t".$featureids{$labels}."\n"; 
34 }
35 foreach my $labels (keys %annotLines) {
36     print "STARTGROUP\t".$labels."\n";
37     foreach my $annot (@{$annotLines{$labels}}) {
38         # bare minimum is written - no attributes/links yet.
39         print "".(join "\t",@{$annot->[0]})."\n"; 
40     }
41     print "ENDGROUP\t".$labels."\n";
42 }
43
44 =pod
45
46 =head1 NAME
47
48 gff2annot.pl
49
50 =head2 SYNOPSIS
51
52
53   gff2annot.pl [one or more files containing gff annotation]
54
55 Generates a nominally usable Jalview Annotation file on B<STDOUT> from arbitrary GFF annotation lines.
56
57 =head2 DESCRIPTION
58
59 This script will generate a jalview features file on standard out, from a set of GFF annotation lines input from STDIN and/or any provided filenames.
60
61 For a series of GFF annotation lines looking like :
62
63 E<lt>seqIdE<gt> E<lt>sourceE<gt> E<lt>nameE<gt> E<lt>startE<gt> E<lt>endE<gt> [E<lt>scoreE<gt> E<lt>strandE<gt> E<lt>frameE<gt> [E<lt>AttributeE<gt> E<lt>Attribute-Value<gt>]]
64
65 The script will generate a seuqence features file on B<STDOUT> where annotation with a particular B<source> string will be grouped together under that name.
66
67 =head2 Example
68
69 Passing some GFF annotation through STDIN:
70
71   perl gff2annot.pl
72   Seq1 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
73   # a comment
74   Seq1 blasty significant_hsp 15 25 0.9 + 1 link http://mylink/
75   Seq1 blastz significant_hsp 32 43 0.9 + 1 link http://mylink/
76   Seq2 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
77   Seq2 blasty significant_hsp 1 5 0.9 + 1 link http://mylink/
78   Seq2 blastz significant_hsp 1 5 0.9
79   Seq3 blastx significant_hsp 50 70
80   <Control^D/Z>
81
82 Produces
83
84   significant_hsp       FF0000
85   STARTGROUP    blasty
86   significant_hsp       Seq1    -1      15      25      significant_hsp
87   significant_hsp       Seq2    -1      1       5       significant_hsp
88   ENDGROUP      blasty
89   STARTGROUP    blastx
90   significant_hsp       Seq1    -1      1       5       significant_hsp
91   significant_hsp       Seq2    -1      1       5       significant_hsp
92   significant_hsp       Seq3    -1      50      70      significant_hsp
93   ENDGROUP      blastx
94   STARTGROUP    blastz
95   significant_hsp       Seq1    -1      32      43      significant_hsp
96   significant_hsp       Seq2    -1      1       5       significant_hsp
97   ENDGROUP      blastz
98
99 =cut