| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | my $columns = 50; | ||
| 6 | my $gapped = 0; | ||
| 7 | my $sorted = 0; | ||
| 8 | |||
| 9 | my $progname = $0; | ||
| 10 | $progname =~ s/^.*?([^\/]+)$/$1/; | ||
| 11 | |||
| 12 | my $usage = "Usage: $progname [<Stockholm file(s)>]\n"; | ||
| 13 | $usage .= " [-h] print this help message\n"; | ||
| 14 | $usage .= " [-g] write gapped FASTA output\n"; | ||
| 15 | $usage .= " [-s] sort sequences by name\n"; | ||
| 16 | $usage .= " [-c <cols>] number of columns for FASTA output (default is $columns)\n"; | ||
| 17 | # parse cmd-line opts | ||
| 18 | my @argv; | ||
| 19 | while (@ARGV) { | ||
| 20 | my $arg = shift; | ||
| 21 | if ( $arg eq "-h" ) { | ||
| 22 | die $usage; | ||
| 23 | } elsif ( $arg eq "-g" ) { | ||
| 24 | $gapped = 1; | ||
| 25 | } elsif ( $arg eq "-s" ) { | ||
| 26 | $sorted = 1; | ||
| 27 | } elsif ( $arg eq "-c" ) { | ||
| 28 | defined( $columns = shift ) or die $usage; | ||
| 29 | } else { | ||
| 30 | push @argv, $arg; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | @ARGV = @argv; | ||
| 34 | |||
| 35 | my %seq; | ||
| 36 | while (<>) { | ||
| 37 | next unless /\S/; | ||
| 38 | next if /^\s*\#/; | ||
| 39 | if (/^\s*\/\//) { printseq() } | ||
| 40 | else { | ||
| 41 | chomp; | ||
| 42 | my ( $name, $seq ) = split; | ||
| 43 | $seq =~ s/[\.\-]//g unless $gapped; | ||
| 44 | $seq{$name} .= $seq; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | printseq(); | ||
| 48 | |||
| 49 | sub printseq { | ||
| 50 | if ($sorted) { | ||
| 51 | foreach my $key ( sort keys %seq ) { | ||
| 52 | print ">$key\n"; | ||
| 53 | for ( my $i = 0 ; $i < length $seq{$key} ; $i += $columns ) { | ||
| 54 | print substr( $seq{$key}, $i, $columns ), "\n"; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | while ( my ( $name, $seq ) = each %seq ) { | ||
| 60 | print ">$name\n"; | ||
| 61 | for ( my $i = 0 ; $i < length $seq ; $i += $columns ) { | ||
| 62 | print substr( $seq, $i, $columns ), "\n"; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | %seq = (); | ||
| 67 | } | ||
| 68 | |||
| 69 | =pod | ||
| 70 | This is slightly modified from the original (https://github.com/ihh/dart/blob/master/perl/stockholm2fasta.pl) by Ian Holmes | ||
| 71 | |||
| 72 | This program is free software: you can redistribute it and/or modify | ||
| 73 | it under the terms of the GNU General Public License as published by | ||
| 74 | the Free Software Foundation, either version 3 of the License, or | ||
| 75 | (at your option) any later version. | ||
| 76 | |||
| 77 | This program is distributed in the hope that it will be useful, | ||
| 78 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 79 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 80 | GNU General Public License for more details. | ||
| 81 | |||
| 82 | You should have received a copy of the GNU General Public License | ||
| 83 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 84 | =cut |