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