Greetings Monks,
A very new Perl programmer having a touch of trouble getting a small program to loop through an array.
The file I have read into the array is a .fasta DNA sequence file and has the format:

>Sequence_Header_A
DNAsequence
>Sequence_Header_B
DNAsequence
>Sequence_Header_C
DNAsequence


etc.

I need a program to cycle through the array from top to bottom and perform various steps.
It needs to:
- Recognise a header line and capture the header name
- insert a newline into the DNAsequence at a pre-determined place (the "e" in this example)
- paste the header line before the newline, forming a new entry into the file
- increase a count on the header each time a new entry is made

Each time it finds a new header the count should reset. Hopefully, giving the output seen below:

>Sequence_Header_A_1
DNAs
>Sequence_Header_A_2
equ
>Sequence_Header_A_3
enc
>Sequence_Header_A_4
e
>Sequence_Header_B_1
DNAs
>Sequence_Header_B_2
equ
>Sequence_Header_B_3
enc
>Sequence_Header_B_4
e

I have managed to get the separate elements working (in a fashion) but I don't seem to be able to get it to work as one element (at the moment the "header capture" section and the "insert newline" section are completely independent. My code is as follows:

#Capture header foreach $line (@array) { if ($line =~ /^>.+$/ ) { $header = $&; } }

...and the section which "cuts" the DNA sequence at the appropriate section is:

$number = 1 ; foreach (@array) { #cut at appropriate sites $_ =~ s/(e)/\n$header_$number\n$1/ ; $number++; #remove double ">>" $_ =~ s/\*/\*\n/g; #remove blank lines $_ =~ s/\n+/\n/g ; }

The last 2 steps are just to tidy up the formatting as the output must be exactly in the format of my 2 examples at the top of this post.

Help please!
Until then, I will get my head down and carry on trying!

Thanks
Microkorg.


In reply to Perl noob struggling to loop through an array by microkorg

Title:
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, details, 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, summary, 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.