-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrev_comp.py
57 lines (45 loc) · 1015 Bytes
/
rev_comp.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
'''
Reverse Compliment Genome Generator
By Ronald Randolph
September 2019
This program generates and returns the reverse compliment
of a given genome to an output file "p3_output.fasta"
'''
import sys
#argument check
if(len(sys.argv) != 2):
print 'Usage: rev_comp.py [filename]'
sys.exit(1)
#open file and split lines into a list
file = sys.argv[1]
with open(file) as f:
data = f.read().splitlines()
#1. update/store header of data
h = data[0].split()
h[0] = ">reversed"
header = ' '.join(h)
#2. remove old header and reverse data
data.pop(0)
data.reverse()
data.pop(0)
with open("p3_output.fasta","w+") as f:
f.write(header)
f.write("\n")
#3. reverse and compliment each line
for line in data:
tmp = list(line)
tmp.reverse()
index = 0
for char in tmp:
if (char == 'A'):
tmp[index] = 'T'
elif (char == 'T'):
tmp[index] = 'A'
elif (char == 'C'):
tmp[index] = 'G'
else:
tmp[index] = 'C'
index += 1
rev = ''.join(tmp)
f.write(rev)
f.write("\n")