Question

I'm trying to calculate the percentage of certain characters in a string from a file that is in FASTA format. So the file looks like this;

>label
sequence
>label
sequence
>label
sequence

I'm trying to calculate the percentage of specific characters (e.g G's) from the "sequence" strings. After calculating that (which I have been able to do), I'm trying to print a sentence that says: "The percentage of G's in (e.g.) label 1 is (e.g)53%".

So my question really is, how do I do a calculation on the sequence strings and then name each one in its corresponding output by the label above it?

The code I have so far works out the percentage but I have no way of identifying it.

#!/usr/bin/perl 
use strict; 

# opens file
my $infile = "Lab1_seq.fasta.txt";
open INFILE, $infile or die "$infile: $!\n";

# reads each line
while (my $line = <INFILE>){ 
    chomp $line;

    #creates an array
    my @seq = split (/>/, $line);

    # Calculates percent
    if ($line !~ />/){
        my $G = ($line =~ tr/G//);
        my $C = ($line =~ tr/C//);
        my $total = $G + $C;
        my $length = length($line);
        my $percent = ($total / $length) * 100;

        #prints the percentage of G's and C's for label is x%
        print "The percentage of G's and C's for @seq[1] is $percent\n";
    }
    else{

    }
}

close INFILE

It spits out this output (below) when I'm really trying to get it to also say the name of each label that corresponds to the sequence

The percentage of G's and C's for  is 53.4868841970569
The percentage of G's and C's for  is 52.5443110348771
The percentage of G's and C's for  is 50.8746355685131
Was it helpful?

Solution

You just need to match your label and save that in a variable:

my $label;

# reads each line
while (my $line = <INFILE>){ 
    ...

    if ($line =~ />(.*)/){
        $label = $1;

    # Calculates percent
    } else{
        ...
        print "The percentage of G's and C's for $label, @seq[1] is $percent\n";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top