~ubuntu-branches/ubuntu/trusty/bioperl/trusty

« back to all changes in this revision

Viewing changes to scripts/utilities/bp_sreformat.pl

  • Committer: Package Import Robot
  • Author(s): Charles Plessy
  • Date: 2013-09-22 13:39:48 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20130922133948-c6z62zegjyp7ztou
Tags: 1.6.922-1
* New upstream release.
* Replaces and Breaks grinder (<< 0.5.3-3~) because of overlaping contents.
  Closes: #722910
* Stop Replacing and Breaking bioperl ( << 1.6.9 ): not needed anymore. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!perl
 
2
# Author:  Jason Stajich <jason-at-bioperl-dot-org>
 
3
# Purpose: Bioperl implementation of Sean Eddy's sreformat
 
4
#          We're not as clever as Sean's squid library though so
 
5
#          you have to specify the input format rather than letting
 
6
#          the application guess.
 
7
 
 
8
use strict;
 
9
use warnings;
 
10
use Bio::SeqIO;
 
11
use Bio::AlignIO;
 
12
use Getopt::Long;
 
13
 
 
14
my $USAGE = "bp_sreformat -if INFORMAT -of OUTFORMAT -i FILENAME -o output.FORMAT
 
15
 
 
16
-h/--help               Print this help
 
17
-if/--informat          Specify the input format
 
18
-of/--outformat         Specify the output format
 
19
-i/--input              Specify the input file name
 
20
                        (to pass in data on STDIN use minus sign as filename)
 
21
-o/--output             Specify the output file name
 
22
                        (to pass data out on STDOUT use minus sign as filename)
 
23
--msa                   Specify this is multiple sequence alignment data
 
24
--special=specialparams Specify special params supported by some formats
 
25
                        Comma or space separated please.
 
26
                        These include:
 
27
                        nointerleaved   -- for phylip,non-interleaved format
 
28
                        idlinebreak     -- for phylip, makes it molphy format
 
29
                        percentages     -- for clustalw, show % id per line
 
30
                        flat            -- don't show start-end in seqid
 
31
                        linelength      -- line length for clustalw
 
32
                        mrbayes         -- for MrBayes proper NEXUS output
 
33
";
 
34
 
 
35
 
 
36
my ($input,$output,$informat,$outformat,$msa,$special);
 
37
 
 
38
GetOptions(
 
39
           'h|help'          => sub { print STDERR ($USAGE); exit(0) },
 
40
           'i|input:s'         => \$input,
 
41
           'o|output:s'        => \$output,
 
42
           'if|informat:s'     => \$informat,
 
43
           'of|outformat:s'    => \$outformat,
 
44
           'msa'               => \$msa,
 
45
           's|special:s'       => \$special,
 
46
           );
 
47
 
 
48
unless( defined $informat && defined $outformat ) { 
 
49
    die(sprintf("Cannot proceed without a defined input and output you gave (%s,%s)\n",
 
50
                defined $informat ? $informat : "''" ,
 
51
                defined $outformat ? $outformat : "''"));
 
52
}
 
53
 
 
54
my ($in,$out);
 
55
my @extra;
 
56
if( $special ) {
 
57
    @extra = map { my @rc;
 
58
                   if( /nointerleaved/) {
 
59
                       @rc = ('-interleaved' => '0');
 
60
                   } elsif( /mrbayes/ ) {
 
61
                       @rc = ('-show_symbols' => 0,
 
62
                              '-show_endblock' => 0);
 
63
                   } elsif( /(\S+)\=(\S+)/ ) { @rc = ( "-$1" => $2) } 
 
64
                   else{ @rc = ("-$_" => 1) }
 
65
                   @rc;
 
66
               } split(/[\s,]/,$special);
 
67
}
 
68
# guess we're talking about MSA if any of the standard MSA names are used
 
69
if( $informat =~ /nexus|phylip|clustal|maf|stockholm|bl2seq|msf/ ||
 
70
    $outformat =~ /nexus|phylip|clustal|maf|stockholm|bl2seq|msf/ ) {
 
71
    $msa = 1;
 
72
}
 
73
 
 
74
if( $msa ) {
 
75
    eval {
 
76
        if( defined $input ) {
 
77
            $in = new Bio::AlignIO(-format => $informat, -file => $input);
 
78
        } else {
 
79
            $in = new Bio::AlignIO(-format => $informat, -fh => \*ARGV);
 
80
        }
 
81
    };
 
82
    if( $@ ) {
 
83
        die("Unknown MSA format to bioperl $informat\n");
 
84
    }
 
85
    eval {
 
86
        if( $output ) {
 
87
            $out = new Bio::AlignIO(-format => $outformat,
 
88
                                    -file => ">$output", @extra);
 
89
        } else {
 
90
            # default to STDOUT for output
 
91
            $out = new Bio::AlignIO(-format => $outformat,@extra);
 
92
        }
 
93
    };
 
94
    if( $@ ) {
 
95
        die("Unknown MSA format to bioperl $outformat\n");
 
96
    }
 
97
    while( my $aln = $in->next_aln) { 
 
98
         if( $special =~ /flat/ ) {$aln->set_displayname_flat(1); }
 
99
         $out->write_aln($aln) }
 
100
 
 
101
} else {
 
102
    eval {
 
103
        if( defined $input ) {
 
104
            $in = new Bio::SeqIO(-format => $informat, -file => $input);
 
105
        } else { 
 
106
            $in = new Bio::SeqIO(-format => $informat, -fh => \*ARGV);
 
107
        }
 
108
    };
 
109
    if( $@ ) {
 
110
        if( $@ =~ /Could not open/ ) {
 
111
            die("Could not open input file: $input\n");
 
112
        } else { 
 
113
            die("Unknown sequence format to bioperl $informat\n");
 
114
        }       
 
115
    }
 
116
    eval {
 
117
           if( $output ) {
 
118
               $out = new Bio::SeqIO(-format => $outformat,
 
119
                                     -file => ">$output");
 
120
           } else {
 
121
               # default to STDOUT for output
 
122
               $out = new Bio::SeqIO(-format => $outformat);
 
123
           }
 
124
       };
 
125
    if( $@ ) {
 
126
        if( $@ =~ /Could not open/ ) {
 
127
            die("Could not open output file: $output\n");
 
128
        } else { 
 
129
            die("Unknown sequence format to bioperl $outformat: $@\n");
 
130
        }
 
131
    }
 
132
    while( my $seq = $in->next_seq ) {
 
133
        $out->write_seq($seq);
 
134
    }
 
135
}
 
136
 
 
137
=head1 NAME
 
138
 
 
139
bpsreformat - convert sequence formats
 
140
 
 
141
=head1 DESCRIPTION
 
142
 
 
143
This script uses the SeqIO system that allows conversion of sequence
 
144
formats either sequence data or multiple sequence alignment data.  The
 
145
name comes from the fact that Sean Eddy's program sreformat (part of
 
146
the HMMER pkg) already does this.  Sean's program tries to guess the
 
147
input formats while in our code we currently require your to specify what
 
148
the input and output formats are and if the data is from a multiple
 
149
sequence alignment or from straight sequence files.
 
150
 
 
151
Usage:
 
152
 
 
153
bpsreformat -if INFORMAT -of OUTFORMAT -i FILENAME -o output.FORMAT
 
154
 
 
155
  -h/--help        Print this help
 
156
 
 
157
  -if/--informat   Specify the input format
 
158
 
 
159
  -of/--outformat  Specify the output format
 
160
 
 
161
  -i/--input       Specify the input file name
 
162
                   (to pass in data on STDIN use minus sign as filename)
 
163
  -o/--output      Specify the output file name
 
164
                   (to pass data out on STDOUT use minus sign as filename)
 
165
 
 
166
  --msa            Specify this is multiple sequence alignment data
 
167
 
 
168
  --special        Will pass on special parameters to the AlignIO/SeqIO
 
169
                   object -- most of these are for Bio::AlignIO objects
 
170
                   Comma separated list of the following
 
171
                   nointerleaved   -- for phylip,non-interleaved format
 
172
                   idlinebreak     -- for phylip, makes it molphy format
 
173
                   percentages     -- for clustalw, show % id per line
 
174
 
 
175
=cut