-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_conversions.py
60 lines (46 loc) · 2.05 KB
/
xml_conversions.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
import xml.etree.ElementTree as ET
import numpy as np
def read_in_xml(xml_file, sample, with_stdev = False):
tree = ET.parse(xml_file)
root = tree.getroot()
transcript_id = root[0].attrib["id"]
length = root[0].attrib["length"]
sequence = root[0][0].text.replace("\t", "").replace("\n", "")
reactivity = np.array(root[0][1].text.replace("\t", "").replace("\n", "").split(",")).astype(float)
if with_stdev:
stdev = np.array(root[0][2].text.replace("\t", "").replace("\n", "").split(",")).astype(float)
return {"sample" : sample,
"transcript_id" : transcript_id,
"length" : length,
"sequence" : sequence,
"reactivity" : reactivity,
"stdev": stdev
}
else:
return {"sample" : sample,
"transcript_id" : transcript_id,
"length" : length,
"sequence" : sequence,
"reactivity" : reactivity
}
def convert_xml_to_bpseq(xml_file,outfile):
tmp_data = read_in_xml(xml_file, "")
reactivities = tmp_data["reactivity"]
sequence = list(tmp_data["sequence"].replace("T", "U"))
reactivities = np.nan_to_num(reactivities, nan=-1.0)
with open(outfile, "w+") as out:
for i in np.arange(1,1+reactivities.shape[0]):
position = int(i)
line = f"{position} {sequence[position-1]} e1 {reactivities[position-1]}\n"
out.write(line)
#function to help predicting only part of an RNA isoform
def convert_xml_to_bpseq_trimmed(xml_file,outfile, length):
tmp_data = read_in_xml(xml_file, "")
reactivities = tmp_data["reactivity"][:length]
sequence = list(tmp_data["sequence"].replace("T", "U"))[:length]
reactivities = np.nan_to_num(reactivities, nan=-1.0)
with open(outfile, "w+") as out:
for i in np.arange(1,1+reactivities.shape[0]):
position = int(i)
line = f"{position} {sequence[position-1]} e1 {reactivities[position-1]}\n"
out.write(line)