-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetLocus
More file actions
executable file
·111 lines (92 loc) · 3.74 KB
/
Copy pathgetLocus
File metadata and controls
executable file
·111 lines (92 loc) · 3.74 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
#!/usr/bin/python
'''
From a bunch of genbank files, obtain a series of files with:
prot_id -> locus [-> dna_id -> replicon]
To obtain the dna-to-replicon conversion, a series of tabular files are needed:
dna_id -> replicon
Also, a series of fasta files with coordinates are needed to0:
>fake_dna_id START
AGGATCGATAGGC
'''
def getOptions():
import argparse
# create the top-level parser
description = ("From a bunch of genbank files, obtain a prot_id to locus_tag conversion")
parser = argparse.ArgumentParser(description = description)
parser.add_argument('gbkdir', action='store',
help='Genbank directory')
parser.add_argument('mapdir', action='store', nargs='?',
default=None,
help='Mapped replicons directory')
parser.add_argument('mapfastadir', action='store', nargs='?',
default=None,
help='Mapped replicons directory (with fasta)')
return parser.parse_args()
options = getOptions()
from Bio import SeqIO
import os
import sys
d = {}
df = {}
if options.mapdir is not None:
# Put the organisms name into a list
orgs = {x.split('.')[0] for x in os.listdir(options.gbkdir)}
# Get the dna_id to replicon dictionary
filez = os.listdir(options.mapdir)
for org in orgs:
if org+'.tab' in filez:
d[org] = {}
for l in open(os.path.join(options.mapdir, org+'.tab')):
try:
did, rep = l.strip().split('\t')
d[org][did] = rep
except:continue
# Is it a "fake" draft?
if options.mapfastadir is not None:
filez = os.listdir(options.mapfastadir)
for org in orgs:
if org+'.fa' in filez:
df[org] = []
for s in SeqIO.parse(os.path.join(options.mapfastadir, org+'.fa'), 'fasta'):
start = int(s.description.split()[1])
df[org].append((start, s.id))
for org in df:
df[org] = sorted(df[org], key=lambda x: x[0])
# Get the actual job done
for f1 in os.listdir(options.gbkdir):
org = f1.split('.')[0]
o = open('%s.tab'%org, 'w')
f2 = os.path.join(options.gbkdir, f1)
for s in SeqIO.parse(f2, 'genbank'):
for f in filter(lambda x: x.type == 'CDS', s.features):
out = []
if 'protein_id' in f.qualifiers:
out.append(f.qualifiers['protein_id'][0])
else:
out.append(f.qualifiers['locus_tag'][0])
out.append(f.qualifiers['locus_tag'][0])
if options.mapdir:
out.append(s.id)
if org in d and org not in df:
out.append(d[org][s.id])
else:
start = int(f.location.start)
stop = int(f.location.end)
found = False
for a, b in zip(df[org], df[org][1:]+[df[org][0]]):
pos, contig = a[0], a[1]
pos1, contig1 = b[0], b[1]
if start >= pos and stop <= pos1:
found = True
break
elif pos1 < pos:
if start >= pos:
found = True
break
if found is True:
out.append(d[org][contig])
else:
print '%d - %d not found in %s'%(start, stop, org)
out.append('UnMapped')
o.write('\t'.join(out) + '\n')
o.close()