File size: 17,714 Bytes
9f29b8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | '''
Utility functions for analysis after running pyHXExpress
Includes some functionality that requires pyHDX
(my github fork has modifications to allow for replicate data)
'''
import os
import importlib
import sys
# hxex_path = os.path.join('')
# sys.path.append(hxex_path)
import hxex_updating as hxex
import numpy as np, pandas as pd
import config as config
from datetime import datetime
pd.set_option('display.max_columns',None)
now = datetime.now()
date = now.strftime("%d%b%Y")
def hxex_reload():
importlib.reload(hxex)
importlib.reload(config)
hxex.config = config
hxex_reload()
### PyHDX documentation, Examples
### https://pyhdx.readthedocs.io/en/stable/examples.html
###
#!pip install pyhdx
import proplot as pplt
from scipy.optimize import lsq_linear
from pathlib import Path
import yaml
from Bio import SeqIO
from collections import defaultdict
from pyhdx.models import Coverage
from pyhdx.plot import peptide_coverage
from pyhdx.batch_processing import StateParser
from pyhdx.fitting import fit_d_uptake
from pyhdx.config import cfg
from pyhdx import read_dynamx, HDXMeasurement
from pyhdx.fitting import fit_rates_half_time_interpolate, fit_rates_weighted_average, fit_gibbs_global
from pyhdx.process import filter_peptides, apply_control, correct_d_uptake
from dask.distributed import Client
from pyhdx.plot import dG_scatter_figure
import pyhdx.plot
from collections import defaultdict
# import warnings
# warnings.simplefilter (action='ignore', )#category=FutureWarning)
# warnings.filterwarnings (action='ignore',)# category=RuntimeWarning,)
# pd.set_option('display.max_columns',None)
def combine_batch_data(dirs):
# Combine the batch outputs into single dataframe files for metadf, datafits, and fitparams
metadf_comb = pd.DataFrame()
datafits_comb = pd.DataFrame()
fitparams_comb = pd.DataFrame()
metadf_prefix = "metadf_asrun_"
datafits_prefix = "data_fits_asrun"
fitparams_prefix = "fitparamsAll_asrun_"
for dir in dirs:
csvfiles = [ f for f in os.listdir(dir) if f[-4:]=='.csv' ]
mf = [ m for m in csvfiles if m[:len(metadf_prefix)]==metadf_prefix]
df = [ d for d in csvfiles if d[:len(datafits_prefix)]==datafits_prefix]
ff = [ f for f in csvfiles if f[:len(fitparams_prefix)]==fitparams_prefix]
for f in mf:
md = pd.read_csv(os.path.join(dir,f))#.drop('Index',axis=1)
md['original_file'] = f
metadf_comb = pd.concat([metadf_comb,md])#,ignore_index = True)
for f in df:
dd = pd.read_csv(os.path.join(dir,f))
dd['original_file'] = f
datafits_comb = pd.concat([datafits_comb,dd])
for f in ff:
fd = pd.read_csv(os.path.join(dir,f)).drop('Index',axis=1)
fd['original_file'] = f
fitparams_comb = pd.concat([fitparams_comb,fd],ignore_index= True)
metadf_comb = metadf_comb.set_index('Index').sort_values('Index')
datafits_comb = datafits_comb.sort_values('data_id').reset_index(drop=True)
fitparams_comb = fitparams_comb.sort_values(['data_id','time_idx','charge','rep','ncurves','nboot']).reset_index(drop=True)
return metadf_comb, datafits_comb, fitparams_comb
def convert_to_uptakedf(datafit,proj=None):
# convert datafits into a format that hdexa_to_pyhdx can recognize (for residue level avg uptake calcs in pyHDX)
# there is an assumption in this that the more protected pop1 goes together and less protected pop2 goes together
# probably mostly okay, but may need to try to account for actual % populations in deciding what goes together
# this needs more consideration if ever more than 2 pops
TD_time = 1e6
test = datafit.copy()
test[['start','end']] = test['peptide_range'].str.split('-',expand=True).astype('int')
try:
test['fracDeut_1'] = test['Dabs_1']/test['max_namides']
except: pass
try:
test['fracDeut_2'] = test['Dabs_2']/test['max_namides']
except: test['fracDeut_2'] = np.nan
#cleanup garbage output - accidental carryover of polymodal data when rolling back to previous number of populations, should be fixed now
test.loc[test['pop_1'] == 1.0, ['centroid_2','Dabs_2','Dabs_std_2','pop_2','pop_std_2','fracDeut_2']] = np.nan
pops = test.fit_pops.max()
testpop = {}
testpop[1] = test.copy()
testpop[1]['Protein State']=testpop[1].apply(lambda row: row['sample']+'_pop1',axis=1)
testpop[1]['Theor Uptake #D'] = testpop[1]['Dabs_1']
testpop[1]['Conf Interval (#D)'] = testpop[1]['Dabs_std_1']
testpop[1]['pop'] = testpop[1]['pop_1']
testpop[1]['pop_std'] = testpop[1]['pop_std_1']
for pop in range(2,pops+1):
testpop[pop] = testpop[1].copy()
testpop[pop]['Protein State']=testpop[pop].apply(lambda row: row['sample']+'_pop'+str(pop),axis=1)
testpop[pop].loc[testpop[pop]['fit_pops']==pop,['Theor Uptake #D']] = testpop[pop]['Dabs_'+str(pop)]
testpop[pop].loc[testpop[pop]['fit_pops']==pop,['Conf Interval (#D)']] = testpop[pop]['Dabs_std_'+str(pop)]
testpop[pop].loc[testpop[pop]['fit_pops']==pop,['pop']] = testpop[pop]['pop_'+str(pop)]
testpop[pop].loc[testpop[pop]['fit_pops']==pop,['pop_std']] = testpop[pop]['pop_std_'+str(pop)]
uptakedf = pd.DataFrame()
for pop in range(1,pops+1):
uptakedf = pd.concat([uptakedf,testpop[pop]])
uptakedf['#D'] = uptakedf['Theor Uptake #D'] * uptakedf['UN_TD_corr']
uptakedf['%D'] = uptakedf['Theor Uptake #D'] / uptakedf['max_namides'] * 100.0
uptakedf['Deut Time (sec)'] = uptakedf['time']
uptakedf.loc[uptakedf['time']==TD_time,['Deut Time (sec)']] = 'MAX'
uptakedf['Sequence'] = uptakedf['peptide']
uptakedf['Peptide Mass'] = uptakedf['centroid']
uptakedf['maxD'] = uptakedf['max_namides']
uptakedf = uptakedf.drop(columns=['start','end'])
uptakedf['Start'] = uptakedf['start_seq']
uptakedf['End'] = uptakedf['end_seq']
if not proj:
proj = test['sample'].unique()[0]
uptakedf['Protein'] = proj
#filter some garbage data based on TD-UN
uptakedf = uptakedf[uptakedf['UN_TD_corr']<1.05]
return uptakedf
def hdexa_to_pyhdx(data,d_percentage=0.85,protein='protein'):
### Function to convert data table exported from HDExaminer to the processed DynamX format pyHDX expects
### this will leave any extra columns, but will chop out the MAX time points after processing
drop_first=2
def _time_to_sec(tp,tpunit):
return tp * np.power(60.0,'smh'.find(tpunit[0]))
if '# Deut' in data.columns:
data = data.rename(columns={"# Deut":"#D"})
data['#D'] = data['#D'].fillna(0.0)
data['#D'] = data['#D'].astype(float)
if 'Deut %' in data.columns:
data = data.rename(columns={"Deut %":"%D"})
data['%D'] = data['%D'].fillna(0.0)
data['%D'] = data['%D'].astype(float)
if 'Deut Time' in data.columns:
data.loc[data['Deut Time'] == 'FD','Deut Time'] = '1e6s'
data['time unit'] = data['Deut Time'].str[-1]
data['Deut Time (sec)'] = data['Deut Time'].str[:-1].astype(float)
data['Deut Time (sec)'] = data.apply(lambda x: _time_to_sec(tp=x['Deut Time (sec)'],tpunit=x['time unit']),axis=1)
data.loc[data['Deut Time (sec)'] == 1e6,'Deut Time (sec)'] = 'MAX'
if 'Protein' not in data.columns:
data['Protein'] = protein
pyhdx_cols = ['start', 'end' ,'stop' ,'sequence', 'state', 'exposure' ,'uptake' ,'maxuptake',
'fd_uptake' ,'fd_uptake_sd' ,'nd_uptake' ,'nd_uptake_sd' ,'rfu', 'protein',
'modification', 'fragment', 'mhp' ,'center' ,'center_sd' ,'uptake_sd' ,'rt',
'rt_sd' ,'rfu_sd' ,'_sequence' ,'_start' ,'_stop' ,'ex_residues',
'uptake_corrected']
data = data.rename(columns={
"Protein State":"state",
"Protein":"protein",
"Start":"start",
"End":"end",
"Sequence":"_sequence",
"Peptide Mass":"mhp",
"RT (min)":"rt",
"Deut Time (sec)":"exposure",
"maxD":"maxuptake",
"Theor Uptake #D":"uptake_corrected",
"#D":"uptake",
"%D":"rfu",
"Conf Interval (#D)":"rfu_sd",
"#Rep":"rep",
"Confidence":"quality",
"Stddev":"center_sd",
#"p"
})
missing = list(set(pyhdx_cols)-set(data.columns))
for mcol in missing:
data[mcol] = np.nan
if mcol == "rfu_sd": data[mcol] = 0.05 #set 5% error as dummy value
data['rfu']=data['rfu']/100.
data.loc[data['exposure']=="0",'rfu_sd']=0.0
data['stop']=data['end']+1
data['sequence']=data["_sequence"].copy()
data['sequence']=[s.replace("P", "p") for s in data["sequence"]]
# Find the total number of n terminal / c_terminal residues to remove from pyhdx/process.py
n_term = np.array([len(seq) - len(seq[drop_first:].lstrip("p")) for seq in data["sequence"]])
c_term = np.array([len(seq) - len(seq.rstrip("p")) for seq in data["sequence"]])
data["sequence"] = ["x" * nt + s[nt:] for nt, s in zip(n_term, data["sequence"])]
data["_start"] = data["start"] + n_term
data["_stop"] = data["stop"] - c_term
ex_residues = (np.array([len(s) - s.count("x") - s.count("p") for s in data["sequence"]])* d_percentage)
data["ex_residues"] = ex_residues
data["uptake_sd"]=data["center_sd"]
data["nd_uptake"]=0.0
data["nd_uptake_sd"]=0.0
data["modification"]=float("nan")
data["fragment"]=float("nan")
# upeps = data[data["exposure"]=="0"]["_sequence"].unique()
# fpeps = data[data["exposure"]=="MAX"]["_sequence"].unique()
# good_peps = np.array(list(set(upeps) & set(fpeps)))
#peps = data["_sequence"].unique()
states = data["state"].unique()
data["fd_uptake"]="novalue"
data["fd_uptake_sd"]="novalue"
for state in states:
peps = data[data["state"]==state]["_sequence"].unique()
for pep in peps:
try: #may have gotten here without a TD measurement
fd_up = data[(data["_sequence"]==pep) & (data["exposure"]=="MAX")& (data["state"]==state)]['uptake'].iat[0]
fd_up_sd = data[(data["_sequence"]==pep) & (data["exposure"]=="MAX")& (data["state"]==state)]['center_sd'].iat[0]
except:
fd_up = data[(data["_sequence"]==pep) & (data["state"]==state)]['maxuptake'].iat[0]
fd_up_sd = data[(data["_sequence"]==pep) & (data["state"]==state)]['maxuptake'].iat[0]
data.loc[data["_sequence"]==pep, "fd_uptake"]=fd_up
data.loc[data["_sequence"]==pep, "fd_uptake_sd"]=fd_up_sd
data["center"]=data["mhp"]+data["uptake"]
data["rt_sd"]=0.05 #dummy value
data['uptake_corrected_orig'] = data['uptake_corrected']
data['uptake_corrected'] = data["rfu"]*data['maxuptake']
data = data[data["exposure"] != "MAX"]
data = data[data["fd_uptake"] != 0]
data = data[~data["uptake"].isna()]
data["exposure"]=data["exposure"].astype(float)
new_columns = [col for col in pyhdx_cols if col in data.columns] + [col for col in data.columns if col not in pyhdx_cols]
return data[new_columns]
def prepare_kwargs(fit_result):
"""Prepare plot kwargs for fit result"""
d = {
"y": fit_result.d_uptake.mean(axis=0),
"fadedata": np.percentile(fit_result.d_uptake, (5, 95), axis=0),
"shadedata": np.percentile(fit_result.d_uptake, (25, 75), axis=0),
}
return d
## Tweaked from pyhdx.plot.single_linear_bar to leave levels in kwargs
def plot_bar(ax, x, z, cmap, norm, height=1,**kwargs):
"""makes a linear bar plot on supplied axis with values z and corresponding x values x"""
if isinstance(z, pd.Series):
z = z.to_numpy()
elif isinstance(z, pd.DataFrame):
assert len(z.columns) == 1, "Can only plot dataframes with 1 column"
z = z.to_numpy().squeeze()
img = np.expand_dims(z, 0)
collection = ax.pcolormesh(
pplt.edges(x),
np.array([0, height]),
img,
cmap=cmap,
vmin=norm.vmin,
vmax=norm.vmax,
**kwargs,
)
ax.format(yticks=[])
return collection
from pyhdx.plot import peptide_coverage
def plot_coverage(hdxm,states=None,times=None,savepath=None,peprange=None):
## Coverage plots
use_hdxm = hdxm.copy()
if states is None:
states = list(use_hdxm.keys())
if times is None:
times = sorted(use_hdxm[states[0]].timepoints)
fig, axes = pplt.subplots(nrows=len(states),ncols=len(times), axwidth="200mm", sharey=False, refaspect=2,)
for j,mutant in enumerate(states):
for i, use_time in enumerate(times):
time_idx = np.where(hdxm[mutant].timepoints == use_time)[0][0]
filtdf = use_hdxm[mutant][time_idx].data
if peprange: filtdf = filter_range(filtdf,peprange)
peptide_coverage(axes[j,i], filtdf, cbar=True,linewidth=0.1)
t = axes[j,i].set_title(f'{mutant} Peptides t = {int(use_time)}s')
l = axes[j,i].set_xlabel('Residue number')
if savepath:
fig.savefig(savepath,format='pdf',dpi=600)
return
def plot_rfu_residue(hdxm,states=None,times=None,colors=None,savepath=None,legendcols=5):
# Prolines: 7,12,15,19,38,45,50,51,57,85,124,129,147,154,159,166,172,
if colors is None:
colors="#000000 #66C2A5 #56B4E9 #7570B3 #E7298A".split()
if states is None:
states = list(hdxm.keys())
if times is None:
times = sorted(hdxm[states[0]].timepoints)
z_value ={'50':0.674,'68':1.0,'sd':1.0,'80':1.282,'90':1.645,'95':1.96,'98':2.326,'99':2.576} #confidence intervals multiplier
nset = 3 #rfu, redundancy, resolution
#ncols = 2
nrows = len(times)*nset
nfigs = nrows
array=[]
for i in range(1,nfigs+1):
array += [[i,i]]
hratios = [10,1,1]*len(times)
fig, axes = pplt.subplots(array, axheight="30mm",axwidth="180mm", #refaspect=10,
wspace=(0), hspace=([0.7,0.3,3.0]*(len(times)-1)+[0.7,0.3]),
sharex=False, sharey=False, hratios=hratios)
vmin = 0
vmax = 20
kwargs = dict(levels=pplt.arange(0, vmax*3, 1),) #extendsize=2.5, extendrect=True)
res_kwargs = dict(levels=pplt.arange(0, vmax, 1),)
for i, use_time in enumerate(times):
for j,mutant in enumerate(states):
time_idx = np.where(hdxm[mutant].timepoints == use_time)[0][0]
norm = 1#hdxm_hxex[mutant][-1].rfu_residues.values[-1]
x_res = hdxm[mutant][time_idx].r_number
center = hdxm[mutant][time_idx].rfu_residues* 1/norm
#high = center + z_value['95']*hdxm[mutant][time_idx].rfu_residues_sd
high50 = center + z_value['50']*hdxm[mutant][time_idx].rfu_residues_sd
#low = center - z_value['95']*hdxm[mutant][time_idx].rfu_residues_sd
low50 = center - z_value['50']*hdxm[mutant][time_idx].rfu_residues_sd
#axes[i*nset].line(hdxm[mutant][time_idx].r_number,hdxm[mutant][time_idx].rfu_residues * 1/norm, label=str(mutant), color=colors[j])
axes[i*nset].line(x_res,center, shadedata=(low50,high50),label=str(mutant), color=colors[j%len(colors)]) #,fadedata=(low,high)
axes[i*nset].format(xlabel="",xtickrange=(-1,-1))
axes[i*nset].format(title=str(use_time)+'s',titleloc= 'lower right',)
redundancy = hdxm[states[0]].coverage.X.sum(axis=0).astype(float)
resolution = np.repeat(hdxm[states[0]].coverage.block_length, hdxm[states[0]].coverage.block_length)
resolution = resolution.astype(float)
resolution[redundancy == 0] = np.nan
redundancy[redundancy == 0] = np.nan
red = plot_bar(axes[i*nset+1],hdxm[states[0]].coverage.r_number,redundancy,
'blues',pplt.Norm("linear", vmin=vmin, vmax=vmax*3),height=1.0,**kwargs)
axes[i*nset+1].format(xtickrange=(-2,-1))
res = plot_bar(axes[i*nset+2],hdxm[states[0]].coverage.r_number,resolution,
'fire',pplt.Norm("linear", vmin=vmin, vmax=vmax),height=1.0,**res_kwargs)
axes[i*nset].format(ylabel="D-uptake")
axes[i*nset+1].set_ylabel("red.",rotation=0,)#loc='center',labelpad=10)
axes[i*nset+1].yaxis.set_label_coords(-.02,-.05)
axes[i*nset+1].format(xtickloc='none')
axes[i*nset+2].set_ylabel("res.",rotation=0,)#loc='center',labelpad=10)
axes[i*nset+2].yaxis.set_label_coords(-.02,-.05)
#need to implement https://proplot.readthedocs.io/en/latest/subplots.html?highlight=spacing#Spacing-and-tight-layout
fig.colorbar(red, label="Redundancy (peptides including replicates)",width=0.1,loc='b',length=0.5,col=1,ticks=np.arange(0, vmax*3+1, 5))# **kwargs)
fig.colorbar(res, label="Resolution (residues)",width=0.1,loc='b',length=0.5,col=2)# **kwargs)
axes[0].legend(loc="t", ncols=legendcols)
axes.format(ylim=(0,1.1),)#xgrid=True,ygrid=True,ytickrange=(0,1.1))
axes.format(xlim=(0,180))
axes[i*nset+2].format(xlabel="Residue number")
# save_path = os.path.join(project_dir,'B5_phospho_2popFiltShade_RFU_residueavg_plots_'+date+'.pdf')
if savepath:
fig.savefig(savepath,format='pdf',dpi=600)
return #fig
def filter_range(hdxm,peprange):
df = hdxm.copy()
peprange = [peprange] if not isinstance(peprange,list) else peprange
df = df[(df['start'] <= peprange[-1]) & (peprange[0] <= df['end'])]
return df
|