aboutsummaryrefslogtreecommitdiff
path: root/src/filter_max_consecutive_binding.py
blob: 9054dac1f530e311fc3fc8b3b61707e81bd603f5 (plain)
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
#!/usr/bin/env python
import sys, os

binding = { 'A': 'T', 'T': 'A',	'C': 'G', 'G': 'C',	'_': False }


def max_consecutive_binding(mer1, mer2):
	if len(mer2) > len(mer1):
		mer1, mer2 = mer2, mer1
	
	# save the len because it'll change when we do a ljust
	mer1_len = len(mer1)
	# reverse mer2,
	mer2 = mer2[::-1]
	# pad mer one to avoid errors
	mer1 = mer1.ljust(mer1_len + len(mer2), "_")

	max_bind = 0;
	for offset in range(mer1_len):
		consecutive = 0
		for x in range(len(mer2)):
			if binding[mer1[offset+x]] == mer2[x]:
				consecutive = consecutive + 1
			else:
				consecutive = 0

			max_bind = max(consecutive,max_bind)

	return max_bind

def test():
	# mer 1  				 mer 2					# correct ans
	arr = [
	("ATATAT",			 "TATATA", 					5),
	("ACAGGGAT",		 "ATATATAT", 				2),
	("CATATATAT", 	 "ATATATATATAT",	  8),
	("ATATATATATAT", "ATATATATAT", 		 10),
	("ATATAT",			 "TATAT",					  5),
	("AACGATACCATG", "GGATCATACGTA", 		3),
	("CGT",          "ACG",			 				3),
	("ACG",          "CGT",			 				3),
	("CACC", 				 "GGTGT", 					4),
	("GGTGT", 			 "CACC", 					4),
	("CCCCCCCCATATAT", "TATA", 4),
	]
	
	print 'pass\tmer1\tmer2\tres\tcorr'
	for mer_combination in arr:
		response = []
		ans = max_consecutive_binding(mer_combination[0], mer_combination[1])
	
		response.append(str(ans == mer_combination[2]))
		response.append(mer_combination[0])
		response.append(mer_combination[1])
		response.append(str(ans))
		response.append(str(mer_combination[2]))
	
		print '\t'.join(response)
	
def main():

	if(len(sys.argv) < 2):
		print "cutoff is expected as an argument"
		exit()
	else:
		cutoff = int(sys.argv[1])
	
	for line in sys.stdin:
		mer = line.split()[0]
		if max_consecutive_binding(mer, mer) < cutoff:
			sys.stdout.write(line)


if __name__ == "__main__":
	sys.exit(main())