-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_adapters.py
executable file
·71 lines (58 loc) · 1.84 KB
/
count_adapters.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
# coding = utf-8
# count adapters from an adapter stability benchmark file
import sys
import re
from collections import defaultdict as dd
DNA_rule = re.compile(r'^[ATCG]+$')
adapters = dd(lambda: dd(lambda: dd(int)))
def store_adapter(adapter, method, data):
try:
assert(len(data) == 2)
except AssertionError:
print("Error on data length")
print(data)
start = data[0]
end = data[1]
if(start):
adapters[method]["start"][start] += 1
if(end):
adapters[method]["end"][end] += 1
with open(sys.argv[1]) as f:
method = ""
data = []
for line in f:
line = line.rstrip("\n")
dna_found = DNA_rule.match(line)
# ####################################
# # DEBUG
# print("###########################")
# print("Line:", line)
# print("DNA found:", dna_found)
# ####################################
# if method
if(line != "" and not dna_found):
if(method):
store_adapter(adapters, method, data)
method = line
data = []
# if data:
elif(len(data) < 2):
data.append(line if line else "None")
# else, separator
else:
store_adapter(adapters, method, data)
method = ""
data = []
if(method and len(data) == 2):
store_adapter(adapters, method, data)
for method in sorted(adapters.keys(), reverse=True):
print(method)
for which_end in ["start", "end"]:
print(which_end)
for key in sorted(adapters[method][which_end].keys(),
key=lambda x: adapters[method][which_end][x],
reverse=True):
val = adapters[method][which_end][key]
print("\t".join(str(el) for el in [val, key]))
print("")
print("")