Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how to access elements in perl

by grewal7634 (Novice)
on Apr 28, 2014 at 06:46 UTC ( #1084076=perlquestion: print w/ replies, xml ) Need Help??
grewal7634 has asked for the wisdom of the Perl Monks concerning the following question:

I have a sequence file of 1000 bases containing repeating elements of ATGC, i want to calculate the ATGC in every 200 bases by using a window of 200bases moving across the sequence at 1base intervals. How can i do that. DO you have any code ???

Comment on how to access elements in perl
Re: how to access elements in perl
by choroba (Abbot) on Apr 28, 2014 at 06:56 UTC
    This is not a bioinformatics site, nor a code writing service. Please, try to explain your problem to people who have very limitied knowledge of ACTG. Do you have any code?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to access elements in perl
by davido (Archbishop) on Apr 28, 2014 at 07:10 UTC

    substr, length, and a C-style for loop are the most obvious tools. What have you tried so far?

    You probably haven't had much exposure to the Monastery, but here are some tips:

    We prefer questions that show some initiative. If you just need code written for you, you may find a paid contractor to be more receptive. If you're here to learn, that process starts with you, and we're happy to add our experience and guidance to the efforts you invest.

    We can assist as you hit stumbling blocks. If the stumbling block is "I have no idea how to program or how to use Perl, but I need a solution by next week.", your situation actually isn't as desperate as you might think. Read perlintro, think of how you might solve the problem with pencil and paper, and then get started trying to implement that strategy with Perl.

    When you do post questions, post the code you've got so far so that we can see where help is needed.


    Dave

Re: how to access elements in perl
by BrowserUk (Pope) on Apr 28, 2014 at 07:12 UTC

    The obvious way:

    #! perl -slw use strict; my $dna = do{ local $/; <DATA> }; $dna =~ tr[\n][]d; my %counts; ++$counts{ substr $dna, $_, 1 } for 0 .. 199; for my $offset ( 0 .. length( $dna )-200 ) { printf "range: %d .. %d A:%d C:%d G:%d T:%d\n", $offset, $offset+1 +99, @counts{ qw[ A C G T ] }; --$counts{ substr $dna, $offset, 1 }; ++$counts{ substr $dna, $offset+200, 1 }; } __DATA__ TGCCATCGCGCCAGGAGGGAAGACCAGTTGCTCTGGGTAAATCAGCCGTGAGCGTATCTGTCCCTCTTAT +AATTAGGAGTTTAAACTCTACGATGTTACC ATCCTTGAATCAGGTATATACCGCACTGAAACATCGTCGGCTTTGGGCTCATTCGCTACCCTGTCGCTGA +TATGCGATCTATTCTACATATGCGGGGCCA CCCAATGTCGATTGTCATGAGGAGGGGGTCATAACGACTCACTATAGGACCAGCACGTCCGGTTGCGCTG +AGTAAAATAGAGCTACAAAAGACACAGTAC TCCAAGATGTAAAGGGGAAGACGGGACCTGGGTCGGGGGCGCAACTTCACTGAGCTTTTAACCTTGCGCG +CATGCGAGAGCCTTTCCGCTTAGGAAATGC TGTAAGATCAAGTGGTAGGACTATTGAGAGGTCTTACCTTGCCGCAGCAACGTGGACGGCGACCGGATAA +TTTAAGGCCGACATTCAATATAGAATGTCA GGGAGCAGAACTGTATCCCAAATGCTAGTTGTAGGTGTACGAGCGCACCTGGGGACCTATCGCTCCGACG +GGGTCTGCGTAGCCCATACATCCTGCCAGC ACTGGGGCTATTGAACGGTCAATCCGTAATGTACTCGCTGAATGCTCAGGGATTCCTTAATCTTTGAGCA +CGCGGCTTCTCTCACTTTTCTCACGTCAAC CCTGACTCATAACGGAGTTCGGCAGTCCCGGAACGGCTTATAGAAGCAATGCCTGAGTAGATAGCGTCAG +GGATCGGCCCTACGTCGAGTCCAGTACGCC ATTGGATGGAGGTCTCAGCCGTCGGTGGAGTGCGGCTCCACCCCCACATGAACGAGGTTGTTCCTGACCA +CCCCTGAAACTGTGAGAACCATTGTGTCGA AACCAAGAGGCTTGCGTCGCGCTATAGGTCGATTGCCCCCTAGTTTCCCTATATTGAACGTGTTTCCATT +AAAGACTCTCGGTAAATCTCACGTATTGCA

    Produces:

    C:\test>junk77 range: 0 .. 199 A:46 C:51 G:47 T:56 range: 1 .. 200 A:46 C:52 G:47 T:55 range: 2 .. 201 A:46 C:53 G:46 T:55 range: 3 .. 202 A:46 C:53 G:46 T:55 range: 4 .. 203 A:47 C:52 G:46 T:55 range: 5 .. 204 A:47 C:52 G:46 T:55 range: 6 .. 205 A:47 C:52 G:46 T:55 range: 7 .. 206 A:47 C:51 G:47 T:55 range: 8 .. 207 A:47 C:51 G:46 T:56 range: 9 .. 208 A:47 C:51 G:46 T:56 range: 10 .. 209 A:47 C:51 G:46 T:56 range: 11 .. 210 A:48 C:50 G:46 T:56 range: 12 .. 211 A:48 C:49 G:46 T:57 range: 13 .. 212 A:47 C:49 G:46 T:58 range: 14 .. 213 A:47 C:49 G:46 T:58 range: 15 .. 214 A:47 C:49 G:45 T:59 range: 16 .. 215 A:46 C:50 G:45 T:59 range: 17 .. 216 A:47 C:50 G:44 T:59 ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      You catch more flies with honey than you do with vinegar

      This constant theme that anyone asking a question about Perl, should already know enough to couch that question in Perlish terms -- ie. Must "demonstrate what they've tried" -- is tired, forlorn and counter-productive.

      It's like asking everyone who seeks out driving instruction to provide video of their last fatal accident.

      Expecting everyone who has discovered (or been pointed at) Perl as a possible solution to their occupational task, is like expecting every footballer or lumberjack to realise that mathematics is the key to their occupational tasks. Forlorn, and naive.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        This constant theme ... is tired, forlorn and counter-productive.

        :) So what do you suggest?

        I agree that the tone of "go figure it out yourself and come back with code" that some responders take on posts like these is not very welcoming. However, I think it's a good idea to ask someone to show some effort, in encouraging terms of "we'll help you figure it out" (I like davido's response). I think it helps the learning process, and also, as opposed to just providing a piece of code, it helps weed out those people who are just asking others to do their work for them, and those who will simply copy and paste a piece of code without trying to understand it. At the very least, it'd be nice to know the OP's level of Perl knowledge.

        ...flies and honey...

        I agree with this. There are times when those of us who are other than full time developers simply face a problem which is hard to express.
        Part of the help one needs might be to actually formulate the question.

        There are certainly folks who come here, drop a 'please provide me with a solution', and seem to expect others to work for them. Based on my reading over the past several months, the ratio of actual 'abusers' to said response is far too small. I have on occasion responded by saying 'make some effort' as well. I hope not often... . The tone of some similar responses has been (to my way of thinking) arrogant and just plain pointlessly unfriendly.

        I am reminded of the scene in RoadHouse where the new lead bouncer tells his team members to "..ask them to leave, but be nice, show them the door, but be nice...".

        ...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...
      Thanks, it works good ....
      If we want to calculate G+C and percentage of G+C then how can we do that using this code?
        If we want to calculate G+C

        Add $counts{ C } + $counts{ G }.

        ... and percentage of G+C

        Multiply the above sum by 2.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

      Hey if we have to print only those regions of range which shows count of C more than 50 then what we have to include in this code written above ????

        Hey if we have to print only those regions of range which shows count of C more than 50 then what we have to include in this code written above ????

        Your question tells you everything you need to know:

        if( $counts{ C } > 50 ) { printf "range: %d .. %d A:%d C:%d G:%d T:%d\n", $offset, $offset+199, @counts{ qw[ A C G T ] }; }

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: how to access elements in perl
by sundialsvc4 (Abbot) on Apr 28, 2014 at 16:33 UTC

    Another way to say it might be that the two-lines of the original post really do not give any concrete details as to, for example, what the input file actually looks like, and very little detail as to what the intended algorithm must be.   Of course, bioinformatics is a very specific vertical within which the implied details of such a request might be “fully understood.”   I would not know – it is not my vertical.   It does seem to be yours, BrowserUK.

    With regard to your particular reply, though, I would observe that it is extremely terse and devoid of comments.   There is a lot going-on in that programming, in almost every single line, such that someone who also knows a lot about the Perl language could gaze in (justifiable) wonderment at it.   However, anyone who knew less about Perl might find it difficult or even impossible to follow.   The two statements, alone, which populate $dna in your example are stuffed with “golf.”   I say that, not as any criticism of you, but rather, to point out why the result might well be “blank looks.”   Knowing how to translate that example into the “key ideas” behind it might be even more difficult, according to one’s advance knowledge of Perl in-particular and programming in-general.

    Since the OP says almost-nothing about what he wants, and nothing about what he is starting or ending with, and relies heavily upon the presumed-to-be mutually understood conventions of the bioinformatics vertical, this is a very tricky OP to respond to.   (S)He also says nothing about the OP’s experience with the Perl language, hence ability to undestand and to make meaningful use of a highly-condensed example.   Writing a “good” OP is always a bit of an art, and writing a response to “two-liner” is equally tricky.   The responses in this thread seem to be a combination of good-intentioned requests for further clarification, and an (intrinsically) difficult-to-understand Perl code-example.   Probably, we should shoot for a balance between the two.

      The fact that after 7 years of being around this place you still find yourself "gazing in wonderment" at such a simple piece of code just typifies your attitude.

      You learnt a bit of programming about 25 years ago -- what was it? COBOL? Fortan77? -- and you've never bothered to learn anything new since.

      If the OP wants to learn, he has only to ask for clarification of those things he doesn't understand. If all he's after is solving this particular problem then he can save himself the bother. But if he comes back asking for another freebie, he'll be far less likely to get it a second time.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-11-29 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls