/
te_score_plots.py
executable file
·208 lines (175 loc) · 7.76 KB
/
te_score_plots.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
#!/usr/bin/env python
from optparse import OptionParser
import pandas as pd
import statsmodels.api as sm
import os, sys, subprocess, collections
import ggplot
import tempura
################################################################################
# te_score_plots.py
#
# Given a BED file containing scores and a MSA fasta file, perform a regression
# on the nucleotides at each position in the MSA against the scores.
################################################################################
################################################################################
# main
################################################################################
def main():
usage = 'usage:%prog [options] <bed_file> <msa_file>'
parser = OptionParser(usage)
parser.add_option('-c', dest='consensus_pct', default=0.5, type='float', help='Required proportion of columns with a valid nt to consider it a consensus column [Default: %default]')
parser.add_option('-m', dest='model_output_file', default='ols_summary.txt', help='The file to write the model summary')
parser.add_option('-p', dest='plot_output_file', default='weights_plot.pdf', help='The file to print the plot of index versus weight')
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error('Must provide BED file with scores and MSA fasta file')
else:
bed_file = args[0]
msa_fasta_file = args[1]
##################################################
# hash scores
##################################################
seq_scores = {}
for line in open(bed_file):
a = line.split('\t')
header = a[3]
score = float(a[4])
seq_scores[header] = score
##################################################
# define consensus
##################################################
consensus_columns = define_consensus(msa_fasta_file, options.consensus_pct)
##################################################
# map sequences to feature vectors
##################################################
quaternary_conversion_dict = {'A':[1,0,0], 'C':[0,1,0], 'G':[0,0,1], 'T':[0,0,0], 'N':[0.25,0.25,0.25], '.':[0.25,0.25,0.25], '-':[0.25,0.25,0.25]}
# initialize the dictionary with score and position/nt features
df_dict = {'Score':[]}
for i in range(len(consensus_columns)):
position = str(i+1)
df_dict[position+'_A'] = []
df_dict[position+'_C'] = []
df_dict[position+'_G'] = []
header = ''
for line in open(msa_fasta_file):
if line[0] == '>':
if header and header != 'Consensus':
# process seq
df_dict['Score'].append(seq_scores[header])
for i in range(len(consensus_columns)):
position = str(i+1)
seq_i = consensus_columns[i]
nt = seq[seq_i].upper()
nt_conv = quaternary_conversion_dict[nt]
df_dict[position+'_A'].append(nt_conv[0])
df_dict[position+'_C'].append(nt_conv[1])
df_dict[position+'_G'].append(nt_conv[2])
header = line[1:].rstrip()
seq = ''
else:
seq += line.rstrip()
if header and header != 'Consensus':
# process last seq
df_dict['Score'].append(seq_scores[header])
for i in range(len(consensus_columns)):
position = str(i+1)
seq_i = consensus_columns[i]
nt = seq[seq_i].upper()
nt_conv = quaternary_conversion_dict[nt]
df_dict[position+'_A'].append(nt_conv[0])
df_dict[position+'_C'].append(nt_conv[1])
df_dict[position+'_G'].append(nt_conv[2])
##################################################
# perform learning
##################################################
# add y-intercept term
df_dict['Const'] = [1]*len(df_dict['Score'])
df = pd.DataFrame(df_dict)
score = df['Score']
X = df.drop('Score', axis=1)
print >> sys.stderr, 'Read in all the sequences and scores. Now fitting the model'
mod = sm.OLS(score, X)
res = mod.fit()
model_output_file = open(options.model_output_file,'w')
print >> model_output_file, res.summary()
model_output_file.close()
print >> sys.stderr, 'Fit an OLS model and print the summary to %s' %(options.model_output_file)
##################################################
# read output
##################################################
position_weights = collections.defaultdict(list)
flag = False
for line in open(options.model_output_file, 'r'):
if line[0:2] == '==':
flag = False
elif line[0:2] == '--':
flag = True
elif flag:
contents = line.split()
if contents[0] != 'Const':
position = int(contents[0].split('_')[0])
weight = float(contents[1])
position_weights[position].append(weight)
df_dict = {'Position':[], 'Nucleotide':[], 'Weight':[]}
#print '\t'.join(df_dict.keys())
for position in position_weights.keys():
weight_A, weight_C, weight_G = position_weights[position]
weight_T = 0.0
nucleotide_weights = [weight_A, weight_C, weight_T, weight_G]
nucleotide_order = ['A','C','T','G']
min_weight = min(nucleotide_weights)
for i in range(0, len(nucleotide_weights)):
nucleotide_weights[i] = nucleotide_weights[i] - min_weight
for i in range(0, len(nucleotide_weights)):
df_dict['Position'].append(position)
df_dict['Nucleotide'].append(nucleotide_order[i])
df_dict['Weight'].append(nucleotide_weights[i])
#print '\t'.join([str(position), nucleotide_order[i], str(nucleotide_weights[i])])
print >> sys.stderr, 'Now plotting the weights of different nucleotides along each position'
ggplot.plot('%s/te_score_plots.r' % tempura.r_dir, df_dict, [options.plot_output_file])
print >> sys.stderr, 'All Done. Check output files'
################################################################################
# define_consensus
#
# Input
# msa_fasta_file:
# consensus_pct: Float above which we consider the column to be consensus.
#
# Output
# consensus_cols: List of consensus column indexes.
################################################################################
def define_consensus(msa_fasta_file, consensus_pct):
valid_nts = ['A','C','G','T']
column_counts = []
header = ''
seq_count = 0
for line in open(msa_fasta_file):
if line[0] == '>':
if header and header != 'Consensus': # avoid DFAM
seq_count += 1
for i in range(len(seq)):
if seq[i].upper() in valid_nts:
while i >= len(column_counts):
column_counts.append(0)
column_counts[i] += 1
header = line[1:].rstrip()
seq = ''
else:
seq += line.rstrip()
if header and header != 'Consensus':
seq_count += 1
for i in range(len(seq)):
if seq[i].upper() in valid_nts:
while i >= len(column_counts):
column_counts.append(0)
column_counts[i] += 1
consensus_columns = []
for i in range(len(column_counts)):
if column_counts[i] / float(seq_count) > consensus_pct:
consensus_columns.append(i)
return consensus_columns
################################################################################
# __main__
################################################################################
if __name__ == '__main__':
main()