-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_readers.py
49 lines (45 loc) · 1.7 KB
/
file_readers.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
# file_readers.py
import numpy as np
from bed_entries import BedGraphEntry, BedEntry
def readBedGraphFile(filename):
entries = []
with open(filename, 'r') as f:
for line in f:
fields = line.strip().split('\t')
chromosome = fields[0]
start = int(fields[1])
end = int(fields[2])
score = fields[3]
if isinstance(score, str):
score = score.strip().split('_')[0]
entries.append(BedGraphEntry(chromosome, start, end, score))
return entries
def readBedFile(filename):
entries = []
with open(filename, 'r') as f:
for line in f:
fields = line.strip().split('\t')
chromosome = fields[0]
start = int(fields[1])
end = int(fields[2])
name = fields[3] if len(fields) >= 4 else ""
score = int(fields[4]) if len(fields) >= 6 else 0
strand = fields[5] if len(fields) >= 6 else ""
entries.append(BedEntry(chromosome, start, end, name, score, strand))
return entries
def read_hmm_matrices(filename):
transition_matrix = []
emission_matrix = []
with open(filename, "r") as f:
line = f.readline().strip()
if line == "$TRANS":
line = f.readline().strip()
while line != "$EM":
transition_matrix.append([float(x) for x in line.split()])
line = f.readline().strip()
if line == "$EM":
line = f.readline().strip()
while line:
emission_matrix.append([float(x) for x in line.split()])
line = f.readline().strip()
return np.array(transition_matrix), np.array(emission_matrix)