Goal

  • Trying varying levels of bandwidth and DBL scaling with pre-fractionation abundances ('DBL-comm')
  • Varying parameters
    • bandwidth (bw)
      • 0.2, 0.6, 1
    • diffusive boundary layer (DBL) scaling (DBL scaling by abundance)
      • 0.15, 0.2, 0.25
  • This notebook is the same as rep3_DBL-comm_bw but more narrow parameter ranges

Init


In [1]:
import os
import glob
import re
import nestly

In [2]:
%load_ext rpy2.ipython
%load_ext pushnote

In [3]:
%%R
library(ggplot2)
library(dplyr)
library(tidyr)
library(gridExtra)
library(phyloseq)


/opt/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py:106: UserWarning: 
Attaching package: ‘dplyr’


  res = super(Function, self).__call__(*new_args, **new_kwargs)
/opt/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py:106: UserWarning: The following objects are masked from ‘package:stats’:

    filter, lag


  res = super(Function, self).__call__(*new_args, **new_kwargs)
/opt/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py:106: UserWarning: The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union


  res = super(Function, self).__call__(*new_args, **new_kwargs)

BD min/max


In [4]:
%%R
## min G+C cutoff
min_GC = 13.5
## max G+C cutoff
max_GC = 80
## max G+C shift
max_13C_shift_in_BD = 0.036


min_BD = min_GC/100.0 * 0.098 + 1.66    
max_BD = max_GC/100.0 * 0.098 + 1.66    

max_BD = max_BD + max_13C_shift_in_BD

cat('Min BD:', min_BD, '\n')
cat('Max BD:', max_BD, '\n')


Min BD: 1.67323 
Max BD: 1.7744 

Nestly

  • assuming fragments already simulated

In [6]:
workDir = '/home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/'
buildDir = os.path.join(workDir, 'rep4_DBL-comm_bw_HR')
R_dir = '/home/nick/notebook/SIPSim/lib/R/'

fragFile = '/home/nick/notebook/SIPSim/dev/bac_genome1147/validation/ampFrags_kde_parsed.pkl'
commFile = '/home/nick/notebook/SIPSim/dev/fullCyc/fullCyc_12C-Con_trm_comm.txt'

# emperical data for validation
emp_shan_file = '/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_shan.txt'
emp_BDspan_file = '/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_trm_BD-span.txt'
emp_corr_file = '/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_trm_corr.txt'

nreps = 4

In [7]:
# building tree structure
nest = nestly.Nest()

# varying params
nest.add('DBL_scaling', [0.15, 0.2, 0.25])
nest.add('bandwidth', [0.2, 0.6, 1])
nest.add('rep', [x + 1 for x in xrange(nreps)])


## set params
nest.add('abs', ['1e9'], create_dir=False)
nest.add('percIncorp', [0], create_dir=False)
nest.add('percTaxa', [0], create_dir=False)
nest.add('np', [6], create_dir=False)
nest.add('subsample_dist', ['lognormal'], create_dir=False)
nest.add('subsample_mean', [9.432], create_dir=False)
nest.add('subsample_scale', [0.5], create_dir=False)
nest.add('subsample_min', [10000], create_dir=False)
nest.add('subsample_max', [30000], create_dir=False)

### input/output files
nest.add('buildDir', [buildDir], create_dir=False)
nest.add('R_dir', [R_dir], create_dir=False)
nest.add('fragFile', [fragFile], create_dir=False)
nest.add('commFile', [commFile], create_dir=False)


# building directory tree
nest.build(buildDir)

# bash file to run
bashFile = os.path.join(buildDir, 'SIPSimRun.sh')

In [8]:
%%writefile $bashFile
#!/bin/bash

export PATH={R_dir}:$PATH

echo '#-- SIPSim pipeline --#'

echo '# shuffling taxa in comm file'
comm_shuffle_taxa.r {commFile} > comm.txt

    
echo '# adding diffusion'    
SIPSim diffusion \
    {fragFile} \
    --bw {bandwidth} \
    --np {np} \
    > ampFrags_KDE_dif.pkl    

echo '# adding DBL contamination; abundance-weighted smearing'
SIPSim DBL \
    ampFrags_KDE_dif.pkl \
    --comm comm.txt \
    --commx {DBL_scaling} \
    --bw {bandwidth} \
    --np {np} \
    > ampFrags_KDE_dif_DBL.pkl
    
echo '# making incorp file'
SIPSim incorpConfigExample \
  --percTaxa {percTaxa} \
  --percIncorpUnif {percIncorp} \
  > {percTaxa}_{percIncorp}.config

echo '# adding isotope incorporation to BD distribution'
SIPSim isotope_incorp \
    ampFrags_KDE_dif_DBL.pkl \
    {percTaxa}_{percIncorp}.config \
    --comm comm.txt \
    --bw {bandwidth} \
    --np {np} \
    > ampFrags_KDE_dif_DBL_inc.pkl

echo '# simulating gradient fractions'
SIPSim gradient_fractions \
    comm.txt \
    > fracs.txt 

echo '# simulating an OTU table'
SIPSim OTU_table \
    ampFrags_KDE_dif_DBL_inc.pkl \
    comm.txt \
    fracs.txt \
    --abs {abs} \
    --np {np} \
    > OTU_abs{abs}.txt
    
#-- w/ PCR simulation --#
echo '# simulating PCR'
SIPSim OTU_PCR \
    OTU_abs{abs}.txt \
    > OTU_abs{abs}_PCR.txt    
    
echo '# subsampling from the OTU table (simulating sequencing of the DNA pool)'
SIPSim OTU_subsample \
    --dist {subsample_dist} \
    --dist_params mean:{subsample_mean},sigma:{subsample_scale} \
    --min_size {subsample_min} \
    --max_size {subsample_max} \
    OTU_abs{abs}_PCR.txt \
    > OTU_abs{abs}_PCR_sub.txt
        
echo '# making a wide-formatted table'
SIPSim OTU_wideLong -w \
    OTU_abs{abs}_PCR_sub.txt \
    > OTU_abs{abs}_PCR_sub_w.txt
    
echo '# making metadata (phyloseq: sample_data)'
SIPSim OTU_sampleData \
    OTU_abs{abs}_PCR_sub.txt \
    > OTU_abs{abs}_PCR_sub_meta.txt
    

#-- w/out PCR simulation --#    
echo '# subsampling from the OTU table (simulating sequencing of the DNA pool)'
SIPSim OTU_subsample \
    --dist {subsample_dist} \
    --dist_params mean:{subsample_mean},sigma:{subsample_scale} \
    --min_size {subsample_min} \
    --max_size {subsample_max} \
    OTU_abs{abs}.txt \
    > OTU_abs{abs}_sub.txt
        
echo '# making a wide-formatted table'
SIPSim OTU_wideLong -w \
    OTU_abs{abs}_sub.txt \
    > OTU_abs{abs}_sub_w.txt
    
echo '# making metadata (phyloseq: sample_data)'
SIPSim OTU_sampleData \
    OTU_abs{abs}_sub.txt \
    > OTU_abs{abs}_sub_meta.txt    
    
    
    
#-- making summary tables --#
# PCR
shannon_calc.r OTU_abs{abs}_PCR_sub.txt > OTU_abs{abs}_PCR_sub_shan.txt
BD_span_calc.r OTU_abs{abs}_PCR_sub.txt comm.txt > OTU_abs{abs}_PCR_sub_BD-span.txt
correlogram_make.r OTU_abs{abs}_PCR_sub.txt > OTU_abs{abs}_PCR_sub_corr.txt    
# no PCR
shannon_calc.r OTU_abs{abs}_sub.txt > OTU_abs{abs}_sub_shan.txt
BD_span_calc.r OTU_abs{abs}_sub.txt comm.txt > OTU_abs{abs}_sub_BD-span.txt
correlogram_make.r OTU_abs{abs}_sub.txt > OTU_abs{abs}_sub_corr.txt


Writing /home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/rep4_DBL-comm_bw_HR/SIPSimRun.sh

In [ ]:
!chmod 777 $bashFile
!cd $workDir; \
    nestrun  --template-file $bashFile -d rep4_DBL-comm_bw_HR --log-file log.txt -j 3


2016-03-07 13:26:01,697 * INFO * Template: ./SIPSimRun.sh
2016-03-07 13:26:01,699 * INFO * [94124] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/0.6/3
2016-03-07 13:26:01,701 * INFO * [94125] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/0.6/2
2016-03-07 13:26:01,702 * INFO * [94127] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/0.6/1
2016-03-07 13:49:11,263 * WARNING * [94124] rep4_DBL-comm_bw_HR/0.2/0.6/3 Finished with non-zero exit status 1
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted
Warning messages:
1: replacing previous import by ‘scales::alpha’ when loading ‘phyloseq’ 
2: replacing previous import by ‘ggplot2::Position’ when loading ‘DESeq2’ 
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted

2016-03-07 13:49:11,316 * INFO * [98589] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/0.6/4
2016-03-07 13:53:25,191 * INFO * [94127] rep4_DBL-comm_bw_HR/0.2/0.6/1 Finished with 0
2016-03-07 13:53:25,202 * INFO * [99459] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/1/3
2016-03-07 14:11:55,946 * INFO * [98589] rep4_DBL-comm_bw_HR/0.2/0.6/4 Finished with 0
2016-03-07 14:11:56,030 * INFO * [101119] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/1/2
2016-03-07 14:16:14,021 * INFO * [99459] rep4_DBL-comm_bw_HR/0.2/1/3 Finished with 0
2016-03-07 14:16:14,050 * INFO * [102189] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/1/1
2016-03-07 14:30:01,939 * WARNING * [101119] rep4_DBL-comm_bw_HR/0.2/1/2 Finished with non-zero exit status 1
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted
Warning messages:
1: replacing previous import by ‘scales::alpha’ when loading ‘phyloseq’ 
2: replacing previous import by ‘ggplot2::Position’ when loading ‘DESeq2’ 
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted

2016-03-07 14:30:01,988 * INFO * [102712] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/1/4
2016-03-07 14:35:49,741 * WARNING * [102189] rep4_DBL-comm_bw_HR/0.2/1/1 Finished with non-zero exit status 1
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted
Warning messages:
1: replacing previous import by ‘scales::alpha’ when loading ‘phyloseq’ 
2: replacing previous import by ‘ggplot2::Position’ when loading ‘DESeq2’ 
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted

2016-03-07 14:35:49,859 * INFO * [103072] Started ./SIPSimRun.sh in rep4_DBL-comm_bw_HR/0.2/0.2/3
2016-03-08 07:35:31,106 * WARNING * [94125] rep4_DBL-comm_bw_HR/0.2/0.6/2 Finished with non-zero exit status 1
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted
Warning messages:
1: replacing previous import by ‘scales::alpha’ when loading ‘phyloseq’ 
2: replacing previous import by ‘ggplot2::Position’ when loading ‘DESeq2’ 
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: load_simulated -> %>% -> eval -> eval -> read.delim -> read.table
Execution halted

Notes

  • Errors due to memory limitations
  • re-ran these simulations

Comparing to emperical data

  • correlation/regression analyses of metrics on community composition

In [12]:
%%R

# function for loading dataset files
load.data.files = function(sim.files, emp.file){
    # loading
    ## simulations
    df = list()
    for(x in sim.files){
        # simulation
        tmp = read.delim(x, sep='\t')
        xx = strsplit(x, '/')[[1]]
        tmp$DBL_scale = xx[10] %>% as.numeric
        tmp$bw = xx[11] %>% as.numeric
        tmp$SIM_rep = xx[12] %>% as.numeric  
        tmp$dataset = 'Simulation'       
        df[[x]] = tmp 
        
        # emperical (matched for each simulation)
        if(xx[12] %>% as.numeric == 1){
            tmp = read.delim(emp.file, sep='\t')
            tmp$DBL_scale = xx[10] %>% as.numeric
            tmp$bw = xx[11] %>% as.numeric
            tmp$SIM_rep = 1
            tmp$dataset = 'Emperical'        
            xy = paste0(x, '_EMP')
            df[[xy]] = tmp
        }
    }
    df = do.call(rbind, df) %>% as.data.frame 
    rownames(df) = 1:nrow(df)

    # return
    return(df)
    }

Shannon index


In [35]:
sim_shan_files = !find $buildDir -name "OTU_abs1e9_PCR_sub_shan.txt"
print len(sim_shan_files)
print emp_shan_file


36
/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_shan.txt

In [36]:
%%R -i sim_shan_files -i emp_shan_file

df.shan = load.data.files(sim_shan_files, emp_shan_file) 
df.shan %>% tail(n=3)


     library      sample                            OTU Buoyant_density
2166       1 1.760-1.766 Acaryochloris_marina_MBIC11017           1.763
2167       1 1.766-1.771 Acaryochloris_marina_MBIC11017           1.768
2168       1 1.771-1.774 Acaryochloris_marina_MBIC11017           1.772
      shannon DBL_scale  bw SIM_rep    dataset
2166 5.498796      0.25 0.2       4 Simulation
2167 5.471169      0.25 0.2       4 Simulation
2168 5.498865      0.25 0.2       4 Simulation

In [37]:
%%R -w 800 -h 600
# summarizing
df.shan.s = df.shan %>%
    group_by(dataset, bw, DBL_scale, BD_bin = ntile(Buoyant_density, 24)) %>%
    summarize(mean_shannon = mean(shannon), 
              sd_shannon = sd(shannon), 
              mean_BD = mean(Buoyant_density))

ggplot(df.shan.s, aes(mean_BD, mean_shannon, color=dataset,
                      ymin=mean_shannon-sd_shannon, ymax=mean_shannon+sd_shannon)) +
    geom_pointrange() +
    facet_grid(DBL_scale ~ bw) +
    labs(x='Buoyant density (binned; 24 bins)', y='Shannon index') +
    theme_bw() +
    theme(
        text = element_text(size=16)
    )



In [39]:
%%R -w 650 -h 600
# pairwise correlations for each dataset
df.shan.bin = df.shan %>%
    group_by(BD_bin = ntile(Buoyant_density, 24))

#calc.spearman = function(x){
#    cor(x[,'shannon.x'], x['shannon.y'], method='spearman')[1,1]
#}

calc.pearson = function(x){
    cor(x[,'shannon.x'], x['shannon.y'], method='pearson')[1,1]
}


df.shan.corr = inner_join(df.shan.bin, df.shan.bin, c('BD_bin' = 'BD_bin',
                                                      'bw' = 'bw',
                                                      'DBL_scale' = 'DBL_scale')) %>%
    group_by(bw, DBL_scale, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.pearson)) %>%
    unnest(pearson = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model) %>%
    mutate(pearson_txt = round(pearson, 2))

        
# plotting
ggplot(df.shan.corr, aes(dataset.x, dataset.y, fill=pearson)) +
    geom_tile() +
    geom_text(aes(label=pearson_txt), color='white', size=6) +
    scale_fill_gradient(low='black', high='red') +
    labs(title='Shannon index') +
    facet_grid(DBL_scale ~ bw) +        
    theme(
        text = element_text(size=16)
    )


BD spans


In [40]:
sim_BDspan_files = !find $buildDir -name "OTU_abs1e9_PCR_sub_BD-span.txt"
print len(sim_BDspan_files)
print emp_BDspan_file


36
/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_trm_BD-span.txt

In [41]:
%%R -i sim_BDspan_files -i emp_BDspan_file
df.BDspan = load.data.files(sim_BDspan_files, emp_BDspan_file) 
df.BDspan %>% head


                                   OTU library mean_preFrac_abund min_BD max_BD
1       Acaryochloris_marina_MBIC11017       1       0.0003366196  1.659  1.781
2       Acetobacterium_woodii_DSM_1030       1       0.0001620700  1.659  1.778
3 Acetobacter_pasteurianus_IFO_3283-03       1       0.0002365695  1.659  1.781
4    Acetohalobium_arabaticum_DSM_5501       1       0.0003956025  1.659  1.781
5         Acholeplasma_laidlawii_PG-8A       1       0.0002238808  1.659  1.776
6             Acholeplasma_palmae_J233       1       0.0007560027  1.659  1.778
  BD_range BD_range_perc DBL_scale  bw SIM_rep    dataset
1    0.122     100.00000       0.2 0.6       3 Simulation
2    0.119      97.54098       0.2 0.6       3 Simulation
3    0.122     100.00000       0.2 0.6       3 Simulation
4    0.122     100.00000       0.2 0.6       3 Simulation
5    0.117      95.90164       0.2 0.6       3 Simulation
6    0.119      97.54098       0.2 0.6       3 Simulation

In [42]:
%%R -w 700 -h 600

# plotting
ggplot(df.BDspan, aes(mean_preFrac_abund, BD_range_perc, fill=dataset)) +
    geom_hex(alpha=0.5) +
    scale_x_log10() +
    facet_grid(DBL_scale ~ bw) +
    labs(x='Pre-fractionation abundance', y='BD span') +
    theme_bw() +
    theme(
        text = element_text(size=16)
    )



In [43]:
%%R -i sim_BDspan_files -i emp_BDspan_file

# binning by pre-fractionation abundances
n.tile = 20
df.BDspan = df.BDspan %>%
    group_by(dataset, library, DBL_scale, bw, preFrac_abund_bin = ntile(mean_preFrac_abund, n.tile)) %>%
    summarize(mean_preFrac_abund = mean(mean_preFrac_abund),
              var_BD_range = var(BD_range),
              sd_BD_range = sd(BD_range))

df.BDspan %>% tail(n=3)


Source: local data frame [3 x 8]
Groups: dataset, library, DBL_scale, bw [1]

     dataset library DBL_scale    bw preFrac_abund_bin mean_preFrac_abund
       (chr)   (int)     (dbl) (dbl)             (int)              (dbl)
1 Simulation       1      0.25     1                18        0.001635665
2 Simulation       1      0.25     1                19        0.002461719
3 Simulation       1      0.25     1                20        0.009106353
Variables not shown: var_BD_range (dbl), sd_BD_range (dbl)

In [44]:
%%R -w 650 -h 600
calc.spearman = function(x){
    cor(x[,'var_BD_range.x'], x['var_BD_range.y'], method='spearman')[1,1]
}

df.BDspan.corr = inner_join(df.BDspan, df.BDspan, c('preFrac_abund_bin' = 'preFrac_abund_bin',
                                                    'DBL_scale' = 'DBL_scale',
                                                    'bw' = 'bw')) %>%
    group_by(DBL_scale, bw, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.spearman)) %>%
    unnest(spearman = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model)  %>%
    mutate(spearman_txt = round(spearman, 2))


# plotting
ggplot(df.BDspan.corr, aes(dataset.x, dataset.y, fill=spearman)) +
    geom_tile() +
    geom_text(aes(label=spearman_txt), color='white', size=6) +
    scale_fill_gradient(low='black', high='red') +
    labs(title='BD span') +
    facet_grid(DBL_scale ~ bw) +
    theme(
        text = element_text(size=16)
    )


correlograms (jaccard ~ BD)


In [45]:
sim_corr_files = !find $buildDir -name "OTU_abs1e9_PCR_sub_corr.txt"
print len(sim_corr_files)
print emp_corr_file


36
/home/nick/notebook/SIPSim/dev/fullCyc_trim/SIP-core_unk_trm_corr.txt

In [46]:
%%R -i sim_corr_files -i emp_corr_file

df.corr = load.data.files(sim_corr_files, emp_corr_file) 

# binning
df.corr = df.corr %>%
    filter(!is.na(Mantel.corr)) %>%
    group_by(DBL_scale, bw, dataset, library, class.index.bin = ntile(class.index, 12)) 

df.corr %>% tail(n=3) %>% as.data.frame


  library class.index n.dist Mantel.corr    Pr Pr.corr DBL_scale  bw SIM_rep
1       1  0.04020833     22 -0.08073937 0.089   0.445      0.25 0.2       4
2       1  0.04412500     16 -0.08882032 0.059   0.354      0.25 0.2       4
3       1  0.04804167     16 -0.05239369 0.186   0.585      0.25 0.2       4
     dataset class.index.bin
1 Simulation              10
2 Simulation              11
3 Simulation              12

In [47]:
%%R -w 800 -h 600
# plotting
df.corr.s = df.corr %>%
    group_by(DBL_scale, bw, dataset, class.index.bin) %>%
    summarize(mean_Mantel.corr = mean(Mantel.corr),
              sd_Mantel.corr = sd(Mantel.corr), 
              mean_class.index = mean(class.index))

ggplot(df.corr.s, aes(mean_class.index, mean_Mantel.corr, color=dataset,
                     ymin=mean_Mantel.corr-sd_Mantel.corr,
                     ymax=mean_Mantel.corr+sd_Mantel.corr)) +
    geom_pointrange() +
    labs(x='Class index (binned; 12 bins)', y='Mantel correlation coef.') +
    facet_grid(DBL_scale ~ bw) + 
    theme_bw() +
    theme(
        text = element_text(size=16)
    )



In [51]:
%%R -w 700 -h 600
# pairwise correlations for each dataset
df.shan.bin = df.shan %>%
    group_by(BD_bin = ntile(Buoyant_density, 24))

calc.pearson = function(x){
    cor(x[,'Mantel.corr.x'], x['Mantel.corr.y'], method='pearson')[1,1]
}

df.corr.lm = inner_join(df.corr, df.corr, c('class.index.bin' = 'class.index.bin',
                                                      'bw' = 'bw',
                                                      'DBL_scale' = 'DBL_scale')) %>%
    group_by(bw, DBL_scale, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.pearson)) %>%
    unnest(pearson = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model) %>%
    mutate(pearson_txt = round(pearson, 2))

        
# plotting
ggplot(df.corr.lm, aes(dataset.x, dataset.y, fill=pearson)) +
    geom_tile() +
    geom_text(aes(label=pearson_txt), color='white', size=6) +
    scale_fill_gradient(low='black', high='red') +
    labs(title='Beta diversity correlogram') +
    facet_grid(DBL_scale ~ bw) +        
    theme(
        text = element_text(size=16)
    )


Summary plots for all simulations


In [52]:
course_data_dir = "/home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/rep4_DBL-comm_bw/"

Shannon


In [179]:
sim_shan_files1 = !find $course_data_dir -name "OTU_abs1e9_PCR_sub_shan.txt"
to_rm = '/home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/rep4_DBL-comm_bw/0.2/0.6'
sim_shan_files1 = [x for x in sim_shan_files1 if not x.startswith(to_rm)]
sim_shan_files2 = !find $buildDir -name "OTU_abs1e9_PCR_sub_shan.txt"
sim_shan_files = sim_shan_files1 + sim_shan_files2

print len(sim_shan_files)


64

In [180]:
%%R -i sim_shan_files -i emp_shan_file

df.shan = load.data.files(sim_shan_files, emp_shan_file) 
df.shan %>% tail(n=3)


     library      sample                            OTU Buoyant_density
3854       1 1.760-1.766 Acaryochloris_marina_MBIC11017           1.763
3855       1 1.766-1.771 Acaryochloris_marina_MBIC11017           1.768
3856       1 1.771-1.774 Acaryochloris_marina_MBIC11017           1.772
      shannon DBL_scale  bw SIM_rep    dataset
3854 5.498796      0.25 0.2       4 Simulation
3855 5.471169      0.25 0.2       4 Simulation
3856 5.498865      0.25 0.2       4 Simulation

In [189]:
%%R -h 300
# pairwise correlations for each dataset
df.shan.bin = df.shan %>%
    group_by(BD_bin = ntile(Buoyant_density, 24))

calc.pearson = function(x){
    cor(x[,'shannon.x'], x['shannon.y'], method='pearson')[1,1]
}


df.shan.corr = inner_join(df.shan.bin, df.shan.bin, c('BD_bin' = 'BD_bin',
                                                      'bw' = 'bw',
                                                      'DBL_scale' = 'DBL_scale')) %>%
    group_by(bw, DBL_scale, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.pearson)) %>%
    unnest(pearson = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model) %>%
    mutate(pearson_txt = round(pearson, 2))

        
# getting emperical-emperical corr        
emp.val = df.shan.corr %>% 
    filter((dataset.x == 'Emperical' &
            dataset.y == 'Emperical')) %>%
    group_by() %>%
    summarize(max_value = max(pearson)) %>%
    ungroup() %>%
    select(max_value) %>% as.matrix %>% as.vector
emp.val = emp.val[1] 
        
# filtering 
df.shan.corr.f = df.shan.corr %>% 
    filter((dataset.x == 'Simulation' &
            dataset.y == 'Emperical')) %>%
    mutate(DBL_scale = DBL_scale %>% as.character,
           bw = bw %>% as.character,
           gt_emp = ifelse(pearson > emp.val, 'bold.italic', 'plain')) %>%
    complete(DBL_scale, bw)
df.shan.corr.f %>% head(n=3)
   
        
# plotting
ggplot(df.shan.corr.f, aes(DBL_scale,bw, fill=pearson)) +
    geom_tile() +
    geom_text(aes(label=pearson_txt,fontface=gt_emp), color='white', size=6) +
    scale_color_manual(values=c('white', 'black')) +
    scale_fill_gradient('Pearson', low='black', high='red') +
    labs(title='Shannon index', x='DBL scaling', y='KDE Bandwidth') +       
    theme(
        text = element_text(size=16)
    )


BD span


In [167]:
sim_BDspan_files1 = !find $course_data_dir -name "OTU_abs1e9_PCR_sub_BD-span.txt"
to_rm = '/home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/rep4_DBL-comm_bw/0.2/0.6'
sim_BDspan_files1 = [x for x in sim_BDspan_files1 if not x.startswith(to_rm)]
sim_BDspan_files2 = !find $buildDir -name "OTU_abs1e9_PCR_sub_BD-span.txt"
sim_BDspan_files = sim_BDspan_files1 + sim_BDspan_files2

print len(sim_BDspan_files)


64

In [168]:
%%R -i sim_BDspan_files -i emp_BDspan_file

df.BDspan = load.data.files(sim_BDspan_files, emp_BDspan_file) 
df.BDspan %>% head(n=3)


                                   OTU library mean_preFrac_abund min_BD max_BD
1       Acaryochloris_marina_MBIC11017       1       0.0002621200  1.659  1.781
2       Acetobacterium_woodii_DSM_1030       1       0.0012047869  1.659  1.781
3 Acetobacter_pasteurianus_IFO_3283-03       1       0.0002955524  1.659  1.781
  BD_range BD_range_perc DBL_scale   bw SIM_rep    dataset
1    0.122           100       0.4 0.06       3 Simulation
2    0.122           100       0.4 0.06       3 Simulation
3    0.122           100       0.4 0.06       3 Simulation

In [169]:
%%R 

# binning by pre-fractionation abundances
n.tile = 20
df.BDspan = df.BDspan %>%
    group_by(dataset, library, DBL_scale, bw, preFrac_abund_bin = ntile(mean_preFrac_abund, n.tile)) %>%
    summarize(mean_preFrac_abund = mean(mean_preFrac_abund),
              var_BD_range = var(BD_range),
              sd_BD_range = sd(BD_range))

df.BDspan %>% tail(n=3)


Source: local data frame [3 x 8]
Groups: dataset, library, DBL_scale, bw [1]

     dataset library DBL_scale    bw preFrac_abund_bin mean_preFrac_abund
       (chr)   (int)     (dbl) (dbl)             (int)              (dbl)
1 Simulation       1       0.8   0.6                18        0.001634448
2 Simulation       1       0.8   0.6                19        0.002465021
3 Simulation       1       0.8   0.6                20        0.009600700
Variables not shown: var_BD_range (dbl), sd_BD_range (dbl)

In [190]:
%%R -h 300
calc.spearman = function(x){
    cor(x[,'var_BD_range.x'], x['var_BD_range.y'], method='spearman')[1,1]
}

df.BDspan.corr = inner_join(df.BDspan, df.BDspan, c('preFrac_abund_bin' = 'preFrac_abund_bin',
                                                    'DBL_scale' = 'DBL_scale',
                                                    'bw' = 'bw')) %>%
    group_by(DBL_scale, bw, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.spearman)) %>%
    unnest(spearman = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model)  %>%
    mutate(spearman_txt = round(spearman, 2))

        
# getting emperical-emperical corr        
emp.val = df.BDspan.corr %>% 
    filter((dataset.x == 'Emperical' &
            dataset.y == 'Emperical')) %>%
    group_by() %>%
    summarize(max_value = max(spearman, na.rm=TRUE)) %>%
    ungroup() %>%
    select(max_value) %>% as.matrix %>% as.vector
emp.val = emp.val[1]   
        
# filtering 
df.BDspan.corr.f = df.BDspan.corr %>% 
    filter((dataset.x == 'Simulation' &
            dataset.y == 'Emperical')) %>%
    mutate(DBL_scale = DBL_scale %>% as.character,
           bw = bw %>% as.character,
           gt_emp = ifelse(spearman > emp.val, 'bold.italic', 'plain')) %>%
    complete(DBL_scale, bw)
   
# plotting
ggplot(df.BDspan.corr.f, aes(DBL_scale, bw, fill=spearman)) +
    geom_tile() +
    geom_text(aes(label=spearman_txt, fontface=gt_emp), color='white', size=6) +
    scale_color_manual(values=c('white', 'black')) +
    scale_fill_gradient('Spearman', low='black', high='red') +
    labs(title='BD span', x='DBL scaling', y='KDE Bandwidth') +   
    theme(
        text = element_text(size=16)
    )


correlogram


In [182]:
sim_corr_files1 = !find $course_data_dir -name "OTU_abs1e9_PCR_sub_corr.txt"
to_rm = '/home/nick/notebook/SIPSim/dev/fullCyc/n1147_frag_norm_9_2.5_n5/rep4_DBL-comm_bw/0.2/0.6'
sim_corr_files1 = [x for x in sim_corr_files1 if not x.startswith(to_rm)]
sim_corr_files2 = !find $buildDir -name "OTU_abs1e9_PCR_sub_corr.txt"
sim_corr_files = sim_corr_files1 + sim_corr_files2

print len(sim_corr_files)


64

In [183]:
%%R -i sim_corr_files -i emp_corr_file

df.corr = load.data.files(sim_corr_files, emp_corr_file) 

# binning
df.corr = df.corr %>%
    filter(!is.na(Mantel.corr)) %>%
    group_by(DBL_scale, bw, dataset, library, class.index.bin = ntile(class.index, 12)) 

df.corr %>% tail(n=3) %>% as.data.frame


  library class.index n.dist Mantel.corr    Pr Pr.corr DBL_scale  bw SIM_rep
1       1  0.04020833     22 -0.08073937 0.089   0.445      0.25 0.2       4
2       1  0.04412500     16 -0.08882032 0.059   0.354      0.25 0.2       4
3       1  0.04804167     16 -0.05239369 0.186   0.585      0.25 0.2       4
     dataset class.index.bin
1 Simulation              10
2 Simulation              11
3 Simulation              12

In [188]:
%%R -h 300
# pairwise correlations for each dataset
df.shan.bin = df.shan %>%
    group_by(BD_bin = ntile(Buoyant_density, 24))

calc.pearson = function(x){
    cor(x[,'Mantel.corr.x'], x['Mantel.corr.y'], method='pearson')[1,1]
}

df.corr.lm = inner_join(df.corr, df.corr, c('class.index.bin' = 'class.index.bin',
                                                      'bw' = 'bw',
                                                      'DBL_scale' = 'DBL_scale')) %>%
    group_by(bw, DBL_scale, dataset.x, dataset.y) %>%
    nest() %>%
    mutate(model = purrr::map(data, calc.pearson)) %>%
    unnest(pearson = model %>% purrr::map(function(x) x)) %>%
    ungroup() %>%
    select(-data, -model) %>%
    mutate(pearson_txt = round(pearson, 2))


# getting emperical-emperical corr        
emp.val = df.corr.lm %>% 
    filter((dataset.x == 'Emperical' &
            dataset.y == 'Emperical')) %>%
    group_by() %>%
    summarize(max_value = max(pearson)) %>%
    ungroup() %>%
    select(max_value) %>% as.matrix %>% as.vector
emp.val = emp.val[1]
print(emp.val)    
        
# filtering 
df.corr.lm.f = df.corr.lm %>% 
    filter((dataset.x == 'Simulation' &
            dataset.y == 'Emperical')) %>%
    mutate(DBL_scale = DBL_scale %>% as.character,
           bw = bw %>% as.character,
           gt_emp = ifelse(pearson >= emp.val, 'bold.italic', 'plain')) %>%
    complete(DBL_scale, bw)
df.corr.lm.f %>% head(n=3)
   
        
# plotting
ggplot(df.corr.lm.f, aes(DBL_scale,bw, fill=pearson)) +
    geom_tile() +
    geom_text(aes(label=pearson_txt,fontface=gt_emp), color='white', size=6) +
    scale_color_manual(values=c('white', 'black')) +
    scale_fill_gradient('Pearson', low='black', high='red') +
    labs(title='Beta diversity correlogram', x='DBL scaling', y='KDE Bandwidth') +     
    theme(
        text = element_text(size=16)
    )


[1] 0.9535471

In [ ]: