Write a perl script that count the number of letter A C G TU

Write a perl script that count the number of letter A, C, G, T/U in a genetic sequence. Sequence information in the file can be in upper or lower case. Your script should be able to handle either. Do not include newline or carriage-return characters in any of your counts.

For example

Give the following sequence

your output should look like

Solution

$ cat test_count.pl

use strict;
use warnings;

# file for reading the data
my $filename = \'data.txt\';

# open file. If unable to open exit program
open(my $fh, $filename)
or die \"Could not open file \'$filename\' $!\";

# read first entry in file to read inventory id. chomp remove ending carriage return
my $row = <$fh>;
chomp $row;

# get inventory id which is first element before space by spliting first line on space
$row = (split(\" \", $row))[0];

print \"inventory for \\\'$row\\\':\ \";

# initialise all counter to 0
my $a_count = 0;
my $c_count = 0;
my $g_count = 0;
my $tu_count = 0;
my $total_length = 0;

# loop over file untill it is completely read
while ($row = <$fh>) {
chomp $row;
# count number of small or capital A. similarly for others
$a_count += () = $row =~ /a|A/g;
$c_count += () = $row =~ /c|C/g;
$g_count += () = $row =~ /g|G/g;
$tu_count += () = $row =~ /t|T|u|U/g;

# count all charcters
$total_length += length($row);
}

# get count of other charcter
my $other_count = $total_length - $a_count - $c_count - $g_count - $tu_count;

# print details.
print \"A: $a_count\ \";
print \"C: $c_count\ \";
print \"G: $g_count\ \";
print \"T/U: $tu_count\ \";
print \"other characters: $other_count\ \";

# sample input file

$ cat data.txt
441E-590 Nov_16c/Nov16c/441E-590.SEQ trimmed vector-stripped
GCGTCGACGTTCTACGACACGTCGTGCCCCAGGGCTCTGGCCACCATCAAGAGCGGCGTG
GCGGCAGCCGTGAGCAGCGATCCCCGCATGGGCGCCTCCCTGCTCAGGCTGCACTTCCAC
GACTGCTTTGTCCAAGGCTGTGACGCGTCTGTTCTTCTGTCCGGCATGGAACAAAACGCG
GGCAAACAACCAAACCTTGGNCGNAACCNTGGGAACACNNANCGAACGCCCCCAAANGGC
GTTTTCNGAACAAAACGGCCCTTNACTTNCAACCCAAAACCCTTCCCTTGGTCAACAAAA
AAAAAANGGGGGNTTCCCCTTGGGNAACTTCGNGGAAANCCAAANGGNGGGGTTTCTTTT
AAAAACAAAC

#sample run

$ perl -w test_count.pl
inventory for \'441E-590\':
A: 89
C: 111
G: 89
T/U: 64
other characters: 17

Write a perl script that count the number of letter A, C, G, T/U in a genetic sequence. Sequence information in the file can be in upper or lower case. Your scr
Write a perl script that count the number of letter A, C, G, T/U in a genetic sequence. Sequence information in the file can be in upper or lower case. Your scr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site