-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGC_content.py
More file actions
67 lines (42 loc) · 1.42 KB
/
GC_content.py
File metadata and controls
67 lines (42 loc) · 1.42 KB
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
##############################################
##############################################
# Input should be in FASTA format.
##############################################
##############################################
import os
try:
os.remove('out.tsv')
except OSError:
pass
import decimal
import linecache
import sys
names = ''
GC_CONTENT = ''
with open(sys.argv[1]) as file:
dat = file.read().strip('\n').split()
for line in dat:
if line.startswith('>'):
names += line.replace('>','') + '\n'
with open(sys.argv[1]) as file:
seqs = file.read().replace('\n','').replace('>','\n').replace('Rosalind_','').strip()
for line in seqs.split():
AT = line.count('A') + line.count('T')
GC = line.count('G') + line.count('C')
AT = decimal.Decimal(AT)
GC = decimal.Decimal(GC)
GC_content = 100 * GC/(AT + GC)
GC_CONTENT += str(round(GC_content, 7)) + '\n'
with open('out_names.tsv', 'a') as out:
out.write(names)
with open('out.tsv', 'a') as out:
out.write(GC_CONTENT)
line_number = 1
with open('out.tsv') as file, open('out_names.tsv') as file2, open('output.tsv', 'w') as file3:
for line in file:
file3.write (linecache.getline('out_names.tsv', line_number).strip() + '\t' + linecache.getline('out.tsv', line_number).strip() + '\n')
line_number = line_number + 1
bashCommand = 'sort -rnk 2 output.tsv'
os.system(bashCommand)
bashCommand = 'rm -f out.tsv && rm -f out_names.tsv'
os.system(bashCommand)