0001 function [ result ] = instance2SBML( CSGdata, Meshdata, savepath, SBMLfile )
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 
0019 
0020 
0021 
0022 
0023 
0024 
0025 
0026 
0027 
0028 
0029 
0030 
0031 
0032 
0033 
0034 
0035 
0036 
0037 
0038 
0039 
0040 
0041 if nargin==0
0042     warning('No input arguments given to instance2SBML. Nothing to do.');
0043     return
0044 elseif nargin==1
0045     warning('No savepath given to instance2SBML. Defaulting to "./model.xml"');
0046     savepath = './model.xml';
0047 end
0048 
0049 
0050 
0051 s = 'spatial:';
0052 
0053 
0054 docNode = com.mathworks.xml.XMLUtils.createDocument('sbml');
0055 
0056 docRootNode = docNode.getDocumentElement;
0057 docRootNode.setAttribute('xmlns','http://www.sbml.org/sbml/level3/version1/core');
0058 docRootNode.setAttribute('level', '3');
0059 docRootNode.setAttribute('version', '1');
0060 
0061 
0062 docRootNode.setAttribute('xmlns:req','http://www.sbml.org/sbml/level3/version1/requiredElements/version1');
0063 docRootNode.setAttribute(['req:','required'],'true');
0064 
0065 
0066 
0067 
0068 docRootNode.setAttribute('xmlns:spatial','http://www.sbml.org/sbml/level3/version1/spatial/version1');
0069 docRootNode.setAttribute([s,'required'],'true');
0070 
0071 
0072 
0073 
0074 wrapperNode = docNode.createElement('model');
0075 wrapperNode.setAttribute('id','CellOrganizer2_0');
0076 wrapperNode.setAttribute('name',['CellOrganizer2_0',CSGdata.name]);
0077 
0078 
0079 
0080 ListOfUnitDefinitions = docNode.createElement('listOfUnitDefinitions');
0081 
0082 UnitDefinition = docNode.createElement('unitDefinition');
0083 UnitDefinition.setAttribute('id','substance');
0084 ListOfUnits = docNode.createElement('listOfUnits');
0085 unit = docNode.createElement('unit');
0086 unit.setAttribute('kind','mole');
0087 unit.setAttribute('exponent','1');
0088 unit.setAttribute('scale','0');
0089 unit.setAttribute('multiplier','1e-09');
0090 ListOfUnits.appendChild(unit);
0091 UnitDefinition.appendChild(ListOfUnits);
0092 ListOfUnitDefinitions.appendChild(UnitDefinition);
0093 
0094 UnitDefinition = docNode.createElement('unitDefinition');
0095 UnitDefinition.setAttribute('id','volume');
0096 ListOfUnits = docNode.createElement('listOfUnits');
0097 unit = docNode.createElement('unit');
0098 unit.setAttribute('kind','metre');
0099 unit.setAttribute('exponent','3');
0100 unit.setAttribute('scale','0');
0101 unit.setAttribute('multiplier','0.1');
0102 ListOfUnits.appendChild(unit);
0103 UnitDefinition.appendChild(ListOfUnits);
0104 ListOfUnitDefinitions.appendChild(UnitDefinition);
0105 
0106 UnitDefinition = docNode.createElement('unitDefinition');
0107 UnitDefinition.setAttribute('id','area');
0108 ListOfUnits = docNode.createElement('listOfUnits');
0109 unit = docNode.createElement('unit');
0110 unit.setAttribute('kind','metre');
0111 unit.setAttribute('exponent','2');
0112 unit.setAttribute('scale','0');
0113 unit.setAttribute('multiplier','1');
0114 ListOfUnits.appendChild(unit);
0115 UnitDefinition.appendChild(ListOfUnits);
0116 ListOfUnitDefinitions.appendChild(UnitDefinition);
0117 
0118 UnitDefinition = docNode.createElement('unitDefinition');
0119 UnitDefinition.setAttribute('id','length');
0120 ListOfUnits = docNode.createElement('listOfUnits');
0121 unit = docNode.createElement('unit');
0122 unit.setAttribute('kind','metre');
0123 unit.setAttribute('exponent','1');
0124 unit.setAttribute('scale','0');
0125 unit.setAttribute('multiplier','1');
0126 ListOfUnits.appendChild(unit);
0127 UnitDefinition.appendChild(ListOfUnits);
0128 ListOfUnitDefinitions.appendChild(UnitDefinition);
0129 
0130 UnitDefinition = docNode.createElement('unitDefinition');
0131 UnitDefinition.setAttribute('id','time');
0132 ListOfUnits = docNode.createElement('listOfUnits');
0133 unit = docNode.createElement('unit');
0134 unit.setAttribute('kind','second');
0135 unit.setAttribute('exponent','1');
0136 unit.setAttribute('scale','0');
0137 unit.setAttribute('multiplier','60');
0138 ListOfUnits.appendChild(unit);
0139 UnitDefinition.appendChild(ListOfUnits);
0140 ListOfUnitDefinitions.appendChild(UnitDefinition);
0141 
0142 UnitDefinition = docNode.createElement('unitDefinition');
0143 UnitDefinition.setAttribute('id','nmol');
0144 ListOfUnits = docNode.createElement('listOfUnits');
0145 unit = docNode.createElement('unit');
0146 unit.setAttribute('kind','mole');
0147 unit.setAttribute('exponent','1');
0148 unit.setAttribute('scale','0');
0149 unit.setAttribute('multiplier','1e-09');
0150 ListOfUnits.appendChild(unit);
0151 UnitDefinition.appendChild(ListOfUnits);
0152 ListOfUnitDefinitions.appendChild(UnitDefinition);
0153 wrapperNode.appendChild(ListOfUnitDefinitions);
0154 
0155 
0156 
0157 
0158 ListOfCompartments = docNode.createElement('listOfCompartments');
0159 
0160 compartment = docNode.createElement('compartment');
0161 dicomWrapper = num2str(dicomuid());
0162 dicomWrapper = strrep(dicomWrapper, '.', '');
0163 compartment.setAttribute('metaid',dicomWrapper);
0164 compartment.setAttribute('id',CSGdata.class);
0165 compartment.setAttribute('name',CSGdata.class);
0166 compartment.setAttribute('spatialDimensions','3');
0167 compartment.setAttribute('size','50000');
0168 compartment.setAttribute('units','m');
0169 compartment.setAttribute('constant','true');
0170 
0171 mapping = docNode.createElement([s,'compartment']);
0172 DomainID = 'subdomain0';
0173 mapping.setAttribute([s,'spatialId'],[DomainID,CSGdata.class]);
0174 mapping.setAttribute([s,'compartment'],[CSGdata.class]);
0175 mapping.setAttribute([s,'domainType'],DomainID);
0176 mapping.setAttribute([s,'unitSize'],'1');
0177 
0178 compartment.appendChild(mapping);
0179 ListOfCompartments.appendChild(compartment);
0180 
0181 wrapperNode.appendChild(ListOfCompartments);
0182 
0183 
0184 ListOfParameters = docNode.createElement('listOfParameters');
0185 
0186 dimensionNames = ['x','y','z'];
0187 
0188 for i = 1:length(dimensionNames)
0189     parameter = docNode.createElement('parameter');
0190     parameter.setAttribute('id',dimensionNames(i));
0191     parameter.setAttribute('value','0');
0192     parameter.setAttribute('req:mathOverridden','spatial');
0193     parameter.setAttribute('req:coreHaseAlternateMath','false');
0194     
0195     symbolref = docNode.createElement([s,'spatialSymbolReference']);
0196     symbolref.setAttribute([s,'spatialId'],dimensionNames(i));
0197     symbolref.setAttribute([s,'type'],'coordinateComponent');
0198     
0199     parameter.appendChild(symbolref);
0200     
0201     ListOfParameters.appendChild(parameter);
0202 end
0203 wrapperNode.appendChild(ListOfParameters);
0204 
0205 
0206 GeowrapperNode = docNode.createElement('spatial:geometry');
0207 GeowrapperNode.setAttribute([s,'coordinateSystem'],'Cartesian');
0208 
0209 
0210 
0211 
0212 
0213 
0214 
0215 
0216 ListOfCoordCompNode = docNode.createElement([s,'listOfCoordinateComponents']);
0217 
0218 for i = 1:length(dimensionNames)
0219     CoordCompNode = docNode.createElement([s,'coordinateComponent']);
0220     CoordCompNode.setAttribute([s,'spatialId'],dimensionNames(i));
0221     CoordCompNode.setAttribute([s,'componentType'],['cartesian',upper(dimensionNames(i))]);
0222     CoordCompNode.setAttribute([s,'sbmlUnit'],'m');
0223     CoordCompNode.setAttribute([s,'index'],num2str(i-1));
0224     
0225     
0226     minNode = docNode.createElement([s,'boundaryMin']);
0227     minNode.setAttribute([s,'spatialId'],[upper(dimensionNames(i)),'min']);
0228     minNode.setAttribute([s,'value'],'-10');
0229     CoordCompNode.appendChild(minNode);
0230     maxNode = docNode.createElement([s,'boundaryMax']);
0231     maxNode.setAttribute([s,'spatialId'],[upper(dimensionNames(i)),'max']);
0232     maxNode.setAttribute([s,'value'],'10');
0233     CoordCompNode.appendChild(maxNode);
0234     
0235     
0236     ListOfCoordCompNode.appendChild(CoordCompNode);
0237 end
0238 GeowrapperNode.appendChild(ListOfCoordCompNode);
0239 
0240 
0241 
0242 ListOfDomainTypesNode = docNode.createElement([s,'listOfDomainTypes']);
0243 DomainTypeNode = docNode.createElement([s,'domainType']);
0244 DomainTypeNode.setAttribute([s,'spatialId'],DomainID);
0245 DomainTypeNode.setAttribute([s,'spatialDimensions'],'3');
0246 ListOfDomainTypesNode.appendChild(DomainTypeNode);
0247 GeowrapperNode.appendChild(ListOfDomainTypesNode);
0248 
0249 
0250 
0251 ListOfDomains = docNode.createElement([s,'listOfDomains']);
0252 DomainNode = docNode.createElement([s,'domain']);
0253 DomainNode.setAttribute([s,'spatialId'],[DomainID,'0']);
0254 DomainNode.setAttribute([s,'domainType'],DomainID);
0255 ListOfInteriorPoints = docNode.createElement([s,'listOfInteriorPoints']);
0256 InteriorPoint = docNode.createElement([s,'interiorPoint']);
0257 InteriorPoint.setAttribute([s,'coord1'],'0');
0258 InteriorPoint.setAttribute([s,'coord2'],'0');
0259 InteriorPoint.setAttribute([s,'coord3'],'0');
0260 ListOfInteriorPoints.appendChild(InteriorPoint);
0261 DomainNode.appendChild(ListOfInteriorPoints);
0262 ListOfDomains.appendChild(DomainNode);
0263 GeowrapperNode.appendChild(ListOfDomains);
0264 
0265 
0266 
0267 geometryDefNode = docNode.createElement([s,'listOfGeometryDefinitions']);
0268 
0269 
0270 
0271 
0272 
0273 
0274 
0275 
0276 CSGeometryNode = docNode.createElement([s,'csGeometry']);
0277 CSGeometryNode.setAttribute([s,'spatialId'], 'CSG_Geometry1');
0278 
0279 
0280 ListOfCSGObjectsNode = docNode.createElement([s,'listOfCSGObjects']);
0281 
0282 
0283 
0284 for j=1:1
0285     
0286     object = CSGdata.list(j);
0287     name = object.name;
0288     type = object.type;
0289     
0290     
0291     CSGObjectNode = docNode.createElement([s,'csgObject']);
0292     
0293     CSGObjectNode.setAttribute([s,'spatialId'],DomainID);
0294     CSGObjectNode.setAttribute([s,'domainType'],DomainID);
0295     
0296     if isfield(object, 'ordinal')
0297         ordinal = num2str(object.ordinal);
0298         CSGObjectNode.setAttribute([s,'ordinal'], ordinal);
0299         
0300     end
0301     
0302     
0303     
0304     if isfield(object, 'position')
0305         position = object.position;
0306         CSGTranslationNode = docNode.createElement([s,'csgTranslation']);
0307         CSGTranslationNode.setAttribute([s,'spatialId'],'translation');
0308         CSGTranslationNode.setAttribute([s,'translateX'], num2str(position(1)));
0309         CSGTranslationNode.setAttribute([s,'translateY'], num2str(position(2)));
0310         CSGTranslationNode.setAttribute([s,'translateZ'], num2str(position(3)));
0311     else
0312         error('No position specified. Unable to create SBML Spatial file.');
0313     end
0314     
0315     if isfield(object, 'rotation')
0316         rotation = object.rotation;
0317         CSGRotationNode = docNode.createElement([s,'csgRotation']);
0318         CSGRotationNode.setAttribute([s,'spatialId'], 'rotation');
0319         
0320         
0321         
0322         CSGRotationNode.setAttribute([s,'rotateAxisX'], num2str(deg2rad(rotation(1))));
0323         CSGRotationNode.setAttribute([s,'rotateAxisY'], num2str(deg2rad(rotation(2))));
0324         CSGRotationNode.setAttribute([s,'rotateAxisZ'], num2str(deg2rad(rotation(3))));
0325     else
0326         error('No rotation specified. Unable to create SBML Spatial file.');
0327     end
0328     
0329     if isfield(object, 'scale')
0330         size = object.scale;
0331         CSGScaleNode = docNode.createElement([s,'csgScale']);
0332         CSGScaleNode.setAttribute([s,'spatialId'], 'scale');
0333         CSGScaleNode.setAttribute([s,'scaleX'], num2str(size(1)));
0334         CSGScaleNode.setAttribute([s,'scaleY'], num2str(size(2)));
0335         CSGScaleNode.setAttribute([s,'scaleZ'], num2str(size(3)));
0336     else
0337         error('No scale specified. Unable to create SBML Spatial file.');
0338     end
0339     
0340     CSGPrimitiveNode = docNode.createElement([s,'csgPrimitive']);
0341     CSGPrimitiveNode.setAttribute([s,'spatialId'],type);
0342     
0343     if strcmpi('sphere',type)
0344         CSGPrimitiveNode.setAttribute([s,'primitiveType'],'SOLID_SPHERE');
0345     else
0346         warning('unrecognized primitive type specified, defaulting to SOLID_CUBE');
0347         CSGPrimitiveNode.setAttribute([s,'primitiveType'],'SOLID_CUBE');
0348     end
0349     
0350     
0351     CSGRotationNode.appendChild(CSGPrimitiveNode);
0352     CSGScaleNode.appendChild(CSGRotationNode);
0353     CSGTranslationNode.appendChild(CSGScaleNode);
0354     
0355     
0356     CSGObjectNode.appendChild(CSGTranslationNode);
0357     
0358     ListOfCSGObjectsNode.appendChild(CSGObjectNode);
0359     
0360 end
0361 CSGeometryNode.appendChild(ListOfCSGObjectsNode);
0362 geometryDefNode.appendChild(CSGeometryNode);
0363 GeowrapperNode.appendChild(geometryDefNode);
0364 
0365 
0366 if ~isfield(CSGdata,'primitiveOnly')||CSGdata.primitiveOnly==0
0367 
0368 
0369 ParaGeometryNode = docNode.createElement('spatial:ParametricGeometry');
0370 
0371 
0372 
0373 ListOfParaObjectsNode = docNode.createElement('spatial:ListOfParametricObjects');
0374 ListOfParaObjectsNode.setAttribute('SpatialId', Meshdata.name);
0375 ListOfParaObjectsNode.setAttribute('polygonType', 'triangle');
0376 ListOfParaObjectsNode.setAttribute('domain', DomainID);
0377 
0378 
0379 
0380 for i = 1:length(Meshdata.list)
0381     object = Meshdata.list(j);
0382     name = object.name;
0383     type = object.type;
0384 
0385     ParaObjectNode = docNode.createElement('spatial:ParaObject');
0386     ParaObjectNode.setAttribute('spatialID',['Sp_', name]);
0387     
0388     ParaObjectNode.setAttribute('polygonType','triangle');
0389     ParaObjectNode.setAttribute('domain',DomainID);
0390 
0391     
0392     
0393     if isfield(object,'ordinal')
0394         ParaObjectNode.setAttribute('ordinal', num2str(ordinal));
0395     end
0396 
0397     
0398     FV = getMeshPoints(object.img);
0399 
0400     PolyObjectNode = docNode.createElement('spatial:PolygonObject');
0401     PolyObjectNode.setAttribute('pointIndex', mat2str(FV.vertices) );
0402     ParaObjectNode.appendChild(PolyObjectNode);
0403     ListOfParaObjectsNode.appendChild(ParaObjectNode);
0404 end
0405     ParaGeometryNode.appendChild(ListOfParaObjectsNode);
0406     GeowrapperNode.appendChild(ParaGeometryNode);
0407 end
0408 
0409 
0410 
0411 
0412 
0413 
0414 
0415 
0416 
0417 wrapperNode.appendChild(GeowrapperNode);
0418 docRootNode.appendChild(wrapperNode);
0419 xmlwrite(savepath, docNode);
0420 result = 1;
0421 end
0422