Home > core > getIndexes.m

getIndexes

PURPOSE ^

getIndexes

SYNOPSIS ^

function indexes=getIndexes(model, objects, type, returnLogical)

DESCRIPTION ^

 getIndexes
   Retrieves the indexes for a list of reactions or metabolites

 Input:
   model           a model structure
   objects         either a cell array of IDs, a logical vector with the
                   same number of elements as metabolites in the model,
                   of a vector of indexes
   type            'rxns', 'mets', or 'genes' depending on what to retrieve
                   'metnames' queries metabolite names, while 'metcomps'
                   allows to provide specific metabolites and their
                   compartments in the format metaboliteName[comp]. If a
                   model.ec structure exists (GECKO 3), then also
                   'ecenzymes', 'ecrxns' and 'ecgenes' are allowed
   returnLogical   Sets whether to return a logical array or an array with
                   the indexes (optional, default false)

 Output:
   indexes         can be a logical array or a double array depending on
                   the value of returnLogical

 Note: If 'ecenzymes', 'ecrxns' or 'ecgenes' are used with a GECKO 3
 model, then the indexes are from the model.ec.enzymes, model.ec.rxns or
 model.ec.genes fields, respectively.
 
 Usage: indexes=getIndexes(model, objects, type, returnLogical)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function indexes=getIndexes(model, objects, type, returnLogical)
0002 % getIndexes
0003 %   Retrieves the indexes for a list of reactions or metabolites
0004 %
0005 % Input:
0006 %   model           a model structure
0007 %   objects         either a cell array of IDs, a logical vector with the
0008 %                   same number of elements as metabolites in the model,
0009 %                   of a vector of indexes
0010 %   type            'rxns', 'mets', or 'genes' depending on what to retrieve
0011 %                   'metnames' queries metabolite names, while 'metcomps'
0012 %                   allows to provide specific metabolites and their
0013 %                   compartments in the format metaboliteName[comp]. If a
0014 %                   model.ec structure exists (GECKO 3), then also
0015 %                   'ecenzymes', 'ecrxns' and 'ecgenes' are allowed
0016 %   returnLogical   Sets whether to return a logical array or an array with
0017 %                   the indexes (optional, default false)
0018 %
0019 % Output:
0020 %   indexes         can be a logical array or a double array depending on
0021 %                   the value of returnLogical
0022 %
0023 % Note: If 'ecenzymes', 'ecrxns' or 'ecgenes' are used with a GECKO 3
0024 % model, then the indexes are from the model.ec.enzymes, model.ec.rxns or
0025 % model.ec.genes fields, respectively.
0026 %
0027 % Usage: indexes=getIndexes(model, objects, type, returnLogical)
0028 
0029 if nargin<4
0030     returnLogical=false;
0031 end
0032 
0033 if ~islogical(objects) && ~isnumeric(objects)
0034     objects=convertCharArray(objects);
0035 end
0036 type=char(type);
0037 
0038 indexes=[];
0039 
0040 switch type
0041     case 'rxns'
0042         searchIn=model.rxns;
0043     case 'mets'
0044         searchIn=model.mets;
0045     case 'genes'
0046         searchIn=model.genes;
0047     case 'metnames'
0048         searchIn=model.metNames;
0049     case 'metcomps'
0050         % If provided as metaboliteName[comp], then index search is quite
0051         % different from general approach, and therefore coded separately.
0052         mets=regexprep(objects,'(^.+)\[(.+)\]$','$1');
0053         comps=regexprep(objects,'(^.+)\[(.+)\]$','$2');
0054         indexes=zeros(1,numel(objects));
0055         for i=1:numel(objects)
0056             index=find(strcmp(mets(i),model.metNames));
0057             index=index(strcmp(comps(i),model.comps(model.metComps(index))));
0058             if ~isempty(index)
0059                 indexes(i)=index;
0060             else
0061                 error(['Could not find object ' objects{i} ' in the model']);
0062             end
0063         end
0064         indexes=indexes(:);
0065         if returnLogical==true
0066             tempIndexes=false(numel(model.mets),1);
0067             tempIndexes(indexes)=true;
0068             indexes=tempIndexes;
0069         end
0070         return %None of the remaining function needs to run if metcomps
0071     case 'ecrxns'
0072         if ~isfield(model,'ec')
0073             error('Type %s cannot be used if no model.ec structure is present.',type)
0074         end
0075         searchIn=model.ec.rxns;
0076     case 'ecenzymes'
0077         if ~isfield(model,'ec')
0078             error('Type %s cannot be used if no model.ec structure is present.',type)
0079         end
0080         searchIn=model.ec.enzymes;
0081     case 'ecgenes'
0082         if ~isfield(model,'ec')
0083             error('Type %s cannot be used if no model.ec structure is present.',type)
0084         end
0085         searchIn=model.ec.genes;
0086     otherwise
0087         if contains(objects,{'rxns','mets','metnames','metcomps','genes','ecgenes','ecenzymes','ecrxns'})
0088             error('The second and third parameter provided to run getIndexes are likely switched. Note that the second parameter is the object to find the index for, while the third parameter is the object type (''rxns'', ''mets'', ''metnames'', ''metcomps'', ''genes'', ''ecgenes'', ''ecenzymes'' or ''ecrxns'').')
0089         else
0090             error('Incorrect value of the ''type'' parameter. Allowed values are ''rxns'', ''mets'', ''metnames'', ''metcomps'', ''genes'', ''ecgenes'', ''ecenzymes'' or ''ecrxns''.');
0091        end
0092 end
0093 
0094 if iscell(objects)
0095     for i=1:numel(objects)
0096         index=find(strcmp(objects(i),searchIn));
0097         if strcmpi(type,'metnames')
0098             indexes{i}=index;
0099         elseif ~isempty(index)
0100             if length(index) > 1
0101                 error('There are multiple instances of object "%s" in the model, while "%s" type should be unique', objects{i}, type)
0102             end
0103             indexes(i)=index;
0104         else
0105             error(['Could not find object ''' objects{i} ''' in the model']);
0106         end
0107     end
0108 else
0109     %Now it's either a logical (or 0/1) array or an array with indexes. We
0110     %want it to be an array with indexes.
0111     if all(objects)
0112         %This gets weird if it's all 1
0113         indexes=objects;
0114     else
0115         indexes=find(objects);
0116     end
0117 end
0118 
0119 if returnLogical==true
0120     tempIndexes=false(numel(searchIn),1);
0121     tempIndexes(indexes)=true;
0122     indexes=tempIndexes;
0123 end
0124 
0125 indexes=indexes(:);
0126 if iscell(indexes) && length(indexes)==1
0127     indexes=indexes{1};
0128 end
0129 end

Generated by m2html © 2005