-
Notifications
You must be signed in to change notification settings - Fork 4
/
species.js
170 lines (153 loc) · 4.89 KB
/
species.js
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
const { Record } = require('./record');
const { UnitTerm } = require('./unit-term');
const { Unit } = require('./unit');
/*
Species class
species1 @Species {
compartment: comp1,
isAmount: true
};
*/
class Species extends Record {
merge(q = {}){
super.merge(q);
let logger = this.namespace?.container?.logger;
let valid = Species.isValid(q, logger);
if (valid) {
if (q.compartment !== undefined) this.compartment = q.compartment;
if (q.isAmount !== undefined) this.isAmount = !!q.isAmount;
}
return this;
}
get className() {
return 'Species';
}
clone(){
let clonedComponent = super.clone();
if (typeof this.compartment !== 'undefined')
clonedComponent.compartment = this.compartment;
if (typeof this.isAmount !== 'undefined')
clonedComponent.isAmount = this.isAmount;
return clonedComponent;
}
toQ(options = {}){
let res = super.toQ(options);
if (this.compartment) res.compartment = this.compartment;
if (this.isAmount) res.isAmount = this.isAmount;
return res;
}
unitsSBML(){
let compartmentUnits = this.compartmentObj?.unitsParsed;
if (!this.isAmount && compartmentUnits!==undefined && this.unitsParsed!==undefined) {
return this.unitsParsed
.multiply(compartmentUnits)
.simplify();
} else if (this.isAmount && this.unitsParsed!==undefined) {
return this.unitsParsed;
} else {
return undefined;
}
}
unitsHash(useSBMLUnits){ // get normal or substance units
if(!useSBMLUnits && this.unitsParsed!==undefined){
return this.unitsParsed.toHash();
}else if(useSBMLUnits && this.unitsSBML()){
return this.unitsSBML().toHash();
}
}
dependOn(context, includeCompartment = false){
let deps = super.dependOn(context);
let useCompartment = includeCompartment
&& this.compartment !== undefined
&& !this.isAmount
&& !this.isRule;
if (useCompartment) deps.push(this.compartment);
return deps;
}
get isExtendedRule(){
return this.assignments.ode_ !== undefined
|| !this.isAmount;
}
_references(){
let classSpecificRefs = [this.compartment];
return super._references()
.concat(classSpecificRefs);
}
/*
Check units for left and right side of ODE
Works only for bound records
*/
checkUnits() {
super.checkUnits();
let logger = this.namespace?.container?.logger;
// if not in processes do not check
let processes = this.backReferences.map(x => x._process_);
if (!processes.length) {
return; // BRAKE
}
// if no units for t skip check, message in Container.checkUnits()
let timeUnits = this.namespace.get('t').unitsParsed;
if (!timeUnits) {
return; // BRAKE
}
let compartmentUnits = this.compartmentObj?.unitsParsed;
if (!compartmentUnits && !this.isAmount) {
logger.warn(`No units set for compartment "${this.compartment}", cannot check units for "${this.index}" associated ODE.`);
return; // BRAKE
}
let speciesUnits = this.unitsParsed;
if (!speciesUnits) {
logger.warn(`No units set for "${this.index}", cannot check units for associated ODE.`);
return; // BRAKE
}
// d(s*c)/dt
let leftSideUnits = speciesUnits
.multiply(this.isAmount ? new Unit() : compartmentUnits) // multiply only for concentrations
.divide(timeUnits)
.simplify();
// r1 + r2 + r3
processes.forEach((proc) => {
let processUnits = proc.unitsParsed;
if (!processUnits) {
// message was in Process.checkUnits
return; // BRAKE
}
if (!leftSideUnits.equal(processUnits, true)) {
logger.warn(`Unit inconsistency for ${this.id} associated ODE. Left: ${leftSideUnits.toString()}. Right: ${processUnits.toString()} (${proc.id})`);
}
});
}
get legalTerms() {
let actualCompartmentTerm = this.compartmentObj?.unitsParsed?.toTerm();
if (this.isAmount) {
return [
new UnitTerm([{kind: 'amount'}]),
new UnitTerm([{kind: 'mass'}]),
];
} else if (actualCompartmentTerm !== undefined) {
return [
new UnitTerm([{kind: 'amount'}]).divide(actualCompartmentTerm),
new UnitTerm([{kind: 'mass'}]).divide(actualCompartmentTerm)
];
} else {
return [
new UnitTerm([{kind: 'amount'}, {kind: 'length', exponent: -1}]),
new UnitTerm([{kind: 'amount'}, {kind: 'length', exponent: -2}]),
new UnitTerm([{kind: 'amount'}, {kind: 'length', exponent: -3}]),
new UnitTerm([{kind: 'mass'}, {kind: 'length', exponent: -1}]),
new UnitTerm([{kind: 'mass'}, {kind: 'length', exponent: -2}]),
new UnitTerm([{kind: 'mass'}, {kind: 'length', exponent: -3}])
];
}
}
}
Species._requirements = {
compartment: {
required: true,
isArray: false,
isReference: true, targetClass: 'Compartment', setTarget: true
}
};
module.exports = {
Species
};