Skip to content

Commit

Permalink
added reverse-complement script
Browse files Browse the repository at this point in the history
  • Loading branch information
gringer committed May 1, 2016
1 parent b8fdba6 commit 3eb4d6a
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions fastx-rc.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/perl
use warnings;
use strict;

## fastx-rc -- reverse complement a sequence

use Getopt::Long qw(:config auto_help pass_through);

my $quiet = 0;

sub rc {
my ($seq) = @_;
$seq =~ tr/ACGTUYRSWMKDVHBXN-/TGCAARYSWKMHBDVXN-/;
# work on masked sequences as well
$seq =~ tr/acgtuyrswmkdvhbxn/tgcaaryswkmhbdvxn/;
return(scalar(reverse($seq)));
}

GetOptions("quiet!" => \$quiet) or
die("Error in command line arguments");

# unknown commands are treated as identifiers
my @files = ();
while(@ARGV){
my $arg = shift(@ARGV);
if(-f $arg){
push(@files, $arg);
}
}
@ARGV = @files;

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){
if($qual){
printf("@%s [RC]\n%s\n+\n%s\n", $seqID, rc($seq), scalar(reverse($qual)));
} else {
printf(">%s [RC]\n%s\n", $seqID, rc($seq));
}
}
$seq = "";
$qual = "";
$seqID = $newSeqID;
} elsif(/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if(length($qual) >= length($seq)){
$inQual = 0; # false
}
}
}

if($seqID){
if($qual){
printf("@%s [rc]\n%s\n+\n%s\n", $seqID, rc($seq), scalar(reverse($qual)));
} else {
printf(">%s [rc]\n%s\n", $seqID, rc($seq));
}
}

0 comments on commit 3eb4d6a

Please sign in to comment.