-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposition_analysis_Thermo.py
360 lines (249 loc) · 10.9 KB
/
composition_analysis_Thermo.py
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
import pandas as pd
import os
import subprocess as sub
import re
import sys
from Bio import SeqUtils
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
path = os.path.join(os.path.expanduser('~'),'GENOMES_BACTER_RELEASE69/genbank')
# ['DbxRefs','Description','FeaturesNum','GenomicID','GenomicLen','GenomicName','Keywords','NucsPresent','Organism_des',
# 'SourceDbxRefs','SourceOrganism','SourcePlasmid','SourceStrain','Taxonomy','BioProject','TaxonID','Organism_env',
# 'OptimumTemperature','TemperatureRange','OxygenReq','Habitat','Salinity','crit_NC','crit_WGS','crit_genlen',
# 'crit_features','crit_comp_genome','crit_plasmid']
env_dat = pd.read_csv(os.path.join(path,"env_catalog_compgenome.dat"))
#['GenomicID','cDNA','fid','pid','product','protein','status','table','ribosomal','CAI','TrOp']
gen_dat = pd.read_csv(os.path.join(path,"complete_CDS_CAI_DNA.dat"))
# PROTEOME LEVEL AMINO ACID FREQUENCIES ...
# "proteome_all.dat"
# # file with the organisms of interest
# dat_fname = os.path.join(bib2_scr_path,'catalog_with_accesion.dat')
# dat = pd.read_csv(dat_fname)
aacids = sorted(list('CMFILVWYAGTSNQDEHRKP'))
cost_vec_path = path
akashi = os.path.join(cost_vec_path,'akashi-cost.d')
argentina = os.path.join(cost_vec_path,'argentina-cost.d')
akashi_cost = pd.read_csv(akashi,header=None,sep=' ')
argentina_cost = pd.read_csv(argentina,header=None,sep=' ')
thermo_freq = pd.read_csv(os.path.join(path,'thermo.dat'),header=None,sep=' ')
akashi_cost.set_index(0,inplace=True)
argentina_cost.set_index(0,inplace=True)
thermo_freq.set_index(0,inplace=True)
akashi_cost.sort_index(inplace=True)
argentina_cost.sort_index(inplace=True)
thermo_freq.sort_index(inplace=True)
#################################################
# after we processed all organism's genomes, we would need to
# pull out proteins with top XXX percent CAI, to analyse their amino acid compositions ...
#################################################################
#################################################################
# (1) for each asm - open protein detail file, sort by CAI and analyse proteins with top 10% CAI ...
# (2) output analysis results to external file ...
#################################################################
#################################################################
# def get_aausage_proteome(seqrec):
# # seqrec = db[seqrec_id]
# features = seqrec.features
# proteome = []
# for feature in features:
# qualifiers = feature.qualifiers
# if (feature.type == 'CDS')and('translation' in qualifiers):
# proteome.append(qualifiers['translation'][0])
# #return the results ...
# proteome = ''.join(proteome)
# prot_len = float(len(proteome))
# aa_freq = tuple(proteome.count(aa)/prot_len for aa in aacids)
# #
# return (int(prot_len),) + aa_freq
# def analyse_genome(db,seqrec_id):
# seqrec = db[seqrec_id]
# pl_aa_freq = get_aausage_proteome(seqrec)
# gc = SeqUtils.GC(seqrec.seq)
# id = seqrec.id
# return (id,gc) + pl_aa_freq
# PERCENTILE = 0.1
# accounted_GC = []
# aafs = {}
# for aa in aacids:
# aafs[aa] = []
# genome_length = []
# proteome_length = []
# and for each assembley it goes ...
######################
# fname = os.path.join(path_CAI,'%s_genes.dat'%asm)
######################
#
#
gen_dat_org = gen_dat.groupby('GenomicID')
# genom_id = orgs.groups.keys() # env_dat['GenomicID'] ...
# gen_dat_grouped.get_group(idx)
#
# how to get quantile ...
# q75 = pid_cai['CAI'].quantile(q=0.75)
#
#
num_of_quantiles = 5
#
stat_dat = {'GenomicID':[],
'OptimumTemperature':[],
'TrOp':[]}
for i in range(num_of_quantiles):
stat_dat['q%d'%i] = []
stat_dat['R20_q%d'%i] = []
stat_dat['Akashi_q%d'%i] = []
#
#
for idx,topt in env_dat[['GenomicID','OptimumTemperature']].itertuples(index=False):
cds_cai_dat = gen_dat_org.get_group(idx)
# is it a translationally optimized organism ?
all,any = cds_cai_dat['TrOp'].all(),cds_cai_dat['TrOp'].any()
if all == any:
trans_opt = all
else: #any != all
print "%s@T=%f: Something wrong is happening: TrOp flag is not same for all ..."%(idx,topt)
# THIS IS just a stupid precaution measure, in case we messed something upstream ...
# not that stupid after all, because NaN is behaving badly here ...
if cds_cai_dat['TrOp'].notnull().all():
#
# we can use this 'qcut' function from pandas to divide our proteins by the quantiles ...
category,bins = pd.qcut(cds_cai_dat['CAI'],q=num_of_quantiles,retbins=True,labels=False)
#
stat_dat['GenomicID'].append(idx)
stat_dat['OptimumTemperature'].append(topt)
stat_dat['TrOp'].append(trans_opt)
#
# then we could iterate over proteins/cDNAs in these categories ...
for cat in range(num_of_quantiles):
cds_cai_category = cds_cai_dat[category==cat]
total_length = cds_cai_category['protein'].str.len().sum()
IVYWREL = sum(cds_cai_category['protein'].str.count(aa).sum() for aa in list('IVYWREL'))
# IVYWREL = cds_cai_category['protein'].str.count('|'.join("IVYWREL")).sum() # tiny bit slower ...
f_IVYWREL = float(IVYWREL)/float(total_length)
# 20-vector for of amino acid composition ...
aa_freq_20 = np.true_divide([cds_cai_category['protein'].str.count(aa).sum() for aa in aacids],float(total_length))
# slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
_1,_2,R20,_4,_5 = stats.linregress(aa_freq_20, thermo_freq[1])
# Akashi ...
cost = np.dot(aa_freq_20,akashi_cost[1])
# appending ...
#
#
stat_dat['q%d'%cat].append(f_IVYWREL)
stat_dat['R20_q%d'%cat].append(R20)
stat_dat['Akashi_q%d'%cat].append(cost)
#
#
#
cai_stats_quant = pd.DataFrame(stat_dat)
#
cai_stats_quant_TrOp = cai_stats_quant[cai_stats_quant.TrOp]
cai_stats_quant_noTrOp = cai_stats_quant[~cai_stats_quant.TrOp]
plt.clf()
bins = np.linspace(-0.05,0.05,50)
plt.hist(list(cai_stats_quant_TrOp.q4 - cai_stats_quant_TrOp.q1),bins=bins,color='blue')
plt.hist(list(cai_stats_quant_noTrOp.q4 - cai_stats_quant_noTrOp.q1),bins=bins,color='red',alpha=0.8)
plt.show()
plt.clf()
bins = np.linspace(-0.15,0.15,50)
# bins=50
plt.hist(list(cai_stats_quant[cai_stats_quant.OptimumTemperature<=50].R20_q4 - cai_stats_quant[cai_stats_quant.OptimumTemperature<=50].R20_q1),bins=bins,color='black',cumulative=False)
plt.hist(list(cai_stats_quant_noTrOp[cai_stats_quant_noTrOp.OptimumTemperature<=50].R20_q4 - cai_stats_quant_noTrOp[cai_stats_quant_noTrOp.OptimumTemperature<=50].R20_q1),bins=bins,color='blue',cumulative=False)
plt.hist(list(cai_stats_quant_TrOp[cai_stats_quant_TrOp.OptimumTemperature<=50].R20_q4 - cai_stats_quant_TrOp[cai_stats_quant_TrOp.OptimumTemperature<=50].R20_q1),bins=bins,color='red',alpha=0.8,cumulative=False)
plt.xlabel('$R^{4}_{T} - R^{1}_{T}$')
# plt.hist(list(cai_stats_quant_TrOp.R20_q4 - cai_stats_quant_TrOp.R20_q1),bins=bins,color='blue')
# plt.hist(list(cai_stats_quant_noTrOp.R20_q4 - cai_stats_quant_noTrOp.R20_q1),bins=bins,color='red',alpha=0.8)
plt.show()
plt.clf()
plt.plot(cai_stats_quant.OptimumTemperature,cai_stats_quant.q1,'bo',alpha=0.8)
plt.plot(cai_stats_quant.OptimumTemperature,cai_stats_quant.q4,'ro',alpha=0.8)
plt.show()
# #
plt.clf()
plt.plot(cai_stats_quant.OptimumTemperature,cai_stats_quant.R20_q1,'bo',alpha=0.8)
plt.plot(cai_stats_quant.OptimumTemperature,cai_stats_quant.R20_q4,'ro',alpha=0.8)
plt.show()
plt.clf()
plt.plot(cai_stats_quant.GC,cai_stats_quant.R20_q1,'bo',alpha=0.8)
plt.plot(cai_stats_quant.GC,cai_stats_quant.R20_q4,'ro',alpha=0.8)
plt.show()
plt.clf()
for i in range(num_of_quantiles):
k1 = 'q%d'%i
k2 = 'R20_q%d'%i
k3 = 'Akashi_q%d'%i
#
plt.plot([i+1,]*cai_stats_quant.shape[0],cai_stats_quant[k1],alpha=0.7)
plt.xlim(0,6)
plt.clf()
for i in range(num_of_quantiles):
k1 = 'q%d'%i
k2 = 'R20_q%d'%i
k3 = 'Akashi_q%d'%i
#
plt.errorbar([i+1,],cai_stats_quant_noTrOp[cai_stats_quant_noTrOp.OptimumTemperature>0][k2].mean(),yerr=cai_stats_quant_noTrOp[cai_stats_quant_noTrOp.OptimumTemperature>0][k2].std(),fmt='o')
plt.xlim(0,6)
plt.show()
# R20 grows on average,
# | meso thermo
# ------+-------------
# TrOp | ++ ~+
# noTrOp| + ~
# Akashi is declining on average
# | meso thermo
# ------+-------------
# TrOp | -- --
# noTrOp| ~- ~-
# IVYWREL is declining on average
# | meso thermo
# ------+-------------
# TrOp | -- ~-
# noTrOp| ~- -
# After reshuffling given CAI, everything becomes flat and 0-centered with much narrower distributions
# #
# # # move on ...
# # # quantiles calculation ...
# # q20,q40,q60,q80 = cds_cai_dat['CAI'].quantile(q=[0.2,0.4,0.6,0.8])
# # #
# # q1_idx = (cds_cai_dat['CAI']<=q20)
# # q2_idx = (q20<cds_cai_dat['CAI'])&(cds_cai_dat['CAI']<=q40)
# # q3_idx = (q40<cds_cai_dat['CAI'])&(cds_cai_dat['CAI']<=q60)
# # q4_idx = (q60<cds_cai_dat['CAI'])&(cds_cai_dat['CAI']<=q80)
# # q5_idx = (q80<cds_cai_dat['CAI'])
# # # q3_idx = q40<cds_cai_dat['CAI']<=q60
# # # q4_idx = q60<cds_cai_dat['CAI']<=q80
# # # q5_idx = q80<cds_cai_dat['CAI']
# # ['q1', 'q2', 'q3', 'q4', 'q5']
# for IndexId,prot_det in dat[['IndexId','protein_details']].get_values():
# ###################
# # in the case of Bacteria, we know for sure that a single accession number refers to related
# if prot_det:
# # open a file with the analysed organismal proteins...
# protein_fname = os.path.join(path_CAI,'%s_genes.dat'%IndexId)
# # load the data ...
# protein_dat = pd.read_csv(protein_fname)
# # "prot_id,cai,gene_product,gene_seq,prot_seq" are the columns ...
# # we'll be taking proteins with top PERCENTILE% CAI in the list ...
# # protein total number is the first dimension of the table here ...
# number_of_proteins,_ = protein_dat.shape
# accounted_proteins = int(number_of_proteins*PERCENTILE)
# # top PERCENTILE proteins will be considered for analysis ...
# accounted_data = protein_dat.sort(columns='cai',ascending=False)[:accounted_proteins]
# # analyse that stuff ...
# cai_proteome = ''.join(accounted_data['prot_seq'])
# cai_proteome_len = float(len(cai_proteome))
# cai_genome = ''.join(accounted_data['gene_seq'])
# #
# accounted_GC.append(SeqUtils.GC(cai_genome))
# #
# for aa in aacids:
# aafs[aa].append(cai_proteome.count(aa)/cai_proteome_len)
# else:
# # no protein details exist, no corresponding file exist at all...
# accounted_GC.append(0.0)
# for aa in aacids:
# aafs[aa].append(0.0)
# dat['GC'] = accounted_GC
# for aa in aacids:
# dat[aa] = aafs[aa]
# dat.to_csv('cai5_bacter.dat',index=False)