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