function theURL = probesetlink(affyStruct,theID,varargin)
% PROBESETLINK links to NetAffx Web site
%
%   PROBESETLINK(affyStruct,ID) displays information from the NetAffx Web
%   site about probe set ID from the CHP or CDF structure affyStruct. ID
%   can be the index of the probe set or the probe set name.
%
%   URL = PROBESETLINK(affyStruct,ID) returns the URL for the information.
%
%   PROBESETLINK(...,'SOURCE',true) links to the data source (e.g., GenBank,
%   Flybase) for the probe set.
%
%   PROBESETLINK(...,'BROWSER',true) displays the information in the system
%   Web browser.
%
%   URL = PROBESETLINK(...,'NODISPLAY',true) returns the URL but does not
%   open a browser.
%
%   Note that the NetAffx web site requires you to register and provide a
%   username and password.
%
%   Example:
%       chpStruct = affyread('Drosophila-121502.chp',...
%                                  'D:\Affymetrix\LibFiles\DrosGenome1')
%       probesetlink(chpStruct,'142176_at');
%
%   See also AFFYREAD, PROBELIBRARYINFO, PROBESETLOOKUP, PROBESETPLOT, 
%   PROBESETVALUES.

%   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:44:58 $


browserFlag = false;
sourceFlag = false;
displayFlag = true;
cFlag = false;  % for CHP or CDF structs
ginFlag = false;
% figure out which type of struct we have

if isfield(affyStruct,'ChipType')
    cFlag = true;
elseif isfield(affyStruct,'ProbeSetName')
    ginFlag = true;
else
    error('Bioinfo:UnsupportedAffyStruct',...
        '%s only supports CHP, CDF and GIN file structures',mfilename);
end
% get the ID
if ischar(theID)
    if cFlag
        ID = strmatch(theID,{affyStruct.ProbeSets.Name});%#ok
    elseif ginFlag
        ID = strmatch(theID,{affyStruct.ProbeSetName});%#ok
    end
    if isempty(ID)
        error('Bioinfo:UnknownProbeName',...
            'Unknown probe set name: %s.',theID);
    elseif length(ID)>1
        warning('Bioinfo:AmbiguousParameterName',...
            'Ambiguous probe set name: %s.',theID);
        ID = ID(1);
    end
else
    ID = theID;
end

% deal with the various inputs
if nargin > 2
    if rem(nargin,2) == 1
        error('Bioinfo:IncorrectNumberOfArguments',...
            'Incorrect number of arguments to %s.',mfilename);
    end
    okargs = {'browser','source','nodisplay'};
    for j=1:2:nargin-2
        pname = varargin{j};
        pval = varargin{j+1};
        k = strmatch(lower(pname), okargs);%#ok
        if isempty(k)
            error('Bioinfo:UnknownParameterName',...
                'Unknown parameter name: %s.',pname);
        elseif length(k)>1
            error('Bioinfo:AmbiguousParameterName',...
                'Ambiguous parameter name: %s.',pname);
        else
            switch(k)
                case 1  % browser
                    browserFlag = opttf(pval);
                    if isempty(browserFlag)
                        error('Bioinfo:InputOptionNotLogical','%s must be a logical value, true or false.',...
                            upper(char(okargs(k))));
                    end
                case 2  % source
                    sourceFlag = opttf(pval);
                    if isempty(sourceFlag)
                        error('Bioinfo:InputOptionNotLogical','%s must be a logical value, true or false.',...
                            upper(char(okargs(k))));
                    end
                  case 3  % no display
                    displayFlag = ~opttf(pval);
                    if isempty(displayFlag)
                        error('Bioinfo:InputOptionNotLogical','%s must be a logical value, true or false.',...
                            upper(char(okargs(k))));
                    end
            end
        end
    end
end

% Extract the data from the big struct
theChip = affyStruct.ChipType;
theName = affyStruct.ProbeSets(ID).Name;
if sourceFlag
    [d1,d2,d3,d4,link] = probesetlookup(chpStruct,chpStruct.ProbeSets(ID).Name); %#ok
else
    theName = strrep(theName,'#','%23');
    link = sprintf('https://www.affymetrix.com/LinkServlet?array=%s&probeset=%s',...
        theChip,theName);
end
% open the link in a browser
if displayFlag
    if browserFlag
        web(link,'-browser');
    else
        web(link);
    end
end
% set the output if required
if nargout > 0
    theURL = link;
end