This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakefile
175 lines (127 loc) · 5.52 KB
/
Snakefile
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
import glob
import os
import shutil
import Bio.SeqIO
import Bio.Blast.NCBIXML
from Bio.Blast.Applications import NcbiblastnCommandline
# --- Configuration -------------------------------
REFERENCE = config.get("reference", os.path.basename(next(glob.iglob("reference/*"))))
CLEAR = config.get("clear", False)
IDENTITY_THRESHOLD = float(config.get("identity", 0.98))
assert 0 <= IDENTITY_THRESHOLD <= 1, "`identity` must be between 0 and 1"
# --- Maintenance rules ------------------------------------------------------
rule all:
"""Annotate all sequences in the `input` directory.
"""
input:
[x.replace("input", "output").replace(".gb", ".annotated.gb") for x in glob.iglob("input/*.gb")]
rule clean:
"""Remove temporary files.
"""
input:
filter(os.path.exists, ("blast", "catalog", "db", "features", "output"))
run:
for directory in input:
shutil.rmtree(directory)
# --- Step 1: Extract features from reference GenBank files ------------------
rule extractFeatures:
"""Build a catalog of features from reference sequences.
"""
input:
"reference/{reference}"
output:
directory("features/{reference}")
run:
features = {}
# extract each feature into an individual record
for sequence in glob.iglob(os.path.join(input[0], "*.gb")):
record = Bio.SeqIO.read(sequence, 'genbank')
for idx, feat in enumerate(record.features):
# NB: do not use `feat.extract` to keep the feature itself
feat_record = record[feat.location.start:feat.location.end]
feat_record.features = [f for f in feat_record.features if f.location.start == 0 and f.location.end >= len(feat_record)]
if feat.strand < 0:
feat_record = feat_record.reverse_complement(features=True)
feat_record.id = feat_record.name = "{}.{}".format(record.id, idx)
feat_record.description = ""
features[str(feat_record.seq)] = feat_record
# write each feature as an individual genbank file
os.makedirs(output[0], exist_ok=True)
for feat_record in features.values():
with open(os.path.join(output[0], "{}.gb".format(feat_record.id)), 'w') as f:
Bio.SeqIO.write(feat_record, f, 'genbank')
# --- Step 2: Build a FASTA catalog of reference features --------------------
rule makeCatalog:
"""Build a FASTA catalog of sequences from a directory of GenBank files.
"""
input:
"features/{reference}"
output:
"catalog/{reference}.fa"
run:
features = [Bio.SeqIO.read(gb, 'gb') for gb in glob.iglob(os.path.join(input[0], "*.gb"))]
with open(output[0], 'w') as f:
Bio.SeqIO.write(features, f, 'fasta')
# --- Step 3: Build a BlastDB of reference features --------------------------
rule makeBlastDb:
"""Build a BLASTn database from a catalog of sequences.
"""
input:
"catalog/{reference}.fa"
output:
expand("db/{{reference}}.{ext}", ext=["nhr", "nin", "nsq"])
log:
"db/{reference}.log"
shell:
"makeblastdb -dbtype nucl -out db/{wildcards.reference} -title 'Annotations' -in {input} 2>&1 >{log}"
# --- Step 4: Run each input sequence against the database of features -------
rule runBlastN:
"""Run a BLASTn query against a reference BLASTn database.
"""
input:
"input/{sequence}.gb", "db/{reference}.nhr"
output:
"blast/{sequence}.{reference}.blastxml"
log:
"blast/{sequence}.{reference}.log"
shell:
"blastn -task blastn-short -ungapped -outfmt 5 -db db/{wildcards.reference} -query {input[0]} >{output} 2>{log}"
# --- Step 5: Copy features detected with `blastn` to the query --------------
rule copyFeatures:
"""Copy features from reference sequences using BLASTn results as a guide.
"""
input:
"input/{sequence}.gb",
expand("blast/{{sequence}}.{reference}.blastxml", reference=REFERENCE),
expand("features/{reference}", reference=REFERENCE)
output:
"output/{sequence}.annotated.gb"
run:
# load the query record input sequence
record = Bio.SeqIO.read(input[0], "genbank")
with open(input[1]) as handle:
result = next(Bio.Blast.NCBIXML.parse(handle))
# remove existing features if desired
if CLEAR:
record.features.clear()
# FIXME: copy features only if alignment has 100% identity
for alignment in result.alignments:
hsp = alignment.hsps[0]
if (hsp.identities / hsp.align_length) > IDENTITY_THRESHOLD:
# get the GenBank file corresponding to the feature
hit_gb = os.path.join(input[2], "{}.gb".format(alignment.hit_def))
hit_record = Bio.SeqIO.read(hit_gb, "genbank")
# use the reverse complement if it is on the reverse strand
if hit_record.seq not in record.seq:
hit_record = hit_record.reverse_complement(features=True)
# position the feature on the query record
try:
hit_feat = hit_record.features[0]
hit_feat.location += record.seq.find(hsp.query)
except IndexError:
continue
# add the feature to the query record
record.features.append(hit_feat)
# write the query record
with open(output[0], 'w') as f:
Bio.SeqIO.write(record, f, "genbank")