I know, this is the most diffucult mode but...
i like this construct and i post it
My task:
I have a gene sequence like
aaaaaxxxxxaaaaaaxxxxxxxxaaaaxxxx
where a=intron x=exons (different regions of a gene)
i use this routine to count the regions a and x
and store it in an array of array:
finally i have something like:
$region[0]=[a, 123]
where the first element is the type and the second element is the length.
the counter use an hash, an array some others data structure to take counts of elemants... here is the code:
sub catch_exons {
my $dna = $_[0];
my @dna = split (//, $dna);
my %seen = ();
my $i=0;
my $a;
my $x;
my @region;
my $nt;
# first split sequemnce in char
# with seen{a} and seen {x} take count of the last letter
# with $a and $x take count of intron and exon length
# @region is a complex data strucure (array of array) that store:
+a or x (intron and exon) and legth
foreach $nt (@dna) {
$i++;
chomp $nt;
if ($nt eq 'a') {
if (!defined $seen{x} and !defined $seen{a}) {
$seen{a} = 0;
$a = 1;
} elsif (!defined $seen{a} and defined $seen{x}) {
$seen{a} = 0; #defined
$seen{x} = undef;
push (@region, ['x',$x]);
$a = 1;
} else {
$a++;
}
}
if ($nt eq 'x') {
if (!defined $seen{a} and !defined $seen{x}) {
$seen{x} = 0;
$x = 1;
}
if (!defined $seen{x}) {
$seen{x} = 0; #defined
$seen{a} = undef;
push (@region , ['a',$a]);
$x = 1;
} else {
$x++;
}
}
}
if (defined $seen{a}) {
push (@region, ['a', $a]);
}
elsif (defined $seen{x}) {
push (@region, ['x', $x]);
}
return (@region);
};
Hope that it will be useful for someone!
20040722 Edit by castaway: Changed title from 'exercise I'
janitored by ybiC: Balanced <readmore> tags around longish codeblock, to reduce scrolling
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.