Home > cellorganizer > utilities > instance2SBML.m

instance2SBML

PURPOSE ^

INSTANCE2SBML Writes data struct to a SBML-Spatial file. If an existing file is provided

SYNOPSIS ^

function [ result ] = instance2SBML( CSGdata, Meshdata, savepath, SBMLfile )

DESCRIPTION ^

INSTANCE2SBML Writes data struct to a SBML-Spatial file. If an existing file is provided
this method will append to the end of the file.

Inputs:
 CSGdata = struct created using createSBMLstruct.m in slml2img.m
 Meshdata = cell array containing image arrays for mesh type objects to be saved
 savepath = output filename
 SBMLfile = optional path to existing file to append to.

Outputs:
 result = boolean flag indicating success
 saves .xml file in SBML-Spatial format.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ result ] = instance2SBML( CSGdata, Meshdata, savepath, SBMLfile )
0002 %INSTANCE2SBML Writes data struct to a SBML-Spatial file. If an existing file is provided
0003 %this method will append to the end of the file.
0004 %
0005 %Inputs:
0006 % CSGdata = struct created using createSBMLstruct.m in slml2img.m
0007 % Meshdata = cell array containing image arrays for mesh type objects to be saved
0008 % savepath = output filename
0009 % SBMLfile = optional path to existing file to append to.
0010 %
0011 %Outputs:
0012 % result = boolean flag indicating success
0013 % saves .xml file in SBML-Spatial format.
0014 
0015 
0016 %9/17/13
0017 %Authors: Rohan Arepally and Devin Sullivan
0018 %Edited:
0019 %9/18/13 D. Sullivan - formatting and support for parametric objects
0020 %9/24/13 D. Sullivan - edited for VCell compatability.
0021 % Copyright (C) 2012 Murphy Lab
0022 % Lane Center for Computational Biology
0023 % School of Computer Science
0024 % Carnegie Mellon University
0025 %
0026 % This program is free software; you can redistribute it and/or modify
0027 % it under the terms of the GNU General Public License as published
0028 % by the Free Software Foundation; either version 2 of the License,
0029 % or (at your option) any later version.
0030 %
0031 % This program is distributed in the hope that it will be useful, but
0032 % WITHOUT ANY WARRANTY; without even the implied warranty of
0033 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0034 % General Public License for more details.
0035 %
0036 % You should have received a copy of the GNU General Public License
0037 % along with this program; if not, write to the Free Software
0038 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0039 % 02110-1301, USA.
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 %Alias s to 'spatial:'. this will be used to denote spatial specific
0050 %attributes and nodes.
0051 s = 'spatial:';
0052 
0053 %Create initial Node Object
0054 docNode = com.mathworks.xml.XMLUtils.createDocument('sbml');
0055 %Create Root node and define the namespace for SBML-Spatial
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 %Added the 'req' namespace
0062 docRootNode.setAttribute('xmlns:req','http://www.sbml.org/sbml/level3/version1/requiredElements/version1');
0063 docRootNode.setAttribute(['req:','required'],'true');
0064 
0065 % D. Sullivan 9/18/13
0066 %probably should have an actual metaID, optional so ignoring for now
0067 % docRootNode.setAttribute('metaid','_000000');
0068 docRootNode.setAttribute('xmlns:spatial','http://www.sbml.org/sbml/level3/version1/spatial/version1');
0069 docRootNode.setAttribute([s,'required'],'true');
0070 
0071 
0072 
0073 %Create model node to wrap everything in
0074 wrapperNode = docNode.createElement('model');
0075 wrapperNode.setAttribute('id','CellOrganizer2_0');
0076 wrapperNode.setAttribute('name',['CellOrganizer2_0',CSGdata.name]);
0077 
0078 %Define units
0079 %list
0080 ListOfUnitDefinitions = docNode.createElement('listOfUnitDefinitions');
0081 %substance
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 %volume
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 %area
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 %length
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 %time
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 %nmol
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 %Define compartments
0157 %list
0158 ListOfCompartments = docNode.createElement('listOfCompartments');
0159 %actual compartment
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 %compartment mapping
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 %add children node
0178 compartment.appendChild(mapping);
0179 ListOfCompartments.appendChild(compartment);
0180 %add compartments to model
0181 wrapperNode.appendChild(ListOfCompartments);
0182 
0183 %set up parameters
0184 ListOfParameters = docNode.createElement('listOfParameters');
0185 %actual parameters
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     %create spatial:spatialSymbolReference node
0195     symbolref = docNode.createElement([s,'spatialSymbolReference']);
0196     symbolref.setAttribute([s,'spatialId'],dimensionNames(i));
0197     symbolref.setAttribute([s,'type'],'coordinateComponent');
0198     %add spatial:spatialSymbolReference node to x parameter node
0199     parameter.appendChild(symbolref);
0200     %add parameter to list
0201     ListOfParameters.appendChild(parameter);
0202 end
0203 wrapperNode.appendChild(ListOfParameters);
0204 
0205 %%%Set up the Geometry node
0206 GeowrapperNode = docNode.createElement('spatial:geometry');
0207 GeowrapperNode.setAttribute([s,'coordinateSystem'],'Cartesian');
0208 %Create a globally unique ID
0209 % dicomWrapper = num2str(dicomuid());
0210 % dicomWrapper = strrep(dicomWrapper, '.', '');
0211 % GeowrapperNode.setAttribute('id', ['co',dicomWrapper]);
0212 % GeowrapperNode.setAttribute('geometryType', 'primitive');
0213 %%%
0214 
0215 %%%Set up the ListOfCoordinateComponent node
0216 ListOfCoordCompNode = docNode.createElement([s,'listOfCoordinateComponents']);
0217 %for each dimension
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     %define dimensions
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     %add component to the list
0236     ListOfCoordCompNode.appendChild(CoordCompNode);
0237 end
0238 GeowrapperNode.appendChild(ListOfCoordCompNode);
0239 %%%
0240 
0241 %%%Set up the ListOfDomainTypes node
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 %%%Set up ListOfDomains
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 %%%Define the Geometry
0267 geometryDefNode = docNode.createElement([s,'listOfGeometryDefinitions']);
0268 % dicom = num2str(dicomuid());
0269 % dicom = strrep(dicom, '.', '');
0270 % geometryDefNode.setAttribute('spatialId', ['co',dicom]);
0271 %%%
0272 
0273 
0274 
0275 %%%Define all the CSG (Primitive) type geometries
0276 CSGeometryNode = docNode.createElement([s,'csGeometry']);
0277 CSGeometryNode.setAttribute([s,'spatialId'], 'CSG_Geometry1');
0278 % geometryDefNode.appendChild(CSGeometryNode);
0279 
0280 ListOfCSGObjectsNode = docNode.createElement([s,'listOfCSGObjects']);
0281 % ListOfCSGObjectsNode.setAttribute('nameId', CSGdata.name);
0282 %add each CSGObject
0283 % for j=1:length(CSGdata.list)
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     %     CSGObjectNode.setAttribute([s,'spatialID'],['Sp_', name]);
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         % CSGObjectNode.setAttribute([s,'ordinal'], '0');
0300     end
0301     
0302     %     CSGTransformationNode = docNode.createElement([s,'csgTransformation']);
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         %         CSGRotationNode.setAttribute([s,'rotateAxisX'], num2str(rotation(1)));
0320         %         CSGRotationNode.setAttribute([s,'rotateAxisY'], num2str(rotation(2)));
0321         %         CSGRotationNode.setAttribute([s,'rotateAxisZ'], num2str(rotation(3)));
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     % CSGPrimitiveNode.setAttribute([s,'spatialId'],'cube');
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     %     CSGScaleNode.appendChild(CSGPrimitiveNode);
0351     CSGRotationNode.appendChild(CSGPrimitiveNode);
0352     CSGScaleNode.appendChild(CSGRotationNode);
0353     CSGTranslationNode.appendChild(CSGScaleNode);
0354     %     CSGNodeNode.appendChild(CSGTransformationNode);
0355     %     CSGNodeNode.appendChild(CSGPrimitiveNode);
0356     CSGObjectNode.appendChild(CSGTranslationNode);
0357     %     CSGObjectNode.appendChild(CSGNodeNode);
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 %%Define all the Mesh objects
0368 % Parametric Geometry
0369 ParaGeometryNode = docNode.createElement('spatial:ParametricGeometry');
0370 % geometryDefNode.appendChild(ParaGeometryNode);
0371 
0372 %List of Parametric Objects
0373 ListOfParaObjectsNode = docNode.createElement('spatial:ListOfParametricObjects');
0374 ListOfParaObjectsNode.setAttribute('SpatialId', Meshdata.name);%(name e.g. 'cell'/'nuc')
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     %for now we will assume we are only dealing with triangulated meshes
0388     ParaObjectNode.setAttribute('polygonType','triangle');
0389     ParaObjectNode.setAttribute('domain',DomainID);
0390 
0391     %This is actually not in the specification for parametric objects,
0392     %but I think it is important to match with CSGObjects. D. Sullivan
0393     if isfield(object,'ordinal')
0394         ParaObjectNode.setAttribute('ordinal', num2str(ordinal));
0395     end
0396 
0397     %Get mesh points
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 %%%Append all the children nodes and save the file
0413 
0414 % geometryDefNode.appendChild(CSGeometryNode);
0415 % GeowrapperNode.appendChild(geometryDefNode);
0416 
0417 wrapperNode.appendChild(GeowrapperNode);
0418 docRootNode.appendChild(wrapperNode);
0419 xmlwrite(savepath, docNode);
0420 result = 1;
0421 end
0422

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005