Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi Perl Monk,

I had tried to add a do-until  loop just after counting bases to the program given below to find the number of motifs and to find lengths between motifs with a <STDIN> input for motif (with 2- 100 letters).

But I didnít get correct results for number of motifs and lengths between any two motifs in either cmd or output text file. I initialized several variables but in vain. I am looking forward to perl monks to help me correct the program in counting motifs(regular expressions) and lengths between motifs in a large string of about 299MB. I want get correct results in cmd as well as in text page (length values in vertical positions in text page).

#!usr/bin/perl use strict; use warnings; if (! @ARGV) { print <<HELP; Usage: > <bases file> HELP exit; } open my $dnaIn, '<', $ARGV[0] or die "Can't open bases file $ARGV[0] +: +$!\n"; my %counts; my @baseList = qw(A T G C); while (defined (my $line = <$dnaIn>)) { chomp $line; ++$counts{$_} for grep {/\S/} split '', $line; } my $bases; my $errors; $bases += $_ for @counts{@baseList}; $errors += $_ for map {$counts{$_}} grep {! /[ATGC]/} keys %counts; print "\n\n Total bases: $bases\n\n"; print join (', ', map {"$_= $counts{$_}"} @baseList), "\n"; print "Errors (N)= $errors\n" if $errors; # In a loop, ask the user for a motif, search for the motif, and repor +t if it # was found. Exit if no motif is entered. my $DNA=join('',@ARGV); my $motif=''; do { print "\n\nEnter a motif to count its number and lengths between motif +s:\n";# $motif = <STDIN>; chomp $motif; # Look for the motif if ( $DNA=~ / $motif/ ) { print "I found the motif!\n\n"; } else { print"I couldn\'t find it.\n\n"; } # Count number of motifs and Count number of nt between two motifs use 5.010; my $string ="@ARGV"; # Remove whitespace $string=~ s/\s//g; my $count= () =$string=~ /$motif/g; print "Number of motifs: $count.\n\n"; say "The inter-motif nt Lengths are:\n"; say length for split/$motif/,$string; my @a=map length,split/$motif/,$string; # Output to a text page my $output="result .txt"; unless (open(RESULT,"> $output")){ print"Cannot open file\"$output\".\n\n"; exit; } print RESULT"\n\n Number of bases: $bases. Errors(N)=$errors.\n Motif: $motif. Number of motifs: $count.\n\n The inter-motif nt Leng +ths are:\n\n @a"; close(RESULT); } until (my $motif =~ /^\s*$/ ); exit;

My input file t.txt has the sequence:


Cmd output is as follows:

Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\user>cd d* C:\Documents and Settings\user\Desktop> t.txt "my" variable $motif masks earlier declaration in same scope at C:\Doc +uments and Settings\user\Desktop\ line 58. Total bases: 36 A= 9, T= 11, G= 6, C= 10 Errors (N)= 3 Enter a motif to count its number and lengths between motifs: AT I couldn't find it. Number of motifs: 0. The inter-motif nt Lengths are: 5 Use of uninitialized value $motif in pattern match (m//) at C:\Documen +ts and Set tings\user\Desktop\ line 27, <STDIN> line 1. C:\Documents and Settings\user\Desktop>

Text Output is as follows:

Number of bases: 36. Errors(N)=3. Motif: AT. Number of motifs: 0. The inter-motif nt Lengths are: 5

Correct results in result.txt file should look something like this:

Number of bases: 39. A=9; T=11; G=6; C=10; Errors(N)=3.

I found the motif.

Motif: AT. Number of motifs: 9.

The inter-motif nt Lengths are:











In reply to Seeking help from Perl Monks for counting regular expressions (motifs) and counting lengths between them in a large string by supriyoch_2008

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2018-05-23 19:38 GMT
    Find Nodes?
      Voting Booth?