function [table,lookup] = probesetvalues(CELStruct,CDFStruct,theID)
% PROBESETVALUES extracts probe set values from probe results
%
%   PSVALUES = PROBESETVALUES(CELStruct,CDFStruct,PS) creates a table of
%   values for probe set PS from the probe data in a CEL file structure
%   CELStruct, where PS is a probe set index or probe set name from the CDF
%   library file structure CDFStruct.
%
%   PSVALUES is a matrix with 18 columns and one row for each probe pair in
%   the probe set. The columns correspond to the fields in a CHP probe set
%   data structure:
%
%     'ProbeSetNumber'
%     'ProbePairNumber'
%     'UseProbePair'
%     'Background'
%     'PMPosX'
%     'PMPosY'
%     'PMIntensity'
%     'PMStdDev'
%     'PMPixels'
%     'PMOutlier'
%     'PMMasked'
%     'MMPosX'
%     'MMPosY'
%     'MMIntensity'
%     'MMStdDev'
%     'MMPixels'
%     'MMOutlier'
%     'MMMasked'
%
%   There are some minor differences between the output of this function
%   and the data in a CHP file. The PM and MM intensity values in the CHP
%   file are normalized by the Affymetrix software. This function returns
%   the raw intensity values. The 'UseProbePair' and 'Background' fields
%   are only returned by this function for compatibility with the CHP probe
%   set data structure and are always set to zero.
%
%   Example:
%       celStruct = affyread('Drosophila-121502.cel');
%       cdfStruct = affyread(...
%                    'D:\Affymetrix\LibFiles\DrosGenome1\DrosGenome1.CDF');
%       % get the values for probe set 147439_at
%       psvals = probesetvalues(celStruct,cdfStruct,'147439_at')
%
%   See also AFFYREAD, PROBELIBRARYINFO, PROBESETLINK, PROBESETLOOKUP.

%   Affymetrix and NetAffx are registered trademarks of Affymetrix, Inc.

% Copyright 2003-2004 The MathWorks, Inc.
% $Revision: 1.1.12.1 $   $Date: 2004/12/24 20:45:01 $ 

% get the ID
if ischar(theID)
    ID = strmatch(theID,{CDFStruct.ProbeSets.Name});%#ok
    if isempty(ID)
        error('Bioinfo:probesetbalues:UnknownProbeName',...
            'Unknown probe set name: %s.',theID);
    elseif length(ID)>1
        warning('Bioinfo:probesetbalues:AmbiguousParameterName',...
            'Ambiguous probe set name: %s.',theID);
        ID = ID(1);
    end
else
    ID = theID;
end

numCols = CDFStruct.Cols;

NumPairs = CDFStruct.ProbeSets(ID).NumPairs;
table = zeros(NumPairs,18);
lookup = zeros(NumPairs,2);
thePairs = CDFStruct.ProbeSets(ID).ProbePairs;
for inner = 1:NumPairs
    PMX = thePairs(inner,3);
    PMY = thePairs(inner,4);
    PMRow = PMY*numCols + PMX +1;
    table(inner,1) = ID-1;
    table(inner,2) = inner-1;
    table(inner,5) = PMX;
    table(inner,6) = PMY;
    lookup(inner,1) = PMRow;
    table(inner,7:11) = CELStruct.Probes(PMRow,3:7);
    MMX = thePairs(inner,5);
    MMY = thePairs(inner,6);
    MMRow = MMY*numCols + MMX + 1;
    lookup(inner,2) = MMRow;
    table(inner,12) = MMX;
    table(inner,13) = MMY;
    table(inner,14:18) = CELStruct.Probes(MMRow,3:7);
end


