Sunday, 18 August 2013

Reading the next line in the file and keeping counts separate

Reading the next line in the file and keeping counts separate

Another question for you everyone. To reiterate I am very new to the Perl
process and I apologize in advance for making silly mistakes
I am trying to calculate the GC content of different lengths of DNA
sequence. The file is in this format:
>gene 1
DNA sequence of specific gene
>gene 2
DNA sequence of specific gene
...etc...
I have established the counter and to read each line of DNA sequence but
at the moment it is do a running summation of the total across all lines.
I want it to read each sequence, print the content after the sequence read
then move onto the next one. Having individual base counts for each line.
This is what I have so far.
#!/usr/bin/perl
#necessary code to open and read a new file and create a new one.
use strict;
my $infile = "Lab1_seq.fasta";
open INFILE, $infile or die "$infile: $!";
my $outfile = "Lab1_seq_output.txt";
open OUTFILE, ">$outfile" or die "Cannot open $outfile: $!";
my $G = (0);
my $C = (0);
my $A = (0);
my $T = (0);
while ( my $line = <INFILE> ) {
chomp $line;
if ($line =~ /^>/){
print "$line\n";
}
if ($line =~ /[A-Z]/){
my @array = split //, $line;
my $array= (@array);
foreach $array (@array){
if ($array eq 'G'){
++$G;
}
elsif ( $array eq 'C' ) {
++$C; }
elsif ( $array eq 'A' ) {
++$A; }
elsif ( $array eq 'T' ) {
++$T; }
}
}
}
print "G:=$G\n";
print "C:=$C\n";
Again I feel like I am on the right path, I am just missing some basic
foundations. Any help would be greatly appreciated.

No comments:

Post a Comment