07b72a00c4dafe89741859613071e27871dd90b1
[jalview.git] / utils / gff2annot.pl
1 #!/usr/bin/perl
2 #*******************************************************************************
3 # Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
4 # Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 #*******************************************************************************
19
20 use strict;
21 use warnings;
22
23 my %annotLines;
24 my %featureids;
25 my @fields;
26 while (<>) {
27     ($_=~/^\#/) and next;
28     my @fields = split /\s+/, $_;
29     if (scalar @fields) {
30         (defined $annotLines{$fields[1]}) or $annotLines{$fields[1]}=[];
31         # this is the tab-separated set of fields forming a jalview annotation line
32         # we only use sequence IDs, not numbers
33         my $line = [$fields[2],$fields[0],"-1", $fields[3], $fields[4], $fields[2]];
34         $featureids{$fields[2]}="FF0000"; # red is the colour.
35         my $attribs = {};
36         if (scalar @fields>5) {
37             $attribs->{"gff:score"}=$fields[5];
38             (scalar @fields>6) and $attribs->{"gff:strand"}=$fields[6];
39             (scalar @fields>7) and $attribs->{"gff:frame"}=$fields[7];
40             if (scalar @fields>8) {
41                 for (my $i=7; ($i+1)<(scalar @fields); $i+=2) {
42                     $attribs->{"gff:".$fields[$i]} = $fields[$i+1];
43                 }
44             }
45         }
46         push @{$annotLines{$fields[1]}}, [$line, $attribs];
47     }
48 }
49 foreach my $labels (keys %featureids) {
50     print "$labels\t".$featureids{$labels}."\n"; 
51 }
52 foreach my $labels (keys %annotLines) {
53     print "STARTGROUP\t".$labels."\n";
54     foreach my $annot (@{$annotLines{$labels}}) {
55         # bare minimum is written - no attributes/links yet.
56         print "".(join "\t",@{$annot->[0]})."\n"; 
57     }
58     print "ENDGROUP\t".$labels."\n";
59 }
60
61 =pod
62
63 =head1 NAME
64
65 gff2annot.pl
66
67 =head2 SYNOPSIS
68
69
70   gff2annot.pl [one or more files containing gff annotation]
71
72 Generates a nominally usable Jalview Annotation file on B<STDOUT> from arbitrary GFF annotation lines.
73
74 =head2 DESCRIPTION
75
76 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.
77
78 For a series of GFF annotation lines looking like :
79
80 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>]]
81
82 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.
83
84 =head2 Example
85
86 Passing some GFF annotation through STDIN:
87
88   perl gff2annot.pl
89   Seq1 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
90   # a comment
91   Seq1 blasty significant_hsp 15 25 0.9 + 1 link http://mylink/
92   Seq1 blastz significant_hsp 32 43 0.9 + 1 link http://mylink/
93   Seq2 blastx significant_hsp 1 5 0.9 + 1 link http://mylink/
94   Seq2 blasty significant_hsp 1 5 0.9 + 1 link http://mylink/
95   Seq2 blastz significant_hsp 1 5 0.9
96   Seq3 blastx significant_hsp 50 70
97   <Control^D/Z>
98
99 Produces
100
101   significant_hsp       FF0000
102   STARTGROUP    blasty
103   significant_hsp       Seq1    -1      15      25      significant_hsp
104   significant_hsp       Seq2    -1      1       5       significant_hsp
105   ENDGROUP      blasty
106   STARTGROUP    blastx
107   significant_hsp       Seq1    -1      1       5       significant_hsp
108   significant_hsp       Seq2    -1      1       5       significant_hsp
109   significant_hsp       Seq3    -1      50      70      significant_hsp
110   ENDGROUP      blastx
111   STARTGROUP    blastz
112   significant_hsp       Seq1    -1      32      43      significant_hsp
113   significant_hsp       Seq2    -1      1       5       significant_hsp
114   ENDGROUP      blastz
115
116 =cut