-
Notifications
You must be signed in to change notification settings - Fork 15
/
shufflefastx.pl
executable file
·121 lines (110 loc) · 2.53 KB
/
shufflefastx.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(:config auto_help pass_through);
use List::Util qw(shuffle);
my $idFileName = "";
my $quiet = 0;
my $wrap = 1;
GetOptions("idfile=s" => \$idFileName, "quiet!" => \$quiet,
"wrap!" => \$wrap) or
die("Error in command line arguments");
my %idsToGet = ();
# unknown commands are treated as identifiers
my @files = ();
while(@ARGV){
my $arg = shift(@ARGV);
if(-f $arg){
push(@files, $arg);
} else {
$idsToGet{$arg} = 1;
}
}
@ARGV = @files;
if($idFileName){
# read sequence IDs from input file
open(my $idFile, "<", $idFileName);
while(<$idFile>){
chomp;
s/^[>@]//;
$idsToGet{$_} = 1;
}
close($idFile);
}
my $numToFind = scalar(keys(%idsToGet));
if(!$quiet && $numToFind){
printf(STDERR "Read %d identifiers\n", $numToFind);
}
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
while(<>){
chomp;
chomp;
if(!$inQual){
if(/^(>|@)((.+?)( .*?\s*)?)$/){
my $newSeqID = $2;
my $newShortID = $3;
if($seqID){
my $newSeq = "";
my @shuffleLocs = (shuffle (0 .. length($seq)));
foreach my $rp (@shuffleLocs){
$newSeq .= substr($seq,$rp,1);
}
if($qual){
my $newQual = "";
foreach my $rp (@shuffleLocs){
$newQual .= substr($qual,$rp,1);
}
printf("@"."shuffled_%s\n%s\n+\n%s\n", $seqID, $newSeq, $qual);
} else {
if($wrap){
$newSeq =~ s/(.{70})/$1\n/g;
$newSeq =~ s/\s+$//;
}
printf(">shuffled_%s\n%s\n", $seqID, $newSeq);
}
}
$seq = "";
$qual = "";
if(!$numToFind ||
exists($idsToGet{$newSeqID}) || exists($idsToGet{$newShortID})){
$seqID = $newSeqID;
} else {
$seqID = "";
}
} elsif(/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if(length($qual) >= length($seq)){
$inQual = 0; # false
}
}
}
if ($seqID) {
my $newSeq = "";
my @shuffleLocs = (shuffle (0 .. length($seq)));
foreach my $rp (@shuffleLocs) {
$newSeq .= substr($seq,$rp,1);
}
if ($qual) {
my $newQual = "";
foreach my $rp (@shuffleLocs) {
$newQual .= substr($qual,$rp,1);
}
printf("@"."shuffled_%s\n%s\n+\n%s\n", $seqID, $newSeq, $qual);
} else {
if ($wrap) {
$newSeq =~ s/(.{70})/$1\n/g;
$newSeq =~ s/\s+$//;
}
printf(">shuffled_%s\n%s\n", $seqID, $newSeq);
}
}