annotate SeqSero2_update_kmer_database.py @ 1:9811f8cd313d draft

planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
author cstrittmatter
date Tue, 21 Apr 2020 12:49:07 -0400
parents fc22ec8e924e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
1 #!/usr/bin/env python3
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
2
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
3 import argparse
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
4 import os,subprocess
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
5 import pickle
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
6
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
7 ### SeqSero Kmer
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
8 def parse_args():
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
9 "Parse the input arguments, use '-h' for help."
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
10 parser = argparse.ArgumentParser(usage='Just type "SeqSero2_update_kmer_database.py", it will update kmer database automatically')
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
11 return parser.parse_args()
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
12
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
13 def reverse_complement(sequence):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
14 complement = {
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
15 'A': 'T',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
16 'C': 'G',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
17 'G': 'C',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
18 'T': 'A',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
19 'N': 'N',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
20 'M': 'K',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
21 'R': 'Y',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
22 'W': 'W',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
23 'S': 'S',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
24 'Y': 'R',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
25 'K': 'M',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
26 'V': 'B',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
27 'H': 'D',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
28 'D': 'H',
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
29 'B': 'V'
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
30 }
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
31 return "".join(complement[base] for base in reversed(sequence))
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
32
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
33 def multifasta_dict(multifasta):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
34 multifasta_list = [
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
35 line.strip() for line in open(multifasta, 'r') if len(line.strip()) > 0
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
36 ]
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
37 headers = [i for i in multifasta_list if i[0] == '>']
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
38 multifasta_dict = {}
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
39 for h in headers:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
40 start = multifasta_list.index(h)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
41 for element in multifasta_list[start + 1:]:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
42 if element[0] == '>':
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
43 break
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
44 else:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
45 if h[1:] in multifasta_dict:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
46 multifasta_dict[h[1:]] += element
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
47 else:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
48 multifasta_dict[h[1:]] = element
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
49 return multifasta_dict
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
50
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
51 def createKmerDict_reads(list_of_strings, kmer):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
52 kmer_table = {}
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
53 for string in list_of_strings:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
54 sequence = string.strip('\n')
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
55 for i in range(len(sequence) - kmer + 1):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
56 new_mer = sequence[i:i + kmer].upper()
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
57 new_mer_rc = reverse_complement(new_mer)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
58 if new_mer in kmer_table:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
59 kmer_table[new_mer.upper()] += 1
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
60 else:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
61 kmer_table[new_mer.upper()] = 1
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
62 if new_mer_rc in kmer_table:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
63 kmer_table[new_mer_rc.upper()] += 1
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
64 else:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
65 kmer_table[new_mer_rc.upper()] = 1
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
66 return kmer_table
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
67
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
68 def multifasta_to_kmers_dict(multifasta):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
69 multi_seq_dict = multifasta_dict(multifasta)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
70 lib_dict = {}
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
71 for h in multi_seq_dict:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
72 lib_dict[h] = set(
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
73 [k for k in createKmerDict_reads([multi_seq_dict[h]], 27)])
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
74 return lib_dict
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
75
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
76 def get_salmid_invA_database(ex_dir):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
77 # read invA kmer and return it
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
78 a = open(ex_dir + '/invA_mers_dict', 'rb')
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
79 invA_dict = pickle.load(a)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
80 try:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
81 del invA_dict['version']
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
82 except:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
83 pass
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
84 return invA_dict
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
85
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
86 def get_salmid_rpoB_database(ex_dir):
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
87 # read invA kmer and return it
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
88 a = open(ex_dir + '/rpoB_mers_dict', 'rb')
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
89 rpoB_dict = pickle.load(a)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
90 try:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
91 del rpoB_dict['version']
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
92 except:
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
93 pass
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
94 return rpoB_dict
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
95
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
96 def main():
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
97 args = parse_args()
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
98 ex_dir = os.path.dirname(os.path.realpath(__file__))
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
99 lib_dict = multifasta_to_kmers_dict(ex_dir + '/H_and_O_and_specific_genes.fasta')
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
100 invA_dict=get_salmid_invA_database(ex_dir)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
101 #rpoB_dict=get_salmid_rpoB_database(ex_dir)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
102 lib_dict_new = lib_dict.copy()
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
103 #print(len(lib_dict_new))
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
104 lib_dict_new.update(invA_dict)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
105 #print(len(lib_dict_new))
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
106 #lib_dict_new.update(rpoB_dict)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
107 #print(len(lib_dict_new))
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
108 f = open(ex_dir + '/antigens.pickle', "wb")
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
109 pickle.dump(lib_dict_new, f)
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
110 f.close()
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
111
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
112 if __name__ == '__main__':
fc22ec8e924e planemo upload commit 6b0a9d0f0ef4bdb0c2e2c54070b510ff28125f7a
cstrittmatter
parents:
diff changeset
113 main()