Welcome back, Perl (GC content)

I coded in Perl for 1-2 weeks in my life 7 months ago, then shifted to Python –and PHP for sometime– for the previous 7 months. Now, I am back to Perl –somehow! Started by this GC content calculator.

#! /usr/bin/perl -w

use strict;

#Hadling args
#http://lowfatlinux.com/linux-perl-arguments.html
#print “My name is $0 \n”; #Script name
#print “First arg is: $ARGV[0] \n”;
#$num = $#ARGV + 1; print “How many args? $num \n”;
#print “The full argument string was: @ARGV \n”;

#Calc GC contenct
#http://www.cs.tut.fi/~jkorpela/perl/regexp.html
#http://manuelcorpas.com/2010/02/03/a-script-to-calculate-gc-content/

sub calcgc {
my $seq = $_[0];
my $count = 0;
$count++ while ($seq =~ m/[GC]/gi); #contains, case-insens-
my $num = $count / length($seq);
#my ($dec) = $num =~ /(\S{0,6})/;
my $dec = sprintf(“%.3f”, $num);
return $dec;
}

#my $seq = “GCTTGTCGATATGACTACTTCGAGAAATTTATCTCGATTATCTGAGA”;
#my $gc = calcgc($seq);
#print(“$gc\n”);

print(“Welcome back, Perl\n”);

my $a = $ARGV[0];
open(IN, $a) || die “cannot open $a for reading: $!”;
while (<IN>) { # read a line from file $a into $_
#print $_; # print that line to STDOUT
if (/^>/) {
print $_;
}
next if /^>/; #http://stackoverflow.com/questions/4119225/perl-regular-expressions-delete-line-if-it-starts-with
#print $_;
my $gc = calcgc($_);
print(“$gc\n”);
}
close(IN);

Advertisements