Groupwise Correlation Toy Example


In [43]:
import sys, getopt, math
import itertools as  itt
from scipy.stats.stats import pearsonr
x2reps = ['xa1', 'xa2', 'xb1', 'xb2', 'xc1', 'xc2', 'xd1', 'xd2']
y2reps = ['ya1', 'ya2', 'yb1', 'yb2', 'yc1', 'yc2', 'yd1', 'yd2']
x3reps = ['xa1', 'xa2', 'xa3', 'xb1', 'xb2', 'xb3', 'xc1', 'xc2', 'xc3', 'xd1', 'xd2', 'xd3']
y3reps = ['ya1', 'ya2', 'ya3', 'yb1', 'yb2', 'yb3', 'yc1', 'yc2', 'yc3', 'yd1', 'yd2', 'yd3']
x4reps = ['xa1', 'xa2', 'xa3', 'xa4', 'xb1', 'xb2', 'xb3', 'xb4', 'xc1', 'xc2', 'xc3', 'xc4', 'xd1', 'xd2', 'xd3', 'xd4']
y4reps = ['ya1', 'ya2', 'ya3', 'ya4', 'yb1', 'yb2', 'yb3', 'yb4', 'yc1', 'yc2', 'yc3', 'yc4', 'yd1', 'yd2', 'yd3', 'yd4']
x23reps = ['xa1', 'xa2', 'xb1', 'xb2', 'xc1', 'xc2', 'xc3', 'xd1', 'xd2', 'xd3']
y23reps = ['ya1', 'ya2', 'yb1', 'yb2', 'yc1', 'yc2', 'yc3', 'yd1', 'yd2', 'yd3']

The main helper function splits input arrays into groups


In [61]:
def parse_replicates(data_in, replicates):
    group_data= []
    #data_in = [array of ungrouped replicate data]
    start = 0
    end = replicates[0]
    for i in range(0,len(replicates)):
        group_data.append(data_in[start:end])
        start = end
        if i == len(replicates)-1:
            end = end + replicates[i]
        else:
            end = end + replicates[i+1]
    return group_data

The parse_replicates function requires two arrays: 1) the data, and 2) number of replicates per group.


In [62]:
x2parse = parse_replicates(x2reps, [2,2,2,2])
y2parse = parse_replicates(y2reps, [2,2,2,2])
x3parse = parse_replicates(x3reps, [3,3,3,3])
y3parse = parse_replicates(y3reps, [3,3,3,3])
x4parse = parse_replicates(x4reps, [4,4,4,4])
y4parse = parse_replicates(y4reps, [4,4,4,4])
x23parse = parse_replicates(x23reps, [2,2,3,3])
y23parse = parse_replicates(y23reps, [2,2,3,3])
x23parse


Out[62]:
[['xa1', 'xa2'], ['xb1', 'xb2'], ['xc1', 'xc2', 'xc3'], ['xd1', 'xd2', 'xd3']]

The main wrapper function first calculates within group combinations, then between group combinations. (it currently doesn't contain the code for betweens)


In [110]:
def get_comb(x_in, y_in, xreps, yreps):
    assert len(xreps) == len(yreps)
    xparse = parse_replicates(x_in, xreps)
    yparse = parse_replicates(y_in, yreps)
    xperm = [list(itt.permutations(x, len(x))) for x in xparse]
    yperm = [list(itt.permutations(y, len(y))) for y in yparse]
    within = [[zip(j,k) for j,k in list(itt.product(x,y))[0:]] for x,y in zip(xperm,yperm)]
    groups = []
    for group in within:
        groups.append(group)
    betweens = list(itt.product(*groups))
    return betweens

In [70]:
x2perm = [list(itt.permutations(x, len(x))) for x in x2parse]
y2perm = [list(itt.permutations(y, len(y))) for y in y2parse]
x3perm = [list(itt.permutations(x, len(x))) for x in x3parse]
y3perm = [list(itt.permutations(y, len(y))) for y in y3parse]
x4perm = [list(itt.permutations(x, len(x))) for x in x4parse]
y4perm = [list(itt.permutations(y, len(y))) for y in y4parse]
x23perm = [list(itt.permutations(x, len(x))) for x in x23parse]
y23perm = [list(itt.permutations(y, len(y))) for y in y23parse]
print len(x2perm[0])
print len(x3perm[0])
print len(x4perm[0])
print len(x23perm[2])
x23perm


2
6
24
6
Out[70]:
[[('xa1', 'xa2'), ('xa2', 'xa1')],
 [('xb1', 'xb2'), ('xb2', 'xb1')],
 [('xc1', 'xc2', 'xc3'),
  ('xc1', 'xc3', 'xc2'),
  ('xc2', 'xc1', 'xc3'),
  ('xc2', 'xc3', 'xc1'),
  ('xc3', 'xc1', 'xc2'),
  ('xc3', 'xc2', 'xc1')],
 [('xd1', 'xd2', 'xd3'),
  ('xd1', 'xd3', 'xd2'),
  ('xd2', 'xd1', 'xd3'),
  ('xd2', 'xd3', 'xd1'),
  ('xd3', 'xd1', 'xd2'),
  ('xd3', 'xd2', 'xd1')]]

In [67]:
zip2reps = zip(x2perm, y2perm)
zip23reps = zip(x3perm, y23perm)
zip23reps


Out[67]:
[([('xa1', 'xa2', 'xa3'),
   ('xa1', 'xa3', 'xa2'),
   ('xa2', 'xa1', 'xa3'),
   ('xa2', 'xa3', 'xa1'),
   ('xa3', 'xa1', 'xa2'),
   ('xa3', 'xa2', 'xa1')],
  [('ya1', 'ya2'), ('ya2', 'ya1')]),
 ([('xb1', 'xb2', 'xb3'),
   ('xb1', 'xb3', 'xb2'),
   ('xb2', 'xb1', 'xb3'),
   ('xb2', 'xb3', 'xb1'),
   ('xb3', 'xb1', 'xb2'),
   ('xb3', 'xb2', 'xb1')],
  [('yb1', 'yb2'), ('yb2', 'yb1')]),
 ([('xc1', 'xc2', 'xc3'),
   ('xc1', 'xc3', 'xc2'),
   ('xc2', 'xc1', 'xc3'),
   ('xc2', 'xc3', 'xc1'),
   ('xc3', 'xc1', 'xc2'),
   ('xc3', 'xc2', 'xc1')],
  [('yc1', 'yc2', 'yc3'),
   ('yc1', 'yc3', 'yc2'),
   ('yc2', 'yc1', 'yc3'),
   ('yc2', 'yc3', 'yc1'),
   ('yc3', 'yc1', 'yc2'),
   ('yc3', 'yc2', 'yc1')]),
 ([('xd1', 'xd2', 'xd3'),
   ('xd1', 'xd3', 'xd2'),
   ('xd2', 'xd1', 'xd3'),
   ('xd2', 'xd3', 'xd1'),
   ('xd3', 'xd1', 'xd2'),
   ('xd3', 'xd2', 'xd1')],
  [('yd1', 'yd2', 'yd3'),
   ('yd1', 'yd3', 'yd2'),
   ('yd2', 'yd1', 'yd3'),
   ('yd2', 'yd3', 'yd1'),
   ('yd3', 'yd1', 'yd2'),
   ('yd3', 'yd2', 'yd1')])]

In [36]:
product2reps = list(itt.product(x2reps, y2reps))
product2perms = list(itt.product(x2perm, y2perm))
print len(x2reps)
print len(product2reps)
print len(product2perms)
print product2reps
print product2perms


8
64
16
[('xa1', 'ya1'), ('xa1', 'ya2'), ('xa1', 'yb1'), ('xa1', 'yb2'), ('xa1', 'yc1'), ('xa1', 'yc2'), ('xa1', 'yd1'), ('xa1', 'yd2'), ('xa2', 'ya1'), ('xa2', 'ya2'), ('xa2', 'yb1'), ('xa2', 'yb2'), ('xa2', 'yc1'), ('xa2', 'yc2'), ('xa2', 'yd1'), ('xa2', 'yd2'), ('xb1', 'ya1'), ('xb1', 'ya2'), ('xb1', 'yb1'), ('xb1', 'yb2'), ('xb1', 'yc1'), ('xb1', 'yc2'), ('xb1', 'yd1'), ('xb1', 'yd2'), ('xb2', 'ya1'), ('xb2', 'ya2'), ('xb2', 'yb1'), ('xb2', 'yb2'), ('xb2', 'yc1'), ('xb2', 'yc2'), ('xb2', 'yd1'), ('xb2', 'yd2'), ('xc1', 'ya1'), ('xc1', 'ya2'), ('xc1', 'yb1'), ('xc1', 'yb2'), ('xc1', 'yc1'), ('xc1', 'yc2'), ('xc1', 'yd1'), ('xc1', 'yd2'), ('xc2', 'ya1'), ('xc2', 'ya2'), ('xc2', 'yb1'), ('xc2', 'yb2'), ('xc2', 'yc1'), ('xc2', 'yc2'), ('xc2', 'yd1'), ('xc2', 'yd2'), ('xd1', 'ya1'), ('xd1', 'ya2'), ('xd1', 'yb1'), ('xd1', 'yb2'), ('xd1', 'yc1'), ('xd1', 'yc2'), ('xd1', 'yd1'), ('xd1', 'yd2'), ('xd2', 'ya1'), ('xd2', 'ya2'), ('xd2', 'yb1'), ('xd2', 'yb2'), ('xd2', 'yc1'), ('xd2', 'yc2'), ('xd2', 'yd1'), ('xd2', 'yd2')]
[([('xa1', 'xa2'), ('xa2', 'xa1')], [('ya1', 'ya2'), ('ya2', 'ya1')]), ([('xa1', 'xa2'), ('xa2', 'xa1')], [('yb1', 'yb2'), ('yb2', 'yb1')]), ([('xa1', 'xa2'), ('xa2', 'xa1')], [('yc1', 'yc2'), ('yc2', 'yc1')]), ([('xa1', 'xa2'), ('xa2', 'xa1')], [('yd1', 'yd2'), ('yd2', 'yd1')]), ([('xb1', 'xb2'), ('xb2', 'xb1')], [('ya1', 'ya2'), ('ya2', 'ya1')]), ([('xb1', 'xb2'), ('xb2', 'xb1')], [('yb1', 'yb2'), ('yb2', 'yb1')]), ([('xb1', 'xb2'), ('xb2', 'xb1')], [('yc1', 'yc2'), ('yc2', 'yc1')]), ([('xb1', 'xb2'), ('xb2', 'xb1')], [('yd1', 'yd2'), ('yd2', 'yd1')]), ([('xc1', 'xc2'), ('xc2', 'xc1')], [('ya1', 'ya2'), ('ya2', 'ya1')]), ([('xc1', 'xc2'), ('xc2', 'xc1')], [('yb1', 'yb2'), ('yb2', 'yb1')]), ([('xc1', 'xc2'), ('xc2', 'xc1')], [('yc1', 'yc2'), ('yc2', 'yc1')]), ([('xc1', 'xc2'), ('xc2', 'xc1')], [('yd1', 'yd2'), ('yd2', 'yd1')]), ([('xd1', 'xd2'), ('xd2', 'xd1')], [('ya1', 'ya2'), ('ya2', 'ya1')]), ([('xd1', 'xd2'), ('xd2', 'xd1')], [('yb1', 'yb2'), ('yb2', 'yb1')]), ([('xd1', 'xd2'), ('xd2', 'xd1')], [('yc1', 'yc2'), ('yc2', 'yc1')]), ([('xd1', 'xd2'), ('xd2', 'xd1')], [('yd1', 'yd2'), ('yd2', 'yd1')])]

In [82]:
[[zip(j,k) for j,k in list(itt.product(x,y))[0:((len(x)+len(y))/2)]] for x,y in zip(x2perm,y2perm)]


Out[82]:
[[[('xa1', 'ya1'), ('xa2', 'ya2')], [('xa1', 'ya2'), ('xa2', 'ya1')]],
 [[('xb1', 'yb1'), ('xb2', 'yb2')], [('xb1', 'yb2'), ('xb2', 'yb1')]],
 [[('xc1', 'yc1'), ('xc2', 'yc2')], [('xc1', 'yc2'), ('xc2', 'yc1')]],
 [[('xd1', 'yd1'), ('xd2', 'yd2')], [('xd1', 'yd2'), ('xd2', 'yd1')]]]

In [81]:
#[itt.product(x,y) for x,y in x2zip]
[[zip(j,k) for j,k in list(itt.product(x,y))[0:((len(x)+len(y))/2)]] for x,y in zip(x3perm,y23perm)]


Out[81]:
[[[('xa1', 'ya1'), ('xa2', 'ya2')],
  [('xa1', 'ya2'), ('xa2', 'ya1')],
  [('xa1', 'ya1'), ('xa3', 'ya2')],
  [('xa1', 'ya2'), ('xa3', 'ya1')]],
 [[('xb1', 'yb1'), ('xb2', 'yb2')],
  [('xb1', 'yb2'), ('xb2', 'yb1')],
  [('xb1', 'yb1'), ('xb3', 'yb2')],
  [('xb1', 'yb2'), ('xb3', 'yb1')]],
 [[('xc1', 'yc1'), ('xc2', 'yc2'), ('xc3', 'yc3')],
  [('xc1', 'yc1'), ('xc2', 'yc3'), ('xc3', 'yc2')],
  [('xc1', 'yc2'), ('xc2', 'yc1'), ('xc3', 'yc3')],
  [('xc1', 'yc2'), ('xc2', 'yc3'), ('xc3', 'yc1')],
  [('xc1', 'yc3'), ('xc2', 'yc1'), ('xc3', 'yc2')],
  [('xc1', 'yc3'), ('xc2', 'yc2'), ('xc3', 'yc1')]],
 [[('xd1', 'yd1'), ('xd2', 'yd2'), ('xd3', 'yd3')],
  [('xd1', 'yd1'), ('xd2', 'yd3'), ('xd3', 'yd2')],
  [('xd1', 'yd2'), ('xd2', 'yd1'), ('xd3', 'yd3')],
  [('xd1', 'yd2'), ('xd2', 'yd3'), ('xd3', 'yd1')],
  [('xd1', 'yd3'), ('xd2', 'yd1'), ('xd3', 'yd2')],
  [('xd1', 'yd3'), ('xd2', 'yd2'), ('xd3', 'yd1')]]]

Create combinations of replicates using the wrapper function


In [ ]:
comb2 = get_comb(x2reps, y2reps, [2,2,2,2], [2,2,2,2])
comb3 = get_comb(x3reps, y3reps, [3,3,3,3], [3,3,3,3])
comb4 = get_comb(x4reps, y4reps, [4,4,4,4], [4,4,4,4])
comb23 = get_comb(x23reps, y23reps, [2,2,3,3], [2,2,3,3])
comb323 = get_comb(x3reps, y23reps, [3,3,3,3], [2,2,3,3])
comb2

In [111]:
len(get_comb(x2reps, y2reps, [2,2,2,2], [2,2,2,2]))


Out[111]:
256

In [ ]: