Goal

  • Follow-up to: atomIncorp_taxaIncorp
  • Determining the effect of 'heavy' BD window (number of windows & window sizes) on HR-SIP accuracy
  • Notes: using hierarchical multi-window method
    • heaviest window first
    • all non-incorporators found for that window are used for next window
    • this repeats for all windows
    • global adjustment of p-values (should be just one test per taxon)

Variable parameters:

  • 'heavy' BD window sizes

Init


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

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

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


/opt/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py:106: UserWarning: Want to understand how all the pieces fit together? Buy the ggplot2
book: http://ggplot2.org/book/

  res = super(Function, self).__call__(*new_args, **new_kwargs)
/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]:
## 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

print 'Min BD: {}'.format(min_BD)
print 'Max BD: {}'.format(max_BD)


Min BD: 1.67323
Max BD: 1.7744

Nestly

  • assuming fragments already simulated

In [5]:
# paths
workDir = '/home/nick/notebook/SIPSim/dev/bac_genome1147/'
buildDir = os.path.join(workDir, 'atomIncorp_taxaIncorp_HMW-HR-SIP')
dataDir = os.path.join(workDir, 'atomIncorp_taxaIncorp')

In [6]:
if not os.path.isdir(buildDir):
    os.makedirs(buildDir)
%cd $buildDir


/home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP

In [7]:
# making an experimental design file for qSIP
x = range(1,7)
y = ['control', 'treatment']

expDesignFile = os.path.join(buildDir, 'qSIP_exp_design.txt')
with open(expDesignFile, 'wb') as outFH:
    for i,z in itertools.izip(x,itertools.cycle(y)):
        line = '\t'.join([str(i),z])
        outFH.write(line + '\n')

!head $expDesignFile


1	control
2	treatment
3	control
4	treatment
5	control
6	treatment

Nestly params


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

# varying params
nest.add('percIncorp', [0, 15, 25, 50, 100])
nest.add('percTaxa', [1, 5, 10, 25, 50])
nest.add('rep', range(1,11))

## set params
nest.add('abs', ['1e9'], create_dir=False)
nest.add('np', [10], create_dir=False)
nest.add('Monte_rep', [100000], 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)
nest.add('min_BD', [min_BD], create_dir=False)
nest.add('max_BD', [max_BD], create_dir=False)
nest.add('DBL_scaling', [0.5], create_dir=False)
nest.add('bandwidth', [0.8], create_dir=False)
nest.add('heavy_BD_min', [1.71], create_dir=False)
nest.add('heavy_BD_max', [1.75], create_dir=False)
nest.add('topTaxaToPlot', [100], create_dir=False)
nest.add('padj', [0.1], create_dir=False)
nest.add('log2', [0.25], create_dir=False)

### input/output files
nest.add('buildDir', [buildDir], create_dir=False)
nest.add('exp_design', [expDesignFile], create_dir=False)


# building directory tree
nest.build(buildDir)

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

Copying input files


In [9]:
files = !find . -name "*.json"
dirs = [os.path.split(x)[0] for x in files]

srcFiles = ['OTU_abs1e9_PCR_sub_w.txt', 'OTU_abs1e9_PCR_sub_meta.txt', 'BD-shift_stats.txt']

for d in dirs:
    for f in srcFiles:
        f1 = os.path.join(dataDir, d, f)
        f2 = os.path.join(buildDir, d, f)
        cmd = 'cp -f {} {}'.format(f1, f2)
        !$cmd

Hierarchical multi-window HR-SIP


In [10]:
bashFileTmp = os.path.splitext(bashFile)[0] + '_HRSIP_multi.sh'
bashFileTmp


Out[10]:
'/home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/SIPSimRun_HRSIP_multi.sh'

In [11]:
%%writefile $bashFileTmp
#!/bin/bash

# phyloseq
## making phyloseq object from OTU table
SIPSimR phyloseq_make \
    OTU_abs{abs}_PCR_sub_w.txt \
    -s OTU_abs{abs}_PCR_sub_meta.txt \
    > OTU_abs{abs}_PCR_sub.physeq

## HR SIP pipeline
SIPSim HRSIP \
    --hier \
    --log2 {log2} \
    --hypo greater \
    --cont 1,3,5 \
    --treat 2,4,6 \
    -w 1.71-1.75 \
    --prefix OTU_abs{abs}_PCR_sub_BD3-1 \
    OTU_abs{abs}_PCR_sub.physeq

## HR SIP pipeline
SIPSim HRSIP \
    --hier \
    --log2 {log2} \
    --hypo greater \
    --cont 1,3,5 \
    --treat 2,4,6 \
    -w 1.69-1.74,1.73-1.78 \
    --prefix OTU_abs{abs}_PCR_sub_BD3-2 \
    OTU_abs{abs}_PCR_sub.physeq    
    
SIPSim HRSIP \
    --hier \
    --log2 {log2} \
    --hypo greater \
    --cont 1,3,5 \
    --treat 2,4,6 \
    -w 1.70-1.73,1.72-1.75,1.74-1.77 \
    --prefix OTU_abs{abs}_PCR_sub_BD3-3 \
    OTU_abs{abs}_PCR_sub.physeq
    
SIPSim HRSIP \
    --hier \
    --log2 {log2} \
    --hypo greater \
    --cont 1,3,5 \
    --treat 2,4,6 \
    -w 1.69-1.73,1.72-1.76,1.75-1.79 \
    --prefix OTU_abs{abs}_PCR_sub_BD3-4 \
    OTU_abs{abs}_PCR_sub.physeq


Writing /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/SIPSimRun_HRSIP_multi.sh

In [ ]:
!chmod 777 $bashFileTmp
!cd $workDir; \
    nestrun --template-file $bashFileTmp -d $buildDir --log-file HR-SIP_multi.log -j 10


2016-04-07 14:24:16,751 * INFO * Template: ./SIPSimRun_HRSIP_multi.sh
2016-04-07 14:24:16,758 * INFO * [155953] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/7
2016-04-07 14:24:16,764 * INFO * [155955] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/9
2016-04-07 14:24:16,768 * INFO * [155956] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/8
2016-04-07 14:24:16,770 * INFO * [155958] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/3
2016-04-07 14:24:16,772 * INFO * [155960] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/10
2016-04-07 14:24:16,776 * INFO * [155962] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/2
2016-04-07 14:24:16,784 * INFO * [155964] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/1
2016-04-07 14:24:16,792 * INFO * [155966] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/4
2016-04-07 14:24:16,793 * INFO * [155968] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/5
2016-04-07 14:24:16,795 * INFO * [155970] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/6
2016-04-07 14:30:21,405 * INFO * [155955] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/9 Finished with 0
2016-04-07 14:30:21,408 * INFO * [163180] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/7
2016-04-07 14:30:25,634 * INFO * [155962] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/2 Finished with 0
2016-04-07 14:30:25,636 * INFO * [163266] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/9
2016-04-07 14:30:29,121 * INFO * [155966] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/4 Finished with 0
2016-04-07 14:30:29,123 * INFO * [163365] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/8
2016-04-07 14:30:29,287 * INFO * [155964] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/1 Finished with 0
2016-04-07 14:30:29,296 * INFO * [163367] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/3
2016-04-07 14:30:30,296 * INFO * [155970] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/6 Finished with 0
2016-04-07 14:30:30,298 * INFO * [163421] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/10
2016-04-07 14:30:30,437 * INFO * [155958] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/3 Finished with 0
2016-04-07 14:30:30,439 * INFO * [163423] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/2
2016-04-07 14:30:34,060 * INFO * [155953] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/7 Finished with 0
2016-04-07 14:30:34,063 * INFO * [163525] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/1
2016-04-07 14:30:34,223 * INFO * [155968] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/5 Finished with 0
2016-04-07 14:30:34,225 * INFO * [163529] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/4
2016-04-07 14:30:34,759 * INFO * [155960] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/10 Finished with 0
2016-04-07 14:30:34,764 * INFO * [163579] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/5
2016-04-07 14:30:35,136 * INFO * [155956] /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/50/8 Finished with 0
2016-04-07 14:30:35,138 * INFO * [163600] Started ./SIPSimRun_HRSIP_multi.sh in /home/nick/notebook/SIPSim/dev/bac_genome1147/atomIncorp_taxaIncorp_HMW-HR-SIP/50/25/6

In [ ]:
%pushnote HMW-HR-SIP complete

Making confusion matrices


In [ ]:
bashFileTmp = os.path.splitext(bashFile)[0] + '_cMtx.sh'
bashFileTmp

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

# HR-SIP multiple 'heavy' BD windows
SIPSimR DESeq2_confuseMtx \
    --libs 2,4,6 \
    --padj {padj} \
    -o DESeq2_BD3-1-cMtx \
    BD-shift_stats.txt \
    OTU_abs1e9_PCR_sub_BD3-1_DESeq2    
    
    
# HR-SIP multiple 'heavy' BD windows
SIPSimR DESeq2_confuseMtx \
    --libs 2,4,6 \
    --padj {padj} \
    -o DESeq2_BD3-2-cMtx \
    BD-shift_stats.txt \
    OTU_abs1e9_PCR_sub_BD3-2_DESeq2   
    
# HR-SIP multiple 'heavy' BD windows
SIPSimR DESeq2_confuseMtx \
    --libs 2,4,6 \
    --padj {padj} \
    -o DESeq2_BD3-3-cMtx \
    BD-shift_stats.txt \
    OTU_abs1e9_PCR_sub_BD3-3_DESeq2   
    
# HR-SIP multiple 'heavy' BD windows
SIPSimR DESeq2_confuseMtx \
    --libs 2,4,6 \
    --padj {padj} \
    -o DESeq2_BD3-4-cMtx \
    BD-shift_stats.txt \
    OTU_abs1e9_PCR_sub_BD3-4_DESeq2

In [ ]:
!chmod 777 $bashFileTmp
!cd $workDir; \
    nestrun --template-file $bashFileTmp -d $buildDir --log-file cMtx.log -j 10

Aggregating the confusion matrix data


In [ ]:
def agg_cMtx(prefix):
    # all data
    #!nestagg delim \
    #    -d $buildDir \
    #    -k percIncorp,percTaxa,rep \
    #    -o $prefix-cMtx_data.txt \
    #    --tab \
    #    $prefix-cMtx_data.txt

    # overall
    x = prefix + '-cMtx_overall.txt'
    !nestagg delim \
        -d $buildDir \
        -k percIncorp,percTaxa,rep \
        -o $x \
        --tab \
        $x

    # by class
    x = prefix + '-cMtx_byClass.txt'
    !nestagg delim \
        -d $buildDir \
        -k percIncorp,percTaxa,rep \
        -o $x \
        --tab \
        $x
        
agg_cMtx('DESeq2_BD3-1')
agg_cMtx('DESeq2_BD3-2')
agg_cMtx('DESeq2_BD3-3')
agg_cMtx('DESeq2_BD3-4')

In [ ]:
%pushnote HMW-HR-SIP complete!

--End of simulation--


Plotting results


In [ ]:
F = os.path.join(buildDir, '*-cMtx_byClass.txt')
files = glob.glob(F)
files

In [ ]:
%%R -i files

df_byClass = list()
for (f in files){
    ff = strsplit(f, '/') %>% unlist
    fff = ff[length(ff)]
    df_byClass[[fff]] = read.delim(f, sep='\t')
}

df_byClass = do.call(rbind, df_byClass)
df_byClass$file = gsub('\\.[0-9]+$', '', rownames(df_byClass))
df_byClass$method = gsub('-cMtx.+', '', df_byClass$file)
rownames(df_byClass) = 1:nrow(df_byClass)

df_byClass %>% head(n=3)

In [ ]:
%%R
# renaming method
rename = data.frame(method = c('DESeq2_BD3-1', 'DESeq2_BD3-2', 'DESeq2_BD3-3', 'DESeq2_BD3-4'), 
                   method_new = c('1.71-1.75','1.69-1.74,1.73-1.78',
                                  '1.70-1.73,1.72-1.75,1.74-1.77',
                                  '1.69-1.73,1.72-1.76,1.75-1.79'))

df_byClass = inner_join(df_byClass, rename, c('method'='method')) %>%
    select(-method) %>%
    rename('method' = method_new) 

df_byClass$method = factor(df_byClass$method, levels=rename$method_new %>% as.vector)

df_byClass %>% head(n=3)

In [ ]:
%%R -w 800 -h 550
# summarize by SIPSim rep & library rep
df_byClass.s = df_byClass %>%
    group_by(method, percIncorp, percTaxa, variables) %>%
    summarize(mean_value = mean(values),
              sd_value = sd(values))

# plotting
ggplot(df_byClass.s, aes(variables, mean_value, color=method,
                         ymin=mean_value-sd_value,
                         ymax=mean_value+sd_value)) +
    geom_pointrange(alpha=0.8, size=0.2) +
    labs(y='Value') +
    facet_grid(percTaxa ~ percIncorp) +
    theme_bw() +
    theme(
        text = element_text(size=16),
        axis.title.x = element_blank(),
        axis.text.x = element_text(angle=45, hjust=1)
    )

In [ ]:
%%R -w 800 -h 600
# summarize by SIPSim rep & library rep
vars = c('Balanced Accuracy', 'Sensitivity', 'Specificity')
df_byClass.s.f = df_byClass.s %>%
    filter(variables %in% vars) 

# plotting
ggplot(df_byClass.s.f, aes(variables, mean_value, fill=method,
                         ymin=mean_value-sd_value,
                         ymax=mean_value+sd_value)) +
    #geom_pointrange(alpha=0.8, size=0.2) +
    geom_bar(stat='identity', position='dodge') +
    geom_errorbar(stat='identity', position='dodge') +
    scale_y_continuous(breaks=seq(0, 1, 0.2)) +
    facet_grid(percTaxa ~ percIncorp) +
    theme_bw() +
    theme(
        text = element_text(size=16),
        axis.title.x = element_blank(),
        axis.text.x = element_text(angle=45, hjust=1),
        axis.title.y = element_blank()
    )

In [ ]: