-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_fasta.py
82 lines (66 loc) · 1.8 KB
/
process_fasta.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
'''
clean and format fasta files
default output: <input_file_path>.clean
usage: python pocess_fasta.py <fasta_file> [output_file]
'''
import sys
COLUMNS = ['gene_stable_id', 'transcript_stable_id', 'seq']
def usage_str():
'''
usage string
'''
print("usage: python pocess_fasta.py <fasta_file> [output_file]")
def decorator(n):
'''
'''
print("*"*n)
def header_line():
'''
returns the header line
'''
return ",".join(COLUMNS)
def format_data_line(line):
'''
formats each line into comma separated values
'''
data_line = line[:15] + "," + line[15:30] + "," + line[30:]
return data_line
def fasta_format(path, output_file):
'''
cleans and formats file and writes to file.clean
removes Sequence unavailable lines too
'''
print("formatting file: "+path)
# create a file handler
input_file = open(path, 'r')
# read, replace new line, split at >
lines = input_file.read()\
.replace("\n", "")\
.replace("|", "")\
.split(">")[1:]
# close file
input_file.close()
# write to output file
print("writing to: "+output_file)
output_file = open(output_file, "w")
# write header line
output_file.write(header_line()+"\n")
# write data lines
for line in lines:
if(not "Sequence unavailable" in line):
output_file.write(format_data_line(line) + "\n")
# flush and close file.clean
output_file.flush()
output_file.close()
# **** Starts here ****
decorator(25)
# read file from args
if(len(sys.argv) < 2):
usage_str()
else:
try:
fasta_format(path=sys.argv[1], output_file=sys.argv[2])
except:
fasta_format(path=sys.argv[1], output_file=sys.argv[1]+".clean")
print("DONE")
decorator(25)