JAL-3521 High quality file icons for jalview associated files, and associated mime...
[jalview.git] / utils / gff2annot.pl
1 #!/usr/bin/perl
2 ##
3 # Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4 # Copyright (C) $$Year-Rel$$ The Jalview Authors
5
6 # This file is part of Jalview.
7
8 # Jalview is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License 
10 # as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 #  
12 # Jalview is distributed in the hope that it will be useful, but 
13 # WITHOUT ANY WARRANTY; without even the implied warranty 
14 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15 # PURPOSE.  See the GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
18 # The Jalview Authors are detailed in the 'AUTHORS' file.
19 ##
20
21 use strict;
22 use warnings;
23
24 my %annotLines;
25 my %featureids;
26 my @fields;
27 while (<>) {
28     ($_=~/^\#/) and next;
29     my @fields = split /\s+/, $_;
30     if (scalar @fields) {
31         (defined $annotLines{$fields[1]}) or $annotLines{$fields[1]}=[];
32         # this is the tab-separated set of fields forming a jalview annotation line
33         # we only use sequence IDs, not numbers
34         my $line = [$fields[2],$fields[0],"-1", $fields[3], $fields[4], $fields[2]];
35         $featureids{$fields[2]}="FF0000"; # red is the colour.
36         my $attribs = {};
37         if (scalar @fields>5) {
38             $attribs->{"gff:score"}=$fields[5];
39             (scalar @fields>6) and $attribs->{"gff:strand"}=$fields[6];
40             (scalar @fields>7) and $attribs->{"gff:frame"}=$fields[7];
41             if (scalar @fields>8) {
42                 for (my $i=7; ($i+1)<(scalar @fields); $i+=2) {
43                     $attribs->{"gff:".$fields[$i]} = $fields[$i+1];
44                 }
45             }
46         }
47         push @{$annotLines{$fields[1]}}, [$line, $attribs];
48     }
49 }
50 foreach my $labels (keys %featureids) {
51     print "$labels\t".$featureids{$labels}."\n"; 
52 }
53 foreach my $labels (keys %annotLines) {
54     print "STARTGROUP\t".$labels."\n";
55     foreach my $annot (@{$annotLines{$labels}}) {
56         # bare minimum is written - no attributes/links yet.
57         print "".(join "\t",@{$annot->[0]})."\n"; 
58     }
59     print "ENDGROUP\t".$labels."\n";
60 }
61
62 =pod
63
64 =head1 NAME
65
66 gff2annot.pl
67
68 =head2 SYNOPSIS
69
70
71   gff2annot.pl [one or more files containing gff annotation]
72
73 Generates a nominally usable Jalview Annotation file on B<STDOUT> from arbitrary GFF annotation lines.
74
75 =head2 DESCRIPTION
76
77 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.
78
79 For a series of GFF annotation lines looking like :
80
81 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>]]
82
83 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.
84
85 =head2 Example
86
87 Passing some GFF annotation through STDIN:
88
89   perl gff2annot.pl
90   Seq1 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
91   # a comment
92   Seq1 blasty significant_hsp 15 25 0.9 + 1 link http://mylink/
93   Seq1 blastz significant_hsp 32 43 0.9 + 1 link http://mylink/
94   Seq2 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
95   Seq2 blasty significant_hsp 1 5 0.9 + 1 link http://mylink/
96   Seq2 blastz significant_hsp 1 5 0.9
97   Seq3 blastx significant_hsp 50 70
98   <Control^D/Z>
99
100 Produces
101
102   significant_hsp       FF0000
103   STARTGROUP    blasty
104   significant_hsp       Seq1    -1      15      25      significant_hsp
105   significant_hsp       Seq2    -1      1       5       significant_hsp
106   ENDGROUP      blasty
107   STARTGROUP    blastx
108   significant_hsp       Seq1    -1      1       5       significant_hsp
109   significant_hsp       Seq2    -1      1       5       significant_hsp
110   significant_hsp       Seq3    -1      50      70      significant_hsp
111   ENDGROUP      blastx
112   STARTGROUP    blastz
113   significant_hsp       Seq1    -1      32      43      significant_hsp
114   significant_hsp       Seq2    -1      1       5       significant_hsp
115   ENDGROUP      blastz
116
117 =cut