| 1 | #!/usr/bin/env perl | ||
| 2 | #original clustal2stockholm.pl from DART | ||
| 3 | # (https://github.com/ihh/dart/blob/master/perl/clustal2stockholm.pl) | ||
| 4 | # Modified by Christopher Bottoms to also incorporate structure data. | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use lib 'lib'; | ||
| 10 | use Bio::App::SELEX::Stockholm; | ||
| 11 | |||
| 12 | my $usage = "\nUsage: $0 <ClustalW file> [gc_file] | ||
| 13 | Converts a ClustalW file (from file or STDIN) to Stockholm format. | ||
| 14 | Only converts sequence information, unless a secondary structure | ||
| 15 | file is given as the second argument.\n"; | ||
| 16 | |||
| 17 | main(@ARGV) unless caller(); | ||
| 18 | |||
| 19 | sub main { | ||
| 20 | my @cmd_line_args = @_; | ||
| 21 | my @argv; | ||
| 22 | my $gc_string; | ||
| 23 | if (@ARGV) { | ||
| 24 | my $arg = shift @cmd_line_args; | ||
| 25 | my $gc_file = shift @cmd_line_args; | ||
| 26 | if ( defined $gc_file ) { | ||
| 27 | open( my $gc_fh, '<', $gc_file ); | ||
| 28 | $gc_string = _get_last_gc_from_file($gc_fh); | ||
| 29 | close($gc_fh); | ||
| 30 | } | ||
| 31 | |||
| 32 | # Exit with usage info if $arg is a flag. Otherwise save it for later. | ||
| 33 | if ( $arg =~ /^-/ ) { | ||
| 34 | if ( ( $arg eq "-h" ) || ( $arg eq "--help" ) ) { | ||
| 35 | print $usage; | ||
| 36 | exit; | ||
| 37 | } | ||
| 38 | else { die $usage; } | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | push @argv, $arg; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | |||
| 46 | # Make stdin the default input file. | ||
| 47 | push @argv, "-"; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $file = shift @argv or die $usage; | ||
| 51 | |||
| 52 | open( my $aln_fh, '<', $file ); | ||
| 53 | my $stk; | ||
| 54 | |||
| 55 | if ( defined $gc_string ) { | ||
| 56 | $stk = | ||
| 57 | Bio::App::SELEX::Stockholm->new( gc => { SS_cons => $gc_string } ); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $stk = Bio::App::SELEX::Stockholm->new(); | ||
| 61 | } | ||
| 62 | |||
| 63 | my $gapChars = '-'; | ||
| 64 | while (<$aln_fh>) { | ||
| 65 | my @a = split; | ||
| 66 | next unless @a == 2; | ||
| 67 | |||
| 68 | my ( $seq, $data ) = @a; | ||
| 69 | next | ||
| 70 | if ( $data =~ /[^a-zA-Z$gapChars]/ ) | ||
| 71 | ; # skip primary sequence conservation lines, etc. | ||
| 72 | |||
| 73 | if ( defined $stk->seqdata->{$seq} ) { | ||
| 74 | $stk->seqdata->{$seq} .= $data; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | push @{ $stk->seqname }, $seq; | ||
| 78 | $stk->seqdata->{$seq} = $data; | ||
| 79 | } | ||
| 80 | |||
| 81 | } | ||
| 82 | die unless $stk->is_flush(); | ||
| 83 | |||
| 84 | print $stk->to_string(); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _extract_gc_string_from { | ||
| 88 | my $line = shift; | ||
| 89 | |||
| 90 | #Remove newline | ||
| 91 | chomp $line; | ||
| 92 | |||
| 93 | #Remove carriage return; | ||
| 94 | $line =~ s/\r//; | ||
| 95 | |||
| 96 | #Remove everything from the first space to the end of the line | ||
| 97 | $line =~ s/\s+.*\z//; | ||
| 98 | |||
| 99 | return $line; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _get_last_gc_from_file { | ||
| 103 | my $fh = shift; | ||
| 104 | |||
| 105 | #Read all lines of file | ||
| 106 | my @lines = <$fh>; | ||
| 107 | |||
| 108 | # Extract the gc string from the last line of the file | ||
| 109 | my $gc_string = _extract_gc_string_from($lines[-1]); | ||
| 110 | |||
| 111 | return $gc_string; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |