Re: Trying to eliminate a loop

• To: mathgroup at smc.vnet.net
• Subject: [mg50604] Re: [mg50595] Trying to eliminate a loop
• From: DrBob <drbob at bigfoot.com>
• Date: Sun, 12 Sep 2004 04:42:15 -0400 (EDT)
• References: <200409111045.GAA19926@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```I haven't gotten to the loop yet, but

Timing[pr=Flatten[First[Extract[freq[[All,2]],Split[Position[freq,Max[freq[[\
All,1]]]][[All,1]]]]]]]

{0.031 Second,{a,c,g,t,t,t,t}}

could be replaced with

Timing[pr=First@freq[[Ordering[freq,-1],-1]]]

{0. Second,{t,t,a,t,a,t,c}}

When I generated a simple, there were 11 ties for the maximum; your code finds the first, and mine finds the last. Given that the sample is random, I doubt you care which one you work with, and Ordering is faster.

Bobby

On Sat, 11 Sep 2004 06:45:09 -0400 (EDT), JÃ¡nos <janos.lobb at yale.edu> wrote:

> Hi,
>
> I have a four letter alphabet:
> In[3]:=
> baseAlphabet={a,t,c,g}
>
> I can create an arbitrary length list from it with:
>
> generateRandomStrand[alphabet_List, len_Integer] := \
> Table[alphabet[[Random[Integer, {1, Length[alphabet]}] ]], {len}];
>
> for example:
>
> ls = generateRandomStrand[baseAlphabet, 23460];
>
> I want to know what kind of primerLength=7  sub-strands are in it, so I
> partition it:
>
> lspr = Partition[ls, primerLength, 1];
>
> All the possible sub-strands form a set:
>
> primerSet=Distribute[Table[baseAlphabet, {primerLength}], List];
>
> << Statistics`DataManipulation`
> freq=Frequencies[lspr];
> gives me a frequency distribution, that is what elements of primerSet
> occur at what frequency in lspr:
>
> To know which sublist occurs the most I do:
> In[64]:=
> pr=Flatten[First[Extract[freq[[All, 2]], Split[Position[freq,
> Max[freq[[All,
>      1]]]][[All, 1]]  ]]  ]]
>
> Out[64]=
> {a,a,c,t,g,c,g}
>
> and it is at positions
>
> In[65]:=
> prpos=Position[lspr,pr]
>
> Out[65]=
> {{2860},{4336},{6791},{11387},{12164},{17472},{17833},{17954}}
>
> in lspr and in ls.
>
> At those positions in ls I want to attach the complement of this pr
> sublist, so I create the following rules:
>
> complementRule = {a -> t, c -> g, t -> a, g -> c};
> replaceWithComplement = {a -> {a, t}, c -> {c, g}, t -> {t, a}, g ->
> {g, c}}
>
> I created a For loop which at prpos replaces the elements there with
> the double elements indicated by the rule above on primerLength
> intervals of ls:
>
> For[i = 1, i Â² Length[pr], i++, ls = ReplacePart[ls,
>      replaceWithComplement, prpos + i - 1, \
> Flatten[Position[replaceWithComplement, {Part[Extract[ls,
>          prpos + i - 1], 1], Part[Extract[
>          ls, prpos + i - 1], 1] /. complementRule}]]] ]
>
> Mathematica does this For loop in about 0.046678 Second on my machine.
> With primerLength=9 it is 0.050748 Second - pr had just three positions
> on the strand.
>
> I have the feeling that it can be done faster with Map or MapAt, so the
> For loop could go away.  I also do not like that I have to rewrite ls
> in every cycle.  ReplacePart does not look good to me in this
> situation, but I have not find yet the way to apply the
> replaceWithComplement  rule directly to the primerLength long intervals
> of ls at prpos positions.
>
> Any good tip ?
>
>
> Jâ?¡nos
>
>
> ----------------------------------------------
> Trying to argue with a politician is like lifting up the head of a
> corpse.
> (S. Lem: His Master Voice)
>
>
>

--
DrBob at bigfoot.com
www.eclecticdreams.net

```

• Prev by Date: Re: DSolve Question
• Next by Date: Re: Re: Smallest enclosing circle
• Previous by thread: Trying to eliminate a loop
• Next by thread: Re: Trying to eliminate a loop