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

« back to all changes in this revision

Viewing changes to scripts/seq/split_seq.PLS

  • 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 -w
2
 
use strict;
3
 
 
4
 
=head1 NAME
5
 
 
6
 
split_seq - splits a sequence into equal sized chunks with an optional
7
 
            overlapping range
8
 
 
9
 
=head1 SYNOPSIS
10
 
 
11
 
split_seq -c 10000 [-o 1000] [-i] -f seq.in
12
 
 
13
 
=head1 DESCRIPTION 
14
 
 
15
 
The script will split sequences into chunks
16
 
 
17
 
Mandatory Options:
18
 
 
19
 
  -c  Desired length of the resulting sequences.
20
 
  -f  Input file (must be FASTA format).
21
 
 
22
 
Special Options:
23
 
 
24
 
  -o  Overlapping range between the resulting sequences.
25
 
  -i  Create an index file with the resulting sequence files. This is
26
 
      useful if you want to pass this list as input arguments into
27
 
      another programs (i.e. CLUSTAL, HMMER, etc.).
28
 
 
29
 
=head1 FEEDBACK
30
 
 
31
 
=head2 Mailing Lists
32
 
 
33
 
User feedback is an integral part of the evolution of this and other
34
 
Bioperl modules. Send your comments and suggestions preferably to
35
 
the Bioperl mailing list.  Your participation is much appreciated.
36
 
 
37
 
  bioperl-l@bioperl.org                  - General discussion
38
 
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
39
 
 
40
 
=head2 Reporting Bugs
41
 
 
42
 
Report bugs to the Bioperl bug tracking system to help us keep track
43
 
of the bugs and their resolution. Bug reports can be submitted via the
44
 
web:
45
 
 
46
 
  https://redmine.open-bio.org/projects/bioperl/
47
 
 
48
 
=head1 AUTHORS
49
 
 
50
 
  Ewan Birney E<lt>birney-at-ebi.ac.ukE<gt>
51
 
  Mauricio Herrera Cuadra E<lt>mauricio at open-bio.orgE<gt>
52
 
  (some enhancements)
53
 
 
54
 
=cut
55
 
 
56
 
# Modules, pragmas and variables to use
57
 
use Bio::Seq;
58
 
use Bio::SeqIO;
59
 
use Getopt::Long;
60
 
use vars qw($opt_c $opt_o $opt_i $opt_f $index_file);
61
 
 
62
 
# Gets options from the command line
63
 
GetOptions qw(-c=i -o:i -i -f=s);
64
 
 
65
 
# If no mandatory options are given prints an error and exits
66
 
if (!$opt_c) {
67
 
    print "ERROR: No chunk size has been specified.\n" and exit();
68
 
} elsif (!$opt_f) {
69
 
    print "ERROR: No FASTA file has been specified.\n" and exit();
70
 
}
71
 
 
72
 
# Declares offset size
73
 
my $offset = $opt_o ? $opt_o : "0";
74
 
 
75
 
# Opens the FASTA file
76
 
my $in = Bio::SeqIO->new(
77
 
    -file   => "$opt_f",
78
 
    -format => "Fasta",
79
 
);
80
 
print "==> Opening FASTA file:\t\t\t\t$opt_f\n";
81
 
 
82
 
# Reads the next sequence object
83
 
while (my $seq = $in->next_seq()) {
84
 
 
85
 
    # Reads the ID for the sequence and prints it
86
 
    my $id = $seq->id();
87
 
    print "--> The ID for this sequence is:\t\t$id\n";
88
 
 
89
 
    # Reads the description for the sequence and prints it
90
 
    my $desc = $seq->desc();
91
 
    print "--> The description for this sequence is:\t$desc\n";
92
 
 
93
 
    # Gets sequence length and prints it
94
 
    my $seq_length = $seq->length();
95
 
    print "--> The length of this sequence is:\t\t$seq_length\n";
96
 
 
97
 
    # If the chunk size is bigger than the sequence length prints the error and exits
98
 
    (print "ERROR: Specified chunk size is bigger than sequence length.\n" and exit()) if ($opt_c > $seq_length);
99
 
 
100
 
    # Creates a directory for writing the resulting files
101
 
    mkdir("split", 0755) unless -e "split" and -d "split";
102
 
 
103
 
    # Creates the INDEX file if the option was given
104
 
    if ($opt_i) {
105
 
        $index_file = "$id.c$opt_c.o$offset.INDEX";
106
 
        open(FH, ">", $index_file) or die("Unable to create file: $index_file ($!)");
107
 
    }
108
 
 
109
 
    # Loops through the sequence
110
 
    for (my $i = 1; $i < $seq_length; $i += $opt_c) {
111
 
        my $end = (($i + $opt_c) > $seq_length) ? ($seq_length + 1) : ($i + $opt_c);
112
 
        my $seq_range = (($i + $opt_c) > $seq_length) ? "$i-".($end - 1) : "$i-$end";
113
 
        my $id = $seq->id();
114
 
        $id .= "_$seq_range";
115
 
 
116
 
        # Stores chunk into its corresponding FASTA file
117
 
        my $out = Bio::SeqIO->new(
118
 
            -file   => ">split/$id.faa",
119
 
            -format => "Fasta",
120
 
        );
121
 
        my $trunc_seq = $seq->trunc($i, $end - 1);
122
 
        $trunc_seq->id($id);
123
 
        $out->write_seq($trunc_seq);
124
 
        print "==> Sequence chunk:\t$seq_range\tstored in file:\tsplit/$id.faa\n";
125
 
 
126
 
        # Prints the current file name into the INDEX file if the option was given
127
 
        print FH "split/$id.faa\n" if $opt_i;
128
 
 
129
 
        # Decreases the $i value with the offset value
130
 
        $i -= $offset;
131
 
    }
132
 
 
133
 
    # Closes the INDEX file if the option was given
134
 
    if ($opt_i) {
135
 
        print "==> INDEX stored in file:\t\t\t$index_file\n";
136
 
        close(FH);
137
 
    }
138
 
}
139
 
 
140
 
__END__