jpayne@1: #!/usr/bin/env python3 jpayne@1: jpayne@1: import argparse jpayne@1: import os,subprocess jpayne@1: import pickle jpayne@1: jpayne@1: ### SeqSero Kmer jpayne@1: def parse_args(): jpayne@1: "Parse the input arguments, use '-h' for help." jpayne@1: parser = argparse.ArgumentParser(usage='Just type "SeqSero2_update_kmer_database.py", it will update kmer database automatically') jpayne@1: return parser.parse_args() jpayne@1: jpayne@1: def reverse_complement(sequence): jpayne@1: complement = { jpayne@1: 'A': 'T', jpayne@1: 'C': 'G', jpayne@1: 'G': 'C', jpayne@1: 'T': 'A', jpayne@1: 'N': 'N', jpayne@1: 'M': 'K', jpayne@1: 'R': 'Y', jpayne@1: 'W': 'W', jpayne@1: 'S': 'S', jpayne@1: 'Y': 'R', jpayne@1: 'K': 'M', jpayne@1: 'V': 'B', jpayne@1: 'H': 'D', jpayne@1: 'D': 'H', jpayne@1: 'B': 'V' jpayne@1: } jpayne@1: return "".join(complement[base] for base in reversed(sequence)) jpayne@1: jpayne@1: def multifasta_dict(multifasta): jpayne@1: multifasta_list = [ jpayne@1: line.strip() for line in open(multifasta, 'r') if len(line.strip()) > 0 jpayne@1: ] jpayne@1: headers = [i for i in multifasta_list if i[0] == '>'] jpayne@1: multifasta_dict = {} jpayne@1: for h in headers: jpayne@1: start = multifasta_list.index(h) jpayne@1: for element in multifasta_list[start + 1:]: jpayne@1: if element[0] == '>': jpayne@1: break jpayne@1: else: jpayne@1: if h[1:] in multifasta_dict: jpayne@1: multifasta_dict[h[1:]] += element jpayne@1: else: jpayne@1: multifasta_dict[h[1:]] = element jpayne@1: return multifasta_dict jpayne@1: jpayne@1: def createKmerDict_reads(list_of_strings, kmer): jpayne@1: kmer_table = {} jpayne@1: for string in list_of_strings: jpayne@1: sequence = string.strip('\n') jpayne@1: for i in range(len(sequence) - kmer + 1): jpayne@1: new_mer = sequence[i:i + kmer].upper() jpayne@1: new_mer_rc = reverse_complement(new_mer) jpayne@1: if new_mer in kmer_table: jpayne@1: kmer_table[new_mer.upper()] += 1 jpayne@1: else: jpayne@1: kmer_table[new_mer.upper()] = 1 jpayne@1: if new_mer_rc in kmer_table: jpayne@1: kmer_table[new_mer_rc.upper()] += 1 jpayne@1: else: jpayne@1: kmer_table[new_mer_rc.upper()] = 1 jpayne@1: return kmer_table jpayne@1: jpayne@1: def multifasta_to_kmers_dict(multifasta): jpayne@1: multi_seq_dict = multifasta_dict(multifasta) jpayne@1: lib_dict = {} jpayne@1: for h in multi_seq_dict: jpayne@1: lib_dict[h] = set( jpayne@1: [k for k in createKmerDict_reads([multi_seq_dict[h]], 27)]) jpayne@1: return lib_dict jpayne@1: jpayne@1: def get_salmid_invA_database(ex_dir): jpayne@1: # read invA kmer and return it jpayne@1: a = open(ex_dir + '/invA_mers_dict', 'rb') jpayne@1: invA_dict = pickle.load(a) jpayne@1: try: jpayne@1: del invA_dict['version'] jpayne@1: except: jpayne@1: pass jpayne@1: return invA_dict jpayne@1: jpayne@1: def get_salmid_rpoB_database(ex_dir): jpayne@1: # read invA kmer and return it jpayne@1: a = open(ex_dir + '/rpoB_mers_dict', 'rb') jpayne@1: rpoB_dict = pickle.load(a) jpayne@1: try: jpayne@1: del rpoB_dict['version'] jpayne@1: except: jpayne@1: pass jpayne@1: return rpoB_dict jpayne@1: jpayne@1: def main(): jpayne@1: args = parse_args() jpayne@1: ex_dir = os.path.dirname(os.path.realpath(__file__)) jpayne@1: lib_dict = multifasta_to_kmers_dict(ex_dir + '/H_and_O_and_specific_genes.fasta') jpayne@1: invA_dict=get_salmid_invA_database(ex_dir) jpayne@1: #rpoB_dict=get_salmid_rpoB_database(ex_dir) jpayne@1: lib_dict_new = lib_dict.copy() jpayne@1: #print(len(lib_dict_new)) jpayne@1: lib_dict_new.update(invA_dict) jpayne@1: #print(len(lib_dict_new)) jpayne@1: #lib_dict_new.update(rpoB_dict) jpayne@1: #print(len(lib_dict_new)) jpayne@1: f = open(ex_dir + '/antigens.pickle', "wb") jpayne@1: pickle.dump(lib_dict_new, f) jpayne@1: f.close() jpayne@1: jpayne@1: if __name__ == '__main__': jpayne@1: main()