forked from ranjit58/NGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-sample-fasta.pl
executable file
·134 lines (110 loc) · 3.35 KB
/
random-sample-fasta.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#FILE: random_sequence_sample.pl
#AUTH: Paul Stothard ([email protected])
#DATE: May 20, 2008
#VERS: 1.0
#USAGE: perl random-sample-fasta.pl [-arguments]
# -i [FILE] : file containing sequences in FASTA format (Required).
# -o [FILE] : output file (Required).
# -n [INTEGER] : number of sequences to obtain (Required).
# -r : sample with replacement (Optional. Default is to
# sample without replacement).
#
#perl random_sequence_sample.pl -i test/sample_in.fasta -o test/sample_out.fasta -n 10
use warnings;
use strict;
use Data::Dumper;
use Getopt::Long;
my %options = (
infile => undef,
outfile => undef,
n => undef,
with_replacment => undef
);
GetOptions(
'i=s' => \$options{infile},
'o=s' => \$options{outfile},
'n=i' => \$options{n},
'r' => \$options{with_replacement}
);
$options{n} = get_integer( $options{n} );
if ( !( defined( $options{infile} ) )
or !( defined( $options{outfile} ) )
or !( defined( $options{n} ) ) )
{
die( print_usage() . "\n" );
}
open( my $INFILE, $options{infile} )
or die("Cannot open file '$options{infile}': $!");
#Determine how many sequences are in the input file
my $count = 0;
while (<$INFILE>) {
if ( $_ =~ m/^>/ ) {
$count++;
}
}
close($INFILE) or die("Cannot close file : $!");
if ( ( $count < $options{n} ) && ( !$options{with_replacement} ) ) {
die("$options{infile} contains less than $options{n} sequences--unable to obtain sample without replacement."
);
}
#Decide which sequences to sample
my $chosen_count = 0;
my @chosen = ();
for ( my $i = 0; $i < $count; $i++ ) {
$chosen[$i] = 0;
}
while ( $chosen_count < $options{n} ) {
my $random = int( rand($count) );
if ( $options{with_replacement} ) {
$chosen[$random]++;
$chosen_count++;
}
elsif ( $chosen[$random] == 0 ) {
$chosen[$random] = 1;
$chosen_count++;
}
}
#Write out sampled sequences
$count = 0;
local $/ = ">";
open( my $OUTFILE, '>', $options{outfile} )
or die("Cannot open file '$options{outfile}': $!");
open( $INFILE, $options{infile} )
or die("Cannot open file '$options{infile}': $!");
while (<$INFILE>) {
if ( $_ =~ m/[^\s>]/ ) {
my @lines = split( /\n/, $_ );
my $title = $lines[0];
my $sequence = $_;
$sequence =~ s/\Q$title\E//;
$sequence =~ s/[^a-zA-Z]//g;
while ( $chosen[$count] > 0 ) {
print {$OUTFILE} ">$title\n$sequence\n";
$chosen[$count]--;
}
$count++;
}
}
close($INFILE) or die("Cannot close file : $!");
close($OUTFILE) or die("Cannot close file : $!");
print "Sequences have been written to '$options{outfile}'\n";
sub get_integer {
my $value = shift;
my $int = undef;
if ( ( defined($value) ) && ( $value =~ m/(\-*\d+)/ ) ) {
$int = $1;
}
return $int;
}
sub print_usage {
print <<BLOCK;
USAGE: perl random_sequence_sample.pl [-arguments]
-i [FILE] : file containing sequences in FASTA format (Required).
-o [FILE] : output file (Required).
-n [INTEGER] : number of sequences to obtain (Required).
-r : sample with replacement (Optional. Default is to
sample without replacement).
perl random_sequence_sample.pl -i test/sample_in.fasta \
-o test/sample_out.fasta -n 10
BLOCK
}