-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta_compressor.py
executable file
·38 lines (34 loc) · 1017 Bytes
/
fasta_compressor.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
# coding=utf-8
import sys
# Compression "ratio" setting
cmp = 1
if(len(sys.argv) > 2):
cmp = int(sys.argv[2])
# Keeping track of how many
# nucleotides have been removed
printed_nuc = 0
deleted_nuc = 0
with open(sys.argv[1]) as f:
for line in f:
line = line.rstrip("\n")
if(line[0] == ">"):
print(line)
else:
prev = ""
count = 0
for nc in line:
if(nc == prev):
count += 1
else:
prev = nc
count = 1
if(count <= cmp):
printed_nuc += 1
print(nc, end="")
else:
deleted_nuc += 1
print("")
cmp_ratio = round(float(printed_nuc) / (printed_nuc + deleted_nuc), 2)
print("Deleted nucleotides: ", deleted_nuc, file=sys.stderr)
print("Printed nucleotides: ", printed_nuc, file=sys.stderr)
print("Compression ratio : ", cmp_ratio , file=sys.stderr)