| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Getopt::Long; | ||
| 7 | use Pod::Usage; | ||
| 8 | use Bio::AlignIO; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | my $help; | ||
| 12 | my $informat = 'fasta'; | ||
| 13 | my $outformat = 'clustalw'; | ||
| 14 | my $id; | ||
| 15 | my $shorten_id_from_end; | ||
| 16 | GetOptions( | ||
| 17 | 'id=s' => \$id, | ||
| 18 | 'to=s' => \$outformat, | ||
| 19 | 'from=s' => \$informat, | ||
| 20 | 'help' => \$help, | ||
| 21 | 'shorten-id-from-end|s' => \$shorten_id_from_end, | ||
| 22 | ) or die "there is a problems with parsing the options. $!"; | ||
| 23 | |||
| 24 | pod2usage( -verbose => 2, -noperldoc => 1 ) if $help; | ||
| 25 | |||
| 26 | #get filenames from arguments | ||
| 27 | my ( $in_file, $out_file ) = @ARGV; | ||
| 28 | #die if input file doesn't exist | ||
| 29 | die ">>$in_file<< is no file" | ||
| 30 | unless ( -f $in_file ); | ||
| 31 | #die if output file is not vaild | ||
| 32 | die ">>$out_file<< is not a valid output file name" | ||
| 33 | unless ( $out_file && $out_file ne '' ); | ||
| 34 | |||
| 35 | #open input file | ||
| 36 | my $aln_in_fh = Bio::AlignIO->new( | ||
| 37 | -format => $informat, | ||
| 38 | -file => $in_file | ||
| 39 | ); | ||
| 40 | |||
| 41 | #open output file | ||
| 42 | my $aln_out_fh = Bio::AlignIO->new( | ||
| 43 | -format => $outformat, | ||
| 44 | -file => '>' . $out_file | ||
| 45 | ); | ||
| 46 | |||
| 47 | no warnings 'redefine'; | ||
| 48 | |||
| 49 | sub Bio::SimpleAlign::set_displayname_flat { | ||
| 50 | my $self = shift; | ||
| 51 | my ( $nse, $seq ); | ||
| 52 | |||
| 53 | foreach $seq ( $self->each_seq() ) { | ||
| 54 | $nse = $seq->get_nse(); | ||
| 55 | $self->displayname( $nse, substr( $seq->id, -10 ) ); | ||
| 56 | } | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | #write from input to output | ||
| 61 | while ( my $align_object = $aln_in_fh->next_aln ) { | ||
| 62 | |||
| 63 | $align_object->id($id) | ||
| 64 | if ($id); | ||
| 65 | if ($shorten_id_from_end) { | ||
| 66 | $align_object->set_displayname_flat; | ||
| 67 | foreach my $seq ( $align_object->each_seq ) { | ||
| 68 | if ( length( $seq->display_id ) > 10 ) { | ||
| 69 | my $nse = $seq->get_nse(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $aln_out_fh->write_aln($align_object); | ||
| 74 | } | ||
| 75 | |||
| 76 | __END__ | ||
| 77 | |||
| 78 | =head1 NAME | ||
| 79 | |||
| 80 | aln2aln - convert different alignment formats to another alignment format | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | perl aln2aln.pl [OPTIONS] [--help] <input_alignment_file> <file_in_other_format> | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | Takes <input_alignment_file> and converts it to <file_in_other_format> | ||
| 89 | On default, it assumes 'fasta' as input format, but you can | ||
| 90 | use the --from option to make sure that aln2aln uses a different | ||
| 91 | format. Same applies to --to | ||
| 92 | |||
| 93 | Some formats have a limitation on identifier length (>30 characters or so), so | ||
| 94 | check this first, if strange problems occur. | ||
| 95 | |||
| 96 | =head1 OPTIONS | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item B<< --shorten-id-from-end >> | ||
| 101 | |||
| 102 | If the sequence ids from the input alignment are too long, take the last 10 characters of the id. | ||
| 103 | |||
| 104 | =item B<--from <FORMAT>> and B<--to <FORMAT>> | ||
| 105 | |||
| 106 | Assume one of the following alignment input formats: | ||
| 107 | |||
| 108 | FORMATS: | ||
| 109 | |||
| 110 | fasta FASTA format | ||
| 111 | pfam pfam format | ||
| 112 | selex selex (hmmer) format | ||
| 113 | stockholm stockholm format | ||
| 114 | prodom prodom (protein domain) format | ||
| 115 | msf msf (GCG) format | ||
| 116 | mase mase (seaview) format | ||
| 117 | bl2seq Bl2seq Blast output | ||
| 118 | nexus Swofford et al NEXUS format | ||
| 119 | pfam Pfam sequence alignment format | ||
| 120 | phylip Felsenstein's PHYLIP format | ||
| 121 | clustalw ClustalW format | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 SEE ALSO | ||
| 126 | |||
| 127 | L<Bio::AlignIO> | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 132 | |||
| 133 | =cut |