Re: Re: Union, Sort at different levels of a list

• To: mathgroup at smc.vnet.net
• Subject: [mg65849] Re: [mg65711] Re: Union, Sort at different levels of a list
• From: leigh pascoe <leigh at cephb.fr>
• Date: Tue, 18 Apr 2006 06:56:45 -0400 (EDT)
• References: <e1nn4v\$lm8\$1@smc.vnet.net> <200604160545.BAA07998@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Jean-Marc Gulliet wrote:
> leigh pascoe wrote:
>
>> Hello Everyone,
>>
>> I am having a problem with the syntax for Sorting at different levels of
>> a list structure. My problem arises in genetics, where an individual
>> locus is characterised by two alleles, say (i,j).
>> If I consider a general mating with parents (i,j) and (k,l) they will
>> have possible offspring (i,k),(i,l),(j,k),and (j,l). I want to generate
>> all the possibilities for a general mating with the offspring and tried
>> the following.
>>
>> combos[i_,j_]:=Flatten[Outer[List,i,j],1]
>> matings[n_]:=
>> Flatten[Table[{{i,j,x,k,l},combos[{i,j},{k,l}]},{i,1,n},{j,1,n},{k,1,n},{l,1,n}],n]
>> In[104]:=Sort[matings[2]]
>> Out[104]=
>> {{{{1,1,x,1,1},{{1,1},{1,1},{1,1},{1,1}}},
>> {{1,1,x,1, 2},{{1,1},{1,2},{1,1},{1,2}}}},
>> {{{1,1,x,2,1},{{1,2},{1,1},{1,2},{1,1}}},
>> {{1,1,x,2,2},{{1,2},{1,2},{1,2},{1,2}}}},
>> {{{1,2,x,1,1},{{1,1},{1,1},{2,1},{2,1}}},
>> {{1,2,x,1,2},{{1,1},{1,2},{2,1},{2,2}}}},
>> {{{1,2,x,2,1},{{1,2},{1,1},{2,2},{2,1}}},
>> {{1,2,x,2,2},{{1,2},{1,2},{2,2},{2,2}}}},
>> {{{2,1,x,1,1},{{2,1},{2,1},{1,1},{1,1}}},
>> {{2,1,x,1,2},{{2,1},{2,2},{1,1},{1,2}}}},
>> {{{2,1,x,2,1},{{2,2},{2,1},{1,2},{1,1}}},
>> {{2,1,x,2,2},{{2,2},{2,2},{1,2},{1,2}}}},
>> {{{2,2,x,1,1},{{2,1},{2,1},{2,1},{2,1}}},
>> {{2,2,x,1,2},{{2,1},{2,2},{2,1},{2,2}}}},
>> {{{2,2,x,2,1},{{2,2},{2,1},{2,2},{2,1}}},
>> {{2,2,x,2,2},{{2,2},{2,2},{2,2},{2,2}}}}}
>>
>> This gives me the full list of possible parents and offspring (the x's
>> are for visual clarity only). However I would like to simplify it. In
>> general it is assumed that (i,j) is the same as (j,i), i.e. these are
>> sets of alleles. It is also usually assumed that the parental
>> combination (i,j),(j,k) is the same as (j,k),(i,j). The obvious function
>> to use is Union after Sorting to reduce the number of classes, but I am
>> having trouble getting the syntax correct for applying the functions at
>> the appropriate level.
>>
>> I tried another approach as follows
>>
>> parents[n_]:=
>> Flatten[Table[{Sort[{i,j}],Sort[{k,l}]},{i,1,n},{j,1,n},{k,1,n},{l,1,n}],3]
>> parents[2]
>> Out[101]=
>> {{{1,1},{1,1}},{{1,1},{1,2}},{{1,1},{1,2}},{{1,1},{2,2}},{{1,2},{1,1}},{{1,
>>       2},{1,2}},{{1,2},{1,2}},{{1,2},{2,2}},{{1,2},{1,1}},{{1,2},{1,2}},{{1,
>>       2},{1,2}},{{1,2},{2,2}},{{2,2},{1,1}},{{2,2},{1,2}},{{2,2},{1,2}},{{2,
>>       2},{2,2}}}
>> It gives the parental combinations as a nested list with the individual
>> genotypes (doublets) sorted. Then
>>
>> In[105]:=offspring[{i_,j_},{k_,l_}]:={{i,k},{i,l},{j,k},{j,l}}
>> offspring[{1,2},{3,4}]
>> Out[106]={{1,3},{1,4},{2,3},{2,4}}
>>
>> works for a given set of parents, but how can I thread it over the list
>> of all parents pairs? And then apply Union to produce, for example in
>> the two allele case, the parents with their offspring as
>>
>> {{{1,1},{1,1}}, >{{1,1}}
>> {{1,1},{1,2}}, > {{1,1},{1,2}}
>> {{1,2},{1,2}}, > {{1,1},{1,2},{2,2}
>> {{1,2},{2,2}}, > {{1,2},{2,2}}
>> {{2,2},{2,2}}} > {{2,2}}
>>
>> (I typed this in, the format is unimportant). This is the form that one
>> normally sees the data in. Eventually I am aiming at writing a
>> likelihood equation, but I am having trouble just listing all the
>> possibilities. Any help much appreciated.
>>
>> LP
>>
>>
>>
>>
> Hi Leigh,
>
> The following expression should do what you are looking for. Note that I
> have changed 'n' by 3 in the last line of the function matings.
>
> In[1]:=
> combos[i_, j_] := Flatten[Outer[List, i, j], 1]
> matings[n_] := Flatten[Table[{{{i, j}, {k, l}},
>       combos[{i, j}, {k, l}]}, {i, 1, n}, {j, 1, n},
>      {k, 1, n}, {l, 1, n}], 3]
>
> In[3]:=
> Union[({Sort[Sort /@ #1[[1]]],
>       Union[Sort /@ #1[[2]]]} & ) /@ matings[2],
>    SameTest -> (#1[[1]] == #2[[1]] & )]
>
> Out[3]=
> {{{{1, 1}, {1, 1}}, {{1, 1}}}, {{{1, 1}, {1, 2}},
>     {{1, 1}, {1, 2}}}, {{{1, 1}, {2, 2}}, {{1, 2}}},
>    {{{1, 2}, {1, 2}}, {{1, 1}, {1, 2}, {2, 2}}},
>    {{{1, 2}, {2, 2}}, {{1, 2}, {2, 2}}},
>    {{{2, 2}, {2, 2}}, {{2, 2}}}}
>
> Best regards,
> Jean-Marc
>
>
>
Thanks everyone for the different solutions to my problem.

Jean-Marc, your solution also works. I had tried to use Union with
SameTest, but couldn't get the syntax right (there are no examples in
the help). However it is interesting that in your solution this test is
apparently not needed!

In[5]:=Union[({Sort[Sort/@#1[[1]]],Union[Sort/@#1[[2]]]}&)/@matings[2],
SameTest\[Rule](#1[[1]]\[Equal]#2[[1]]&)]==
Union[({Sort[Sort/@#1[[1]]],Union[Sort/@#1[[2]]]}&)/@matings[2]]

Out[5]=True

```

• Prev by Date: Re: Re: Problem with limiits
• Next by Date: Re: Simplifying equations for Mathematica
• Previous by thread: Re: Union, Sort at different levels of a list