In [2]:
import numpy as np
import skbio.stats.composition as cmpstn

subcompositional coherence


In [9]:
# create communities with absolute abundances

## community 1
comm1 = np.array([500.0, 300.0, 200.0, 10.0, 10.0, 5.0, 5.0, 5.0, 5.0])

## commuity 2 (1st taxon has higher counts, but the others are the same)
comm2 = np.copy(comm1)
comm2[0] = comm2[0] + 1000.0

## commuity 3 (1st taxon has higher counts, but the others are the same)
comm3 = np.copy(comm1)
comm3 = comm3[comm3 > 5]


print comm1
print comm2
print comm3


[ 500.  300.  200.   10.   10.    5.    5.    5.    5.]
[ 1500.   300.   200.    10.    10.     5.     5.     5.     5.]
[ 500.  300.  200.   10.   10.]

In [10]:
# tranform absolute to relative abundace

comm1_rel = comm1 / np.sum(comm1)
comm2_rel = comm2 / np.sum(comm2)
comm3_rel = comm3 / np.sum(comm3)

print comm1
print comm2
print comm3


[ 500.  300.  200.   10.   10.    5.    5.    5.    5.]
[ 1500.   300.   200.    10.    10.     5.     5.     5.     5.]
[ 500.  300.  200.   10.   10.]

In [11]:
# pull out subset of taxa
#subs = np.arange(1,9)
comm1_sub = comm1[np.arange(1,comm1.shape[0])] 
comm2_sub = comm2[np.arange(1,comm2.shape[0])] 
comm3_sub = comm3[np.arange(1,comm3.shape[0])] 

print comm1_sub 
print comm2_sub 
print comm3_sub


[ 300.  200.   10.   10.    5.    5.    5.    5.]
[ 300.  200.   10.   10.    5.    5.    5.    5.]
[ 300.  200.   10.   10.]

In [12]:
# performing closure
comm1_sub_cls = comm1_sub / np.sum(comm1_sub)
comm2_sub_cls = comm2_sub / np.sum(comm2_sub)
comm3_sub_cls = comm3_sub / np.sum(comm3_sub)

print comm1_sub_cls
print comm2_sub_cls
print comm3_sub_cls


[ 0.55555556  0.37037037  0.01851852  0.01851852  0.00925926  0.00925926
  0.00925926  0.00925926]
[ 0.55555556  0.37037037  0.01851852  0.01851852  0.00925926  0.00925926
  0.00925926  0.00925926]
[ 0.57692308  0.38461538  0.01923077  0.01923077]

In [13]:
# overlapping taxa

print comm1_sub_cls[np.arange(4)]
print comm2_sub_cls[np.arange(4)]
print comm3_sub_cls[np.arange(4)]


[ 0.55555556  0.37037037  0.01851852  0.01851852]
[ 0.55555556  0.37037037  0.01851852  0.01851852]
[ 0.57692308  0.38461538  0.01923077  0.01923077]

In [14]:
# ratios of abundances

print '\nAbundance ratios of subset taxa (absolute abundances)'
print comm1[1] /  comm1[4]
print comm2[1] /  comm2[4]
print comm3[1] /  comm3[4]

print '\nAbundance ratios of subset taxa (relative abundances)'
print comm1_sub[0] /  comm1_sub[3]
print comm2_sub[0] /  comm2_sub[3]
print comm3_sub[0] /  comm3_sub[3]

print '\nAbundance ratios of subset taxa after transforming by the sum of the subset'
print comm1_sub_cls[0] /  comm1_sub_cls[3]
print comm2_sub_cls[0] /  comm2_sub_cls[3]
print comm3_sub_cls[0] /  comm3_sub_cls[3]


Abundance ratios of subset taxa (absolute abundances)
30.0
30.0
30.0

Abundance ratios of subset taxa (relative abundances)
30.0
30.0
30.0

Abundance ratios of subset taxa after transforming by the sum of the subset
30.0
30.0
30.0

In [19]:
print comm1_sub_cls[0] /  comm2_sub_cls[0]
print comm1_sub_cls[0] /  comm3_sub_cls[0]
print comm2_sub_cls[0] /  comm3_sub_cls[0]


1.0
0.962962962963
0.962962962963

Notes

  • As the example illustrates, the subset relative abundances (post-transformation) are equivalent to the ratios of absolute abundances