-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseComplement.py
84 lines (68 loc) · 2.58 KB
/
ReverseComplement.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
#!/usr/bin/env python
import os
import argparse
#This script is created be yuxikai
# If anything happen yuxi.kai@163.com for u 2 contact with me
def Instructions():
Instruction = argparse.ArgumentParser()
Instruction.add_argument('-f', '--fasta', help='Your sequence file', required='True')
Instruction.add_argument('-s', '--start', help='The startinng point of your region', required='True')
Instruction.add_argument('-e', '--end', help='The ending point of your region', required='True')
return Instruction.parse_args()
db = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
def block_cacher_v2(lines: list, separator_first: str):
file_blocks = []
block = []
for line in lines:
if len(line) == 0:
continue
if line.startswith(separator_first):
if len(block) > 0:
file_blocks.append(block)
block = [line]
continue
block.append(line)
if len(block) > 0:
file_blocks.append(block)
return file_blocks
def openfile(path: str):
lines = []
with open(path, 'r', encoding='utf-8') as f_open:
file_lines = f_open.read().split('\n')
for line in file_lines:
if len(line) == 0 or '#' == line[0]:
continue
lines.append(line)
return lines
def regularFa(in_fa: str):
new_f = []
for block in block_cacher_v2(openfile(in_fa), '>'):
new_f.append(block[0])
new_f.append(''.join(block[1:]))
return new_f
def reverse(in_fasta: str, start: str, end: str):
new_block = []
blocks = regularFa(in_fasta)
tmp_l = []
tmp_ll = []
new_block.append(blocks[0])
tmp_ll.append(blocks[1][:int(start)-1])
for i in blocks[1][int(start)-1:int(end)]:
tmp_l.append(db[i])
tmp_ll.append(''.join(tmp_l[::-1]))
tmp_ll.append(blocks[1][int(end):])
new_block.append(''.join(tmp_ll))
return new_block
def writeFa(in_f: str, s: str, e: str):
with open('.'.join(in_f.split('.')[:-1]) + '.' + s + '_' + e + '.Reversed.fasta', 'w')as f:
f.write('\n'.join(reverse(in_fasta=in_f, start=s, end=e))+'\n')
if __name__ == '__main__':
print('The scripts only designed for \
reverse complement of only one sequence at a time, \
please make sure your input region is single! :b\n\
Usage: python ' + os.getcwd() + '/ReverseComplement.py -f [dir/str] -s [startPoint/int] -e [endPoint/int]')
ins = Instructions()
in_f = ins.fasta
s = ins.start
e = ins.end
writeFa(in_f=in_f, s=s, e=e)