-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment2entropy.py
More file actions
185 lines (155 loc) · 6.97 KB
/
alignment2entropy.py
File metadata and controls
185 lines (155 loc) · 6.97 KB
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
'''
alignment2entropy.py - summarize information in a multiple sequence alignment
=============================================================================
:Author: Jethro Johnson
:Release: $Id$
:Date: |today|
:Tags: Metagenomics
Purpose
-------
When designing primers to work with commonly amplified regions in
metagenomic studies (e.g. 16S, 18S, ITS), it is of interest to know
which subregions contain information useful for distinguishing certain
taxa.
This script takes a multiple sequence alignment and outputs a table
containing the shannon entropy at each base position in the alignment.
If -r/--variants is specified, output will report whether there is a
substitution (X) or indel (I) at each alignment position.
There is also the option to supply a tab-separated taxonomy file, in
which the first column corresponds to sequences in the alignment, and
subsequent columns correspond to taxonomic levels. If this file is
present and a taxonomic level specified, then the per-base shannon
entropy is calculated separately for each unique taxa at that taxonomic
level.
'''
import cgatcore.experiment as E
import cgatcore.iotools as IOTools
import pandas as pd
import sys
import re
import numpy as np
from scipy.stats import entropy
from Bio import AlignIO
from Bio.Align import MultipleSeqAlignment
from collections import Counter
def main(argv=None):
if argv is None:
argv = sys.argv
parser = E.OptionParser(
version="%prog version: $Id$",
usage=globals()["__doc__"])
parser.add_option("-a", "--alignment-file", dest="alignment_file",
help="an aligned fasta file")
parser.add_option("-t", "--tax-table", dest="tax_table",
help="tab separated table of taxonomy (with headers)")
parser.add_option("-b", "--baseline", dest="base_sequence",
help="name of the baseline sequence against which to"
" base comparisons")
parser.add_option("-l", "--tax-level", dest="tax_level",
help="taxonomic level at which to summarize entropy, must"
" correspond to a header in the taxonomy file")
parser.add_option("-m", "--min-occurence", dest="min_replicates",
help="minimum number of sequences that must occur"
" for entropy to be calculated for a particular taxon")
parser.add_option("-s", "--sliding-window", dest="sliding_window",
help="option supply window for smoothing entropy")
parser.add_option("-o", "--out-table", dest="out_table",
help="location of table to be written out")
parser.add_option("-r", "--variants", dest="variants", action="store_true",
help="if selected, output indels & substitutions rather than entropy")
parser.set_defaults(base_sequence=None,
tax_table=None,
tax_level="Phylum",
min_replicates=5,
sliding_window=None,
)
(options, args) = E.start(parser)
# read the infiles
if options.tax_table:
tax_df = pd.read_table(IOTools.open_file(options.tax_table),
header=0,
index_col=0)
align_fa = AlignIO.read(IOTools.open_file(options.alignment_file),
format='fasta')
# fetch the base sequence, if there is one
if options.base_sequence:
ids = [x.id for x in align_fa]
base = align_fa[ids.index(options.base_sequence)]
index = [i for i, b in enumerate(base.seq.__str__()) if b != '-']
else:
index = np.arange(align_fa.get_alignment_length())
# set up dataframe to contain entropy calculations
if options.tax_table and options.tax_level:
columns = ['Entropy',]
for tax in set(tax_df[options.tax_level]):
n = tax_df[options.tax_level].tolist()
if n.count(tax) >= int(options.min_replicates):
columns.append(tax)
else:
E.warn('Skipping taxon %s... too few sequences (%i) in the'
' taxonomy file' % (tax, n.count(tax)))
else:
columns = ['Entropy',]
df_entropy = pd.DataFrame(columns=columns, index=index)
# write entropy to dataframe
to_drop = []
for tax in df_entropy.columns.tolist():
if tax == 'Entropy':
sequence_ids = [x.id for x in align_fa]
else:
sequence_ids = tax_df[tax_df[options.tax_level] \
== tax].index.tolist()
seq_records = []
n = 0
for sequence in align_fa:
if sequence.id in sequence_ids:
seq_records.append(sequence)
n += 1
elif options.base_sequence and \
sequence.id == options.base_sequence:
seq_records.append(sequence)
else:
continue
E.info('There are %i sequences in alignment for %s' % (n, tax))
# drop taxa for which there are too few sequences in alignment
# (...catches cases where sequences in tax file are absent in
# alignment)
if len(seq_records) < int(options.min_replicates):
E.warn('Skipping taxon %s... too few sequences (%i) in the'
' alignment file' % (tax, len(seq_records)))
to_drop.append(tax)
continue
# Iterate across alignment and calculate either indel/subs or entropy
sub_fa = MultipleSeqAlignment(seq_records)
for i in df_entropy.index.tolist():
string = sub_fa[:,i]
assert len(set('ACTGU-') - set(string)) >= 0
string = Counter(sub_fa[:,i])
if options.variants:
if len(string.keys()) > 1:
if len(string.keys()) > 2 and string.get('-'):
ent = "I,X"
elif len(string.keys()) == 2 and string.get('-'):
ent = "I"
else:
ent = "X"
else:
ent = ""
else:
ent = entropy([x/ float(len(string)) for x in string.values()])
df_entropy.loc[i, tax] = ent
# drop taxa & reset index to match positions in base sequence
df_entropy.drop(to_drop, axis=1, inplace=True)
df_entropy.reset_index(drop=True, inplace=True)
# smooth entropy
if options.sliding_window:
if options.variants:
raise IOError('Cannot select sliding window and variants')
df_entropy = pd.rolling_mean(df_entropy,
center=True,
window=int(options.sliding_window))
df_entropy.to_csv(IOTools.open_file(options.out_table, 'w'),
sep='\t',
index_label='Base_Position')
if __name__ == "__main__":
sys.exit(main(sys.argv))