-
Notifications
You must be signed in to change notification settings - Fork 0
/
customParser.py
170 lines (152 loc) · 7.63 KB
/
customParser.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#Gillespie's Direct Stochastic Simulation Algorithm Program
#Parser to convert .PSC files to arrays/matrices (speciesArray, parameterArray, and reactionMatrix) suitable for the main simulation code
#Note: the example .psc file was generated from a SBML .xml file using StochPy
#Final Project for BIOEN 6760, Modeling and Analysis of Biological Networks
#Trevor James Tanner
#Copyright 2013-2015
import re
import numpy as np
import itertools
import pandas as pd
filer = "BIOMD0000000504.xml.psc"
data = open(filer)
def convertReactants(reactants):
newReactantList = list()
for reactant in reactants:
if '>' not in reactant and ('+' not in reactant and '-' not in reactant):
if '{' in reactant:
m=re.match("{[0-9]*.[0-9]*}",reactant)
num = float(m.group()[1:-1])
reactant = reactant[m.span()[1]:]
newReactantList.append((num,reactant))
else:
newReactantList.append((1,reactant))
else:
newReactantList.append(reactant)
return newReactantList
def convertMe(line, reactants):
reactants = convertReactants(reactants)
flatReactants = list(itertools.chain(*reactants))
line = line.strip()
numMultiply = len(re.findall("\*",line))
equalsLocation = 0
multiplyIter = re.finditer("\*",line)
if(numMultiply==1):
for a in multiplyIter:
multiplyLocation = a.start()
cVal = line[equalsLocation:multiplyLocation]
specieName = line[multiplyLocation+1:len(line)]
if 'Sink' in flatReactants:
return (1,cVal,-reactants[0][0],reactants[0][1],0,0,0,reactants[2][1],0,0)
elif 'Source' in flatReactants:
return (1,cVal,-0,reactants[0][1], 0,0,reactants[2][0],reactants[2][1],0,0)
elif '$pool' in flatReactants:
return (1,cVal,-0,reactants[0][1], 0,0,reactants[2][0],reactants[2][1],0,0)
elif len(reactants)==5:
return (1,cVal,-reactants[0][0],reactants[0][1],0,0, reactants[2][0],reactants[2][1],reactants[4][0],reactants[4][1])
else:
return (1,cVal,-reactants[0][0],reactants[0][1],0,0, reactants[2][0],reactants[2][1],0,0)
elif(numMultiply==2):
for i,a in enumerate(multiplyIter):
if i==0:
multiplyLocation1 = a.start()
cVal = line[equalsLocation:multiplyLocation1]
elif i==1:
multiplyLocation2 = a.start()
specie1 = line[multiplyLocation1:multiplyLocation2][1:]
specie2 = line[multiplyLocation2:len(line)][1:]
if len(reactants)==3:
if specie1 not in flatReactants:
return (2,cVal,-reactants[0][0],reactants[0][1],0,specie1,reactants[2][0],reactants[2][1],0,0)
elif specie2 not in flatReactants:
return (2,cVal,-reactants[0][0],reactants[0][1],0,specie2,reactants[2][0],reactants[2][1],0,0)
reactionArrowIndex = reactants.index('>')
if reactionArrowIndex==3:
return (2,cVal,-reactants[0][0],reactants[0][1],-reactants[2][0],reactants[2][1],reactants[4][0],reactants[4][1],0,0)
elif reactionArrowIndex==1:
if specie1 not in flatReactants:
return (2,cVal,-reactants[0][0],reactants[0][1],0,specie1,reactants[2][0],reactants[2][1],reactants[4][0],reactants[4][1])
elif specie2 not in flatReactants:
return (2,cVal,-reactants[0][0],reactants[0][1],0,specie2,reactants[2][0],reactants[2][1],reactants[4][0],reactants[4][1])
elif(numMultiply==3):
for i,a in enumerate(multiplyIter):
if i==0:
multiplyLocation1 = a.start()
cVal = line[equalsLocation:multiplyLocation1]
elif i==1:
multiplyLocation2 = a.start()
minusOneLocation = line.find("-1.0")
specie1 = line[multiplyLocation1:multiplyLocation2][1:]
convertReactants(reactants)
return (3,cVal,-reactants[0][0],reactants[0][1],0,0,reactants[2][0],reactants[2][1],0,0)
reactionList = list()
specieDict = dict()
parameterDict = dict()
reactionFlag = bool()
fixedSpeciesFlag = bool()
variableSpeciesFlag = bool()
parameterFlag = bool()
targetLine1 = int()
targetLine2 = int()
subLine = str()
for i,line in enumerate(data):
if "# Reactions" in line:
reactionFlag = True
if "# Fixed species" in line:
fixedSpeciesFlag = True
if "# Variable species" in line:
variableSpeciesFlag = True
if "# Parameters" in line:
parameterFlag = True
if reactionFlag==True and fixedSpeciesFlag==False:
if ":" in line and "#" not in line:
targetLine1 = i+1
targetLine2 = i+2
elif i==targetLine1:
subLine = line.strip()
elif i==targetLine2:
reactionList.append(convertMe(line,subLine.split(' ')))
elif variableSpeciesFlag==True and parameterFlag==False:
if "=" in line:
subLine = line.strip()
equalsLocation = subLine.find("=")
specie = subLine[0:equalsLocation-1].split('@')[0]
specieQuantity = subLine[equalsLocation+2:len(subLine)]
specieDict[specie] = float(specieQuantity)
elif parameterFlag==True:
if "=" in line:
subLine = line.strip()
equalsLocation = subLine.find("=")
parameter = subLine[0:equalsLocation-1]
parameterValue = subLine[equalsLocation+2:len(subLine)]
parameterDict[parameter] = float(parameterValue)
specieDict['Source'] = 1;
specieDict['Sink'] = 1;
specieDict['$pool'] = 1;
def findIndex(inputReactant,inputReactantDict):
if(inputReactant == 0):
return 0
else:
return inputReactantDict.keys().index(inputReactant)
reactionMatrixList = list()
for reaction in reactionList:
reactionMatrixList.append((reaction[0],findIndex(reaction[1],parameterDict),reaction[2],findIndex(reaction[3],specieDict),reaction[4],findIndex(reaction[5],specieDict),reaction[6],findIndex(reaction[7],specieDict),reaction[8],findIndex(reaction[9],specieDict)))
reactionMatrix = np.array(reactionMatrixList,dtype='int32')
reactionMatrix = reactionMatrix[reactionMatrix[:,0].argsort()] #Sort the arrays by reaction type to minimize branch divergence / warp divergence when calculating propensities
#Make Parameter Array
parameterList = list()
parameterIndices = reactionMatrix[:,1]
for subParameterIndex in parameterIndices:
parameterList.append(parameterDict.values()[int(subParameterIndex)])
parameterArray = np.array(parameterList,dtype='float32')
#Make Specices Array
speciesArray = np.array(specieDict.values(),dtype='int32')
#Make Reaction Matrix
reactionMatrix = np.delete(reactionMatrix,1,axis=1)
reactionDF = pd.DataFrame(reactionMatrix)
reactionDF = reactionDF[[0,2,4,6,8,1,3,5,7]] #Reshuffle the columns so reactant/product indices first, followed by their respective reaction deltas
reactionDF = reactionDF.sort(columns=[0,2,6,4,8]) #Simple sorting to maximize coalesced reads
reactionMatrix = reactionDF.values
np.savetxt('reactionMatrix.txt',reactionMatrix,fmt='%i',header="%i rows" % reactionMatrix.shape[0])
np.savetxt('speciesArray.txt',speciesArray,fmt='%i',header="%i rows" % speciesArray.shape[0])
np.savetxt('parameterArray.txt',parameterArray,header="%i rows" % parameterArray.shape[0])