-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmer_reduction.py
132 lines (119 loc) · 3.81 KB
/
kmer_reduction.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import sys
import os
import argparse
import hashlib
from Bio import SeqIO
def hash_seqid(seqid):
hash = hashlib.sha1(seqid.encode("UTF-8")).hexdigest()[:16]
fasta_ids[hash] = seqid
return hash
def split_to_kmers(k, rec, hash):
length = len(rec)
for i in range(0,length - k):
kmer_str = str(rec.seq[i:i+k])
if kmer_str not in kmers:
kmers[kmer_str] = [hash]
else:
if hash not in kmers[kmer_str]:
kmers[kmer_str].append(hash)
parser = argparse.ArgumentParser(
description='Caclulates pairwise kmer identity between input sequences in multifasta, or list of fastas, lower triangle is template, upper is query id')
parser.add_argument(
'-m',
dest="mfsa",
default=None,
help='Input multifasta')
parser.add_argument(
'-l',
dest="fsa_list",
default=None,
help='Input list of fastas')
parser.add_argument(
'-o',
dest="odir",
default=None,
help='Output directory')
parser.add_argument(
'-k',
dest="kmer",
default=16,
type=int,
help='Kmer size')
parser.add_argument(
'-t',
dest="thr",
default=1.00,
type=float,
help='Homology reduction threshold, discard above')
parser.add_argument(
'-s',
dest="split",
default=None,
help='Split entry id on this character')
args = parser.parse_args()
del_ids = set()
fasta_ids = {}
kmers = {}
if args.mfsa is not None and os.path.exists(args.mfsa):
seq_iter = SeqIO.parse(args.mfsa, "fasta")
for rec in seq_iter:
hashid = hash_seqid(rec.id)
split_to_kmers(args.kmer, rec, hashid)
elif args.fsa_list is not None and os.path.exists(args.fsa_list):
with open(args.fsa_list, "r") as fp:
for line in fp:
parser = SeqIO.parse(line.strip(), "fasta")
for rec in parser:
hashid = hash_seqid(rec.id)
split_to_kmers(args.kmer, rec, hashid)
#print stats
print(len(kmers))
# list of hashes
seqlist = fasta_ids.keys()
# template
for i_seq in seqlist:
if i_seq not in del_ids:
for j_seq in seqlist:
if i_seq != j_seq:
i_mers = 0.0
common = 0
for val in kmers.values():
if i_seq in val:
i_mers += 1
if j_seq in val:
common += 1
simil = common / i_mers
if simil > args.thr:
print(fasta_ids[i_seq], fasta_ids[j_seq], i_mers, common, simil)
del_ids.add(j_seq)
# print as separate entries
splitchar = ""
if args.split is not None:
splitchar = args.split
# get the ids to keep
output_ids = set(seqlist) - del_ids
if args.mfsa is not None and os.path.exists(args.mfsa):
seq_iter = SeqIO.parse(args.mfsa, "fasta")
for rec in seq_iter:
if hash_seqid(rec.id) in output_ids:
tmp = rec.id.split(splitchar)
rec.id = tmp[0]
if len(tmp) > 1:
rec.description = splitchar.join(tmp[1:])
ofn = os.path.join(args.odir, "{0}.fa".format(rec.id))
with open(ofn, "w") as of:
SeqIO.write(rec, of, "fasta")
elif args.fsa_list is not None and os.path.exists(args.fsa_list):
with open(args.fsa_list, "r") as fp:
for line in fp:
parser = SeqIO.parse(line.strip(), "fasta")
for rec in parser:
if hash_seqid(rec.id) in output_ids:
tmp = rec.id.split(splitchar)
rec.id = tmp[0]
if len(tmp) > 1:
rec.description = splitchar.join(tmp[1:])
ofn = os.path.join(args.odir, "{0}.fa".format(rec.id))
with open(ofn, "w") as of:
SeqIO.write(rec, of, "fasta")