-
Notifications
You must be signed in to change notification settings - Fork 3
/
silva-survey.py
executable file
·117 lines (102 loc) · 3.31 KB
/
silva-survey.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
#!/usr/bin/env python3
from collections import defaultdict
import time
import re
import os
import sys
import textwrap
import argparse
import urllib.request
import ssl
from urllib.error import HTTPError
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
parser = argparse.ArgumentParser(
prog="silva-survey.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Developed by Arkadiy Garber; University of Delaware, Geological Sciences
Please send comments and inquiries to arkg@udel.edu
'''))
parser.add_argument('-taxa', help='taxanomic affiliation of organism that you would like to investigate')
parser.add_argument('-silva_DB', help="location of SILVA database")
parser.add_argument('-out_dir', help="directory to which the output file will be written")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
def replace(stringOrlist, list, item):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
emptyList.append(item)
outString = "".join(emptyList)
return outString
def remove(stringOrlist, list):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
pass
outString = "".join(emptyList)
return outString
def fasta(fasta_file):
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
seq = ''
else:
header = i[1:]
seq = ''
else:
seq += i
Dict[header] = seq
return Dict
silva = open(args.silva_DB, "r")
silva =fasta(silva)
outfile = open(args.out_dir + "/" + args.taxa + "_SILVA.csv", "w")
outfile.write(args.taxa + "_strain" + "," + "Source" + "," + "Reference" + "," + "16S_seq" + "\n")
for i in silva.keys():
if re.findall(args.taxa, i):
print(i)
time.sleep(2)
outfile.write(i + ",")
id = i.split(" ")[0]
id = id.split(".")[0]
fp = urllib.request.urlopen("https://www.ebi.ac.uk/ena/data/view/%s&display=text" % id, context=gcontext)
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()
lines = mystr.split('\n')
string = ''
count = 0
ref = ''
for j in lines:
if re.findall(r'isolation_source', j) or re.findall(r'tissue_type', j) or \
re.findall(r'Source of Isolate', j):
count += 1
try:
string = j.split("\"")[1]
string = replace(string, [","], ";")
except IndexError:
string = j
string = replace(j, [","], ";")
count += 1
if re.match(r'^RT', j) and len(j) > 6:
ref += j
if count == 0:
outfile.write("No_data" + ",")
else:
outfile.write(replace(string, [","], ";") + ",")
ref = (replace(ref, [","], ";"))
ref = remove(ref, ["RT"])
outfile.write(ref + ",")
outfile.write(silva[i] + "\n")