-
Notifications
You must be signed in to change notification settings - Fork 0
/
biopepa_model_parser.py
54 lines (44 loc) · 1.73 KB
/
biopepa_model_parser.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
from location import Location
from locationtree import LocationTree
import re
class Biopepa_Model_Parser():
def __init__(self):
self.loc_results = {}
self.loc_tree = {}
self.tree = None
def parse(self, path):
data = self.open_model(path)
locations = self.get_locations(data)
self.parse_location(locations)
def open_model(self, model):
with open(model, 'r') as f:
data = f.read()
return data
def get_locations(self, data):
locations = []
location_lines = re.findall("location (.*?)\n", data)
for location in location_lines:
locations.append("location " + location)
return locations
def parse_location(self, locations):
for location in locations:
try:
loc_type = re.findall("type = (.*?);", location)[0].strip()
loc_size = "Remove this"
loc_name = re.findall("location (.*?) ", location)[0].strip()
loc_parent = re.findall("in (.*?):", location)[0].strip() if len(re.findall("in (.*?):", location)) > 0 else "root"
if loc_name != "mechanisms":
self.loc_results[loc_name] = Location(loc_name, loc_size,
loc_parent, loc_type)
except:
print "malformed location line: " + location
def __str__(self):
output = ""
for location in self.loc_results:
output += self.loc_results[location].__str__()
return output
def build_graph(self):
if self.loc_results != {}:
self.tree = LocationTree(self.loc_results)
return self.tree.build_tree()
return {}