287 lines
8.3 KiB
R
287 lines
8.3 KiB
R
#============================================
|
|
# TASK: to calculate allele frequency and OR
|
|
# on master data
|
|
#===========================================
|
|
homedir = '~'
|
|
getwd()
|
|
#setwd('~/git/LSHTM_analysis/meta_data_analysis')
|
|
setwd(paste0(homedir, '/', 'git/LSHTM_analysis/meta_data_analysis'))
|
|
getwd()
|
|
|
|
#%% variable assignment: input and output paths & filenames
|
|
drug = 'pyrazinamide'
|
|
gene = 'pncA'
|
|
gene_match = paste0(gene,'_p.')
|
|
print(gene_match)
|
|
|
|
#=======
|
|
# input dir
|
|
#=======
|
|
# file1: Raw data
|
|
#indir = 'git/Data/pyrazinamide/input/original'
|
|
indir = paste0('git/Data', '/', drug, '/', 'input/original')
|
|
in_filename = 'original_tanushree_data_v2.csv'
|
|
infile = paste0(homedir, '/', indir, '/', in_filename)
|
|
print(paste0('Reading infile:', ' ', infile) )
|
|
|
|
# file2: file to extract valid snps and add calcs to: pnca_metadata.csv {outfile3 from data extraction script}
|
|
indir_metadata = paste0('git/Data', '/', drug, '/', 'output')
|
|
in_filename_metadata = 'pnca_metadata.csv'
|
|
infile_metadata = paste0(homedir, '/', indir_metadata, '/', in_filename_metadata)
|
|
print(paste0('Reading metadata infile:', infile_metadata))
|
|
|
|
#=========
|
|
# output dir
|
|
#=========
|
|
# output filename in respective section at the time of outputting files
|
|
#outdir = 'git/Data/pyrazinamide/output'
|
|
outdir = paste0('git/Data', '/', drug, '/', 'output')
|
|
out_filename = paste0(tolower(gene),'_', 'meta_data_with_AFandOR.csv')
|
|
outfile = paste0(homedir, '/', outdir, '/', out_filename)
|
|
print(paste0('Output file with full path:', outfile))
|
|
|
|
#%% end of variable assignment for input and output files
|
|
|
|
#===============
|
|
# Step 1: Read master/raw data stored in Data/
|
|
#===============
|
|
raw_data_all = read.csv(infile, stringsAsFactors = F)
|
|
|
|
raw_data = raw_data_all[,c("id"
|
|
, "pyrazinamide"
|
|
, "dr_mutations_pyrazinamide"
|
|
, "other_mutations_pyrazinamide")]
|
|
rm(raw_data_all)
|
|
|
|
rm(indir, in_filename, infile)
|
|
|
|
#####
|
|
# 1a: exclude na
|
|
#####
|
|
raw_data = raw_data[!is.na(raw_data$pyrazinamide),]
|
|
|
|
total_samples = length(unique(raw_data$id))
|
|
print(paste0('Total samples without NA in', ' ', drug, 'is:', total_samples))
|
|
|
|
# sanity check: should be true
|
|
is.numeric(total_samples)
|
|
|
|
#####
|
|
# 1b: combine the two mutation columns
|
|
#####
|
|
raw_data$all_mutations_pyrazinamide = paste(raw_data$dr_mutations_pyrazinamide
|
|
, raw_data$other_mutations_pyrazinamide)
|
|
head(raw_data$all_mutations_pyrazinamide)
|
|
|
|
#####
|
|
# 1c: create yet another column that contains all the mutations but in lower case
|
|
#####
|
|
raw_data$all_muts_pnca = tolower(raw_data$all_mutations_pyrazinamide)
|
|
|
|
# sanity checks
|
|
#table(grepl("pnca_p",raw_data$all_muts_pnca))
|
|
print(paste0('converting gene match:', gene_match, ' ', 'to lowercase'))
|
|
gene_match = tolower(gene_match)
|
|
|
|
table(grepl(gene_match,raw_data$all_muts_pnca))
|
|
|
|
# sanity check: should be TRUE
|
|
#sum(table(grepl("pnca_p",raw_data$all_muts_pnca))) == total_samples
|
|
sum(table(grepl(gene_match,raw_data$all_muts_pnca))) == total_samples
|
|
|
|
# set up variables: can be used for logistic regression as well
|
|
i = "pnca_p.ala134gly" # has a NA, should NOT exist
|
|
table(grepl(i,raw_data$all_muts_pnca))
|
|
|
|
i = "pnca_p.trp68gly"
|
|
table(grepl(i,raw_data$all_muts_pnca))
|
|
|
|
mut = grepl(i,raw_data$all_muts_pnca)
|
|
dst = raw_data$pyrazinamide
|
|
table(mut, dst)
|
|
|
|
#chisq.test(table(mut,dst))
|
|
#fisher.test(table(mut, dst))
|
|
#table(mut)
|
|
|
|
#===============
|
|
# Step 2: Read valid snps for which OR can be calculated (infile_comp_snps.csv)
|
|
#===============
|
|
print(paste0('Reading metadata infile:', infile_metadata))
|
|
|
|
pnca_metadata = read.csv(infile_metadata
|
|
# , file.choose()
|
|
, stringsAsFactors = F
|
|
, header = T)
|
|
|
|
|
|
# clear variables
|
|
rm(homedir, in_filename, indir, infile)
|
|
rm(indir_metadata, infile_metadata, in_filename_metadata)
|
|
|
|
# count na in pyrazinamide column
|
|
tot_pza_na = sum(is.na(pnca_metadata$pyrazinamide))
|
|
expected_rows = nrow(pnca_metadata) - tot_pza_na
|
|
|
|
# drop na from the pyrazinamide colum
|
|
pnca_snps_or = pnca_metadata[!is.na(pnca_metadata$pyrazinamide),]
|
|
|
|
# sanity check
|
|
if(nrow(pnca_snps_or) == expected_rows){
|
|
print('PASS: no. of rows match with expected_rows')
|
|
} else{
|
|
print('FAIL: nrows mismatch.')
|
|
}
|
|
|
|
# extract unique snps to iterate over for AF and OR calcs
|
|
pnca_snps_unique = unique(pnca_snps_or$mutation)
|
|
|
|
print(paste0('Total no. of distinct comp snps to perform OR calcs: ', length(pnca_snps_unique)))
|
|
|
|
# Define OR function
|
|
x = as.numeric(mut)
|
|
y = dst
|
|
or = function(x,y){
|
|
tab = as.matrix(table(x,y))
|
|
a = tab[2,2]
|
|
if (a==0){ a<-0.5}
|
|
b = tab[2,1]
|
|
if (b==0){ b<-0.5}
|
|
c = tab[1,2]
|
|
if (c==0){ c<-0.5}
|
|
d = tab[1,1]
|
|
if (d==0){ d<-0.5}
|
|
(a/b)/(c/d)
|
|
|
|
}
|
|
|
|
dst = raw_data$pyrazinamide
|
|
ors = sapply(pnca_snps_unique,function(m){
|
|
mut = grepl(m,raw_data$all_muts_pnca)
|
|
or(mut,dst)
|
|
})
|
|
|
|
ors
|
|
|
|
pvals = sapply(pnca_snps_unique,function(m){
|
|
mut = grepl(m,raw_data$all_muts_pnca)
|
|
fisher.test(mut,dst)$p.value
|
|
})
|
|
|
|
pvals
|
|
|
|
afs = sapply(pnca_snps_unique,function(m){
|
|
mut = grepl(m,raw_data$all_muts_pnca)
|
|
mean(mut)
|
|
})
|
|
|
|
afs
|
|
|
|
# check ..hmmm
|
|
afs['pnca_p.trp68gly']
|
|
afs['pnca_p.gln10pro']
|
|
afs['pnca_p.leu4ser']
|
|
|
|
plot(density(log(ors)))
|
|
plot(-log10(pvals))
|
|
hist(log(ors)
|
|
, breaks = 100
|
|
)
|
|
|
|
# FIXME: could be good to add a sanity check
|
|
if (table(names(ors) == names(pvals)) & table(names(ors) == names(afs)) & table(names(pvals) == names(afs)) == length(pnca_snps_unique)){
|
|
print('PASS: names of ors, pvals and afs match: proceed with combining into a single df')
|
|
} else{
|
|
print('FAIL: names of ors, pvals and afs mismatch')
|
|
}
|
|
|
|
# combine
|
|
comb_AF_and_OR = data.frame(ors, pvals, afs)
|
|
head(rownames(comb_AF_and_OR))
|
|
|
|
# add rownames of comb_AF_and_OR as an extra column 'mutation' to allow merging based on this column
|
|
comb_AF_and_OR$mutation = rownames(comb_AF_and_OR)
|
|
|
|
# sanity check
|
|
if (table(rownames(comb_AF_and_OR) == comb_AF_and_OR$mutation)){
|
|
print('PASS: rownames and mutaion col values match')
|
|
}else{
|
|
print('FAIL: rownames and mutation col values mismatch')
|
|
}
|
|
|
|
############
|
|
# Merge 1:
|
|
###########
|
|
df1 = pnca_metadata
|
|
df2 = comb_AF_and_OR
|
|
|
|
head(df1$mutation); head(df2$mutation)
|
|
|
|
# FIXME: newlines
|
|
print(paste0('merging two dfs: '
|
|
,'\ndf1 (big df i.e. meta data) nrows: ', nrow(df1)
|
|
,'\ndf2 (small df i.e af, or, pval) nrows: ', nrow(df2)
|
|
, 'expected rows in merged df: ', nrow(df1), 'expected cols in merged_df: ', (ncol(df1) + ncol(df2) - 1)))
|
|
|
|
merged_df = merge(df1 # big file
|
|
, df2 # small (afor file)
|
|
, by = "mutation"
|
|
, all.x = T) # because you want all the entries of the meta data
|
|
|
|
# sanity check
|
|
if(ncol(merged_df) == (ncol(df1) + ncol(df2) - 1)){
|
|
print(paste0('PASS: no. of cols is as expected: ', ncol(merged_df)))
|
|
} else{
|
|
print('FAIL: no.of cols mistmatch')
|
|
}
|
|
|
|
# quick check
|
|
i = "pnca_p.ala134gly" # has all NAs in pyrazinamide, should be NA in ors, etc.
|
|
merged_df[merged_df$mutation == i,]
|
|
|
|
# count na in each column
|
|
na_count = sapply(merged_df, function(y) sum(length(which(is.na(y))))); na_count
|
|
# only some or and Af should be NA
|
|
#Row.names ors pvals afs
|
|
#63 63 63 63
|
|
|
|
# reassign custom colnames
|
|
colnames(merged_df)[colnames(merged_df)== "ors"] <- "OR"
|
|
colnames(merged_df)[colnames(merged_df)== "afs"] <- "AF"
|
|
colnames(merged_df)[colnames(merged_df)== "pvals"] <- "pvalue"
|
|
|
|
colnames(merged_df)
|
|
|
|
# add log OR and neglog pvalue
|
|
merged_df$logor = log(merged_df$OR)
|
|
is.numeric(merged_df$logor)
|
|
|
|
merged_df$neglog10pvalue = -log10(merged_df$pvalue)
|
|
is.numeric(merged_df$neglog10pvalue)
|
|
|
|
merged_df$AF_percent = merged_df$AF*100
|
|
is.numeric(merged_df$AF_percent)
|
|
|
|
# check AFs
|
|
#i = 'pnca_p.trp68gly'
|
|
i = 'pnca_p.gln10pro'
|
|
#i = 'pnca_p.leu4ser'
|
|
merged_df[merged_df$mutation == i,]
|
|
|
|
# FIXME: harcoding (beware!), NOT FATAL though!
|
|
ncol_added = 3
|
|
|
|
print(paste0('Added', ncol_added, ' ', 'more cols to merged_df i.e log10 OR and -log10 P-val: '
|
|
, 'no. of cols in merged_df now: ', ncol(merged_df)))
|
|
|
|
#%% write file out: pnca_meta_data_with_AFandOR
|
|
print(paste0('writing output file in: '
|
|
, 'Filename: ', out_filename
|
|
, 'Path:', outdir))
|
|
|
|
write.csv(merged_df, outfile
|
|
, row.names = F)
|
|
|
|
print(paste0('Finished writing:', out_filename, '\nExpected no. of cols:', ncol(merged_df)))
|
|
print('======================================================================')
|
|
rm(out_filename)
|