Union, Sort at different levels of a list
- To: mathgroup at smc.vnet.net
- Subject: [mg65680] Union, Sort at different levels of a list
- From: leigh pascoe <leigh at cephb.fr>
- Date: Fri, 14 Apr 2006 04:32:04 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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