]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/Stockholm.pm
added missing files
[biopieces.git] / code_perl / Maasha / Stockholm.pm
1 package Maasha::Stockholm;
2
3 # Copyright (C) 2006 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # Routines for manipulation of the Stockholm format.
26 # http://www.cgb.ki.se/cgb/groups/sonnhammer/Stockholm.html
27
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 use warnings;
33 use strict;
34 use Data::Dumper;
35 use Maasha::Common;
36 use vars qw ( @ISA @EXPORT );
37
38 @ISA = qw( Exporter );
39
40
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
42
43
44 sub get_stockholm_entry
45 {
46     # Martin A. Hansen, February 2007.
47
48     # Given a file handle, returns the next stockholm
49     # entry as a list of lines.
50
51     my ( $fh,   # file handle
52        ) = @_;
53
54     # returns a list
55
56     my ( $line, @lines );
57
58     while ( defined $fh and $line = <$fh> )
59     {
60         chomp $line;
61
62         push @lines, $line;
63
64         last if $line eq "//";
65     }
66
67     if ( not @lines ) {
68         return undef;
69     } else {
70         return wantarray ? @lines : \@lines;
71     }
72 }
73
74
75 sub parse_stockholm_entry
76 {
77     # Martin A. Hansen, February 2007.
78     
79     # given a Stockholm entry as a list of lines,
80     # parses this into an elaborate data structure.
81     # Compultory fields: AC ID DE AU SE SS BM GA TC NC TP SQ
82     # Non-compultory fields: PI DC DR RC RN RM RT RA RL CC
83
84     my ( $entry,   # stockholm entry
85        ) = @_;
86
87     # returns data structure
88
89     my ( $line, %hash, %align_hash, @align_list, @align );
90
91     foreach $line ( @{ $entry } )
92     {
93         next if $line =~ /^# /;
94
95         if ( $line =~ /^#=GF\s+([^\s]+)\s+(.*)$/ )
96         {
97             push @{ $hash{ "GF" }{ $1 } }, $2;
98         }
99         elsif ( $line =~ /^#=GC\s+([^\s]+)\s+(.*)$/ )
100         {
101             push @{ $hash{ "GC" }{ $1 } }, $2;
102         }
103         elsif ( $line =~ /^#=GS\s+([^\s]+)\s+([^\s]+)\s+(.*)$/ )
104         {
105             push @{ $hash{ "GS" }{ $1 }{ $2 } }, $3;
106         }
107         elsif ( $line =~ /^#=GR\s+([^\s]+)\s+([^\s]+)\s+(.*)$/ )
108         {
109             push @{ $hash{ "GR" }{ $1 }{ $2 } }, $3;
110         }
111         elsif ( $line =~ /^([^\s]+)\s+(.+)$/ )
112         {
113             push @align_list, $1 if not exists $align_hash{ $1 };
114
115             $align_hash{ $1 } .= $2;
116         }
117     }
118
119     map { $hash{ "GF" }{ $_ } = join " ", @{ $hash{ "GF" }{ $_ } } } keys %{ $hash{ "GF" } };
120     map { $hash{ "GC" }{ $_ } = join  "", @{ $hash{ "GC" }{ $_ } } } keys %{ $hash{ "GC" } };
121     map { push @align, [ $_, $align_hash{ $_ } ] } @align_list;
122
123     push @align, [ "SS_cons", $hash{ "GC" }{ "SS_cons" } ];
124     push @align, [ "RF", $hash{ "GC" }{ "RF" } ] if $hash{ "GC" }{ "RF" };
125
126     delete $hash{ "GC" };
127
128     $hash{ "ALIGN" } = \@align;
129
130     return wantarray ? %hash : \%hash;
131 }
132
133
134 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<