MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Union, Sort at different levels of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65711] Re: Union, Sort at different levels of a list
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 16 Apr 2006 01:45:17 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <e1nn4v$lm8$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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


  • Prev by Date: Re: Union, Sort at different levels of a list
  • Next by Date: Re: 3D ContourGraphics alternative
  • Previous by thread: Re: Union, Sort at different levels of a list
  • Next by thread: Re: Re: Union, Sort at different levels of a list