-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
51 lines (40 loc) · 1.33 KB
/
utilities.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
def colored(seq):
bcolors = {
'A': '\033[92m',
'C': '\033[94m',
'G': '\033[93m',
'T': '\033[91m',
'U': '\033[91m',
'reset': '\033[0;0m'
}
tmpStr = ""
for nuc in seq:
if nuc in bcolors:
tmpStr += bcolors[nuc] + nuc
else:
tmpStr += bcolors['reset'] + nuc
return tmpStr + '\033[0;0m'
def read_file(file_path):
'''Reading a file and returning a list of word line'''
with open(file_path, 'r') as f:
return [line.strip('\n') for line in f.readlines()]
def write_file(file_path, seq, mode='w'):
'''Writing sequence in a file'''
with open(file_path, mode) as f:
f.write(seq + '\n')
def read_fasta(file_path):
'''Reading fasta formatted file and returning into line'''
with open(file_path, 'r') as f:
# for storing the contents of the file into a list
fasta_file = [line.strip('\n') for line in f.readlines()]
# for storing labels and data
fasta_dict = {}
fasta_label = ''
# for preparing data and taking into dictionary
for line in fasta_file:
if '>' in line:
fasta_label = line
fasta_dict[fasta_label] = ''
else:
fasta_dict[fasta_label] += line
return fasta_dict