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]
   returnLogical   Sets whether to return a logical array or an array with
                   the indexes (opt, default false)

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

     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]
0014 %   returnLogical   Sets whether to return a logical array or an array with
0015 %                   the indexes (opt, default false)
0016 %
0017 %   Output:
0018 %   indexes         can be a logical array or a double array depending on
0019 %                   the value of returnLogical
0020 %
0021 %     Usage: indexes=getIndexes(model, objects, type, returnLogical)
0022 
0023 if nargin<4
0024     returnLogical=false;
0025 end
0026 
0027 if ~islogical(objects) && ~isnumeric(objects)
0028     objects=convertCharArray(objects);
0029 end
0030 type=char(type);
0031 
0032 indexes=[];
0033 
0034 switch type
0035     case 'rxns'
0036         searchIn=model.rxns;
0037     case 'mets'
0038         searchIn=model.mets;
0039     case 'genes'
0040         searchIn=model.genes;
0041     case 'metnames'
0042         searchIn=model.metNames;
0043     case 'metcomps'
0044         % If provided as metaboliteName[comp], then index search is quite
0045         % different from general approach, and therefore coded separately.
0046         mets=regexprep(objects,'(^.+)\[(.+)\]$','$1');
0047         comps=regexprep(objects,'(^.+)\[(.+)\]$','$2');
0048         indexes=zeros(1,numel(objects));
0049         for i=1:numel(objects)
0050             index=find(strcmp(mets(i),model.metNames));
0051             index=index(strcmp(comps(i),model.comps(model.metComps(index))));
0052             if ~isempty(index)
0053                 indexes(i)=index;
0054             else
0055                 error(['Could not find object ' objects{i} ' in the model']);
0056             end
0057         end
0058         indexes=indexes(:);
0059         if returnLogical==true
0060             tempIndexes=false(numel(model.mets),1);
0061             tempIndexes(indexes)=true;
0062             indexes=tempIndexes;
0063         end
0064         return %None of the remaining function needs to run if metcomps
0065     otherwise
0066         if contains(objects,{'rxns','mets','metnames','metcomps','genes'})
0067             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'' or ''genes'').')
0068         else
0069             error('Incorrect value of the ''type'' parameter. Allowed values are ''rxns'', ''mets'', ''metnames'', ''metcomps'' or ''genes''.');
0070         end
0071 end
0072 
0073 if iscell(objects)
0074     for i=1:numel(objects)
0075         index=find(strcmp(objects(i),searchIn));
0076         if strcmpi(type,'metnames')
0077             indexes{i}=index;
0078         elseif ~isempty(index)
0079             indexes(i)=index;
0080         else
0081             error(['Could not find object ''' objects{i} ''' in the model']);
0082         end
0083     end
0084 else
0085     %Now it's either a logical (or 0/1) array or an array with indexes. We
0086     %want it to be an array with indexes.
0087     if all(objects)
0088         %This gets weird if it's all 1
0089         indexes=objects;
0090     else
0091         indexes=find(objects);
0092     end
0093 end
0094 
0095 if returnLogical==true
0096     tempIndexes=false(numel(searchIn),1);
0097     tempIndexes(indexes)=true;
0098     indexes=tempIndexes;
0099 end
0100 
0101 indexes=indexes(:);
0102 if iscell(indexes) && length(indexes)==1
0103     indexes=indexes{1};
0104 end
0105 end

Generated by m2html © 2005