Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^14: how to access elements in perl

by grewal7634 (Novice)
on May 19, 2014 at 05:21 UTC ( #1086583=note: print w/ replies, xml ) Need Help??


in reply to Re^13: how to access elements in perl
in thread how to access elements in perl

hey i have a code (given below) which counts A,T,G,C and GC,and GC percentage in between 0-200, 1-201, 2-202 regions. But i have to count a lenghty sequence upto 10,000 basepairs, so this would be very lenghty and less efficient. What modification can i do in this code so that it can automateically count upto 10,000 bases.

#!/usr/bin/perl use warnings;my$seq;my$file;my$line;my$len;my@seq; print "Select the region where you want to find CpG Island\n"; print "1. Region 1-200\n"; print "2. Region 2-202\n"; print "3. Region 3-203\n"; $choice =<STDIN>; chomp($choice); if($choice == 1){ &region1; } if($choice == 2){ &region2; } if($choice == 3){ &region3; } sub region1 { $file = 'd:\perl\1.txt'; open(INPUT, $file) or die ("File not found"); foreach($line = <INPUT>){ $a = substr($line,0,200) =~tr/A//; $t = substr($line,0,200) =~tr/T//; $g = substr($line,0,200) =~tr/G//; $c = substr($line,0,200) =~tr/C//; $cg = substr($line,0,200) =~tr/ CG //; $total = substr($line,0,200) =~tr/ATGC//; } $CpG = ($cg)/$total*100; print"Count of A = $a\n"; print"Count of T = $t\n"; print"Count of G = $g\n"; print"Count of C = $c\n"; print"CG = $cg\n"; print"total count of ATGC = $total\n"; print"CpG percentage = $CpG\n"; if ($CpG>=60){ print"CpG present";} else{ print"CpG absent";} } sub region2 { $file = 'd:\perl\1.txt'; open(INPUT, $file) or die ("File not found"); foreach($line = <INPUT>){ $a = substr($line,1,201) =~tr/A//; $t = substr($line,1,201) =~tr/T//; $g = substr($line,1,201) =~tr/G//; $c = substr($line,1,201) =~tr/C//; $total = substr($line,1,201) =~tr/ATGC//; } $CpG = ($g+$c)/$total*100; regarding perl,So i need your help for the same. print"Count of A = $a\n"; print"Count of T = $t\n"; print"Count of G = $g\n"; print"Count of C = $c\n"; print"total count of ATGC = $total\n"; print"CpG percentage = $CpG\n"; if ($CpG>=60){ print"CpG present";} else{ print"CpG absent";} } sub region3 { $file = 'd:\perl\1.txt'; open(INPUT, $file) or die ("File not found"); foreach($line = <INPUT>){ $a = substr($line,2,203) =~tr/A//; $t = substr($line,2,203) =~tr/T//; $g = substr($line,2,203) =~tr/G//; $c = substr($line,2,203) =~tr/C//; $total = substr($line,2,203) =~tr/ATGC//; } $CpG = ($g+$c)/$total*100; print"Count of A = $a\n"; print"Count of T = $t\n"; print"Count of G = $g\n"; print"Count of C = $c\n"; print"total count of ATGC = $total\n"; print"CpG percentage = $CpG\n"; if ($CpG>=60){ print"CpG present";} else{ print"CpG absent";} }


Comment on Re^14: how to access elements in perl
Download Code
Re^15: how to access elements in perl
by wjw (Curate) on May 19, 2014 at 08:08 UTC
    Could you provide perhaps a single line or maybe even 2 or 3 of the input file?

    I modified your code a bit for brevity(untested). I am not all that familiar with microbiology(been 35 years since high school biology). I suspect there are CPAN modules that will handle problems like this. Do a search for like this one at bio:: and see if there might be something you can use if that is an option for you.

    Hope that is in some way helpful...

    #!/usr/bin/perl use strict; use warnings; print "Select the region where you want to find CpG Island\n"; my ($a,$t,$c,$g,$cg,$total); print "1. Region 1-200\n"; print "2. Region 2-202\n"; print "3. Region 3-203\n"; my $choice =<STDIN>; chomp($choice); my $file = 'd:\perl\1.txt'; if ( $choice == 1 ) { region(0); } elsif ( $choice == 2 ) { region(1); } elsif ( $choice == 3) { region(2); } sub region { my $start = shift; open(INPUT, $file) or die ("File not found"); foreach(my $line = <INPUT>) { my $a = substr($line,$start,200) =~ tr/A//; my $t = substr($line,$start,200) =~ tr/T//; my $g = substr($line,$start,200) =~ tr/G//; my $c = substr($line,$start,200) =~ tr/C//; my $cg = substr($line,$start,200) =~ tr/ CG //; my $total = substr($line,$start,200) =~ tr/ATGC//; } } my $CpG = ($cg)/$total*100; print"Count of A = $a\n"; print"Count of T = $t\n"; print"Count of G = $g\n"; print"Count of C = $c\n"; print"CG = $cg\n"; print"total count of ATGC = $total\n"; print"CpG percentage = $CpG\n"; if ($CpG>=60) { print"CpG present"; } else { print"CpG absent"; }
    Update: moved printing back outside the sub. What I get for messing with code at this hour of the morning...
    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...

      My input file contains:

      ATGCGCGATGCTAGCTGCAATCGATCGCCGATCGCGATGCGAAATCGAGCGCTGCGCGATGCGATCGATC +GCTGCCGATGCGCGATTACGTCGATCGATCGATCGATGCCGTCAGCTGCATGCTGCGCTACGCTAGCTA +GCTAGCTGACTGATCGTAGCTCGATGCGCGCTAGCTAGCTGCTAGCTAGCTGATCGTGCGATTCGTAGC +TAGTCGATCGATCGATCGATCGATCGATCGATGCATGTCGATCGACTAGCTAGCATCGTAGCATGCATC +TAGCTCGATCGACTAGCTAGCTAGCTGACTGTTCGATCAGCTGGCATGCGCGCTAGCGCGCTAGCGCGC +GATGCGCGAAACTGCTATATAGCCGCGCGATGCGCGCTAGCGACGTAGCTAGCTAGCGCGCATGCTAGC +TAGCTAGCTAGCTCGCTAGCTCGATCAGCTGCGCGCATGCTAGCTTTCGATCGATATCGAGCTAGCTAG +CTAGCTAGCTAGCTTAGCTTAGCTAGCTACGTAGCTACGATGCTACGTAGCGGCTAGCATCGATCGTAG +CTAGCTAGCGATCGACTGATCGATCGACTAGCTAGCTAGCGATCGATCTACGACGAGCTACGATCAACG +TCGCGCTAATATTTACGTCGATCTACGTAGTTATCTACTTATCTATCGATCGATCGATCGTTAGCTAGC +TAGCTAGCTCGATCGATCGATTAATCGATCGATCTACTATGATTATCGCTATTCAGCTATGCGTACTTA +GCATCGCGCGCTATCTTAGCGTGCGATATATTACGGCGCATGCACGTACGTAGCTAGCTAGCTATCGAT +CATGCATGACGCGGCGCGCGATAGTCGATCGCTAGCTGATCGATCGTAGCTATCGATCGTAGCTAGCTA +GC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-26 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls