function [spec, mass] = collateSpectra(dirName, rawOrCorrected, oneOrMany, skipLines)
% collateSpectra(DIRECTORY, RAWORCORRECTED, ONEORMANY, SKIPLINES)
%
% This function collects an entire set of spectra into a single matrix.
% The DIRECTORY argument pointws the function to the top-level direcctory
% containing the data. The argument RAWORCORRECTED determines whether we
% are collating the raw spectra or the baseline-corrected and normalized
% spectra. If RAWORCORRECTED takes the value "raw", then the data is found
% in the subdirectory "RawSpectra" and is described by the file "RawFiles.txt";
% the format of this file is defined in the online help for batchProcessRawFiles.
% If RAWORCORRECTED taskes on the value "corrected", then the data is found in
% the subdirectory "CorrectedSpectra" and is described by the file named
% "CorrectedFiles.txt"; this has the same format as the corresponding raw file.
% The argument ONEORMANY, which can only take on the values "one" or "many",
% tells us if the data files contiaing a single spectrum or multiple spectra.
% Finally, the SKIPLINES argument tells us how many header lines there are at
% the top of each data file.
%
% The function returns two items
%	SPEC = a matrix, with one row per spectrum and one column for each time point.
%	MASS = a vector with (some estimate) of the common masses.
%
% Note: this routine does not do any fine tuning of the alignment across spectra.
% It implicitly assumesd all the masses are the same, and that the time points in
% different spectra coincide with the same masses. Further, everything is truncated
% to the length of the shortest spectrum in the set.

% Copyright (c) 2003, 2004 UT M.D. Anderson Cancer Center. All rights reserved.
% See the accompanying file "license.txt" for details.

% The original version of this file was created by Kevin Coombes on 1 April 2003.
% $Revision: 2.2 $
% Last modified by $Author: krc $ on $Date: 2003/10/14 22:40:19 $.

startDir = pwd;
cd(dirName);

% are we doing raw spectra or corrected?
if strcmp(rawOrCorrected, 'raw'),
   description = 'RawFiles.txt';
   subdir = 'RawSpectra';
   magic = 1;
elseif strcmp(rawOrCorrected(1:3), 'cor'),
   description = 'CorrectedFiles.txt';
   subdir = 'CorrectedSpectra';
   magic = 2;
   if nargin < 3,
      oneOrMany = 'one';
   end
else
   return
end

% get the list of source files
if (strcmp(oneOrMany, 'one')),
   inputFile = textread(description, '%s', 'whitespace', '\b\r\n\t');
   outputFile = inputFile;
   columnIndex = repmat(1, size(inputFile));
   columnHead = repmat('Intensity', size(inputFile));
   distinctInputFile = inputFile
elseif (strcmp(oneOrMany, 'many')),
   [inputFile, columnIndex, columnHead, outputFile] = textread(description, '%s%d%s%s', 'delimiter', '\t');
	distinctInputFile = unique(inputFile)
else
   return;
end

% read in the raw data from all the source files
readError = ['error(''could not read source file'')'];
reshapeError = ['error(''bad sizes passed to reshape'')'];
counter = zeros(length(distinctInputFile), 2);	% number of data points in each spectrum
cd(subdir)
for i = 1:length(distinctInputFile),
   filename = char(distinctInputFile(i))
   dataname = truncateName(filename)	% danger: part before the first period must be unique
   numColumns(i) = magic + sum(strcmp(inputFile, filename)); % how many columns do we expect?
   n = numColumns(i)
   nstr = num2str(numColumns(i));
   cmd = [dataname '= textread(''' filename ''', ''%f'', ''headerlines'',' num2str(skipLines) ');']
   eval(cmd, readError);		% read it as one long column
   cmd = [dataname '= reshape(' dataname ', [' nstr ', length(' dataname ')/' nstr ']);'];
   eval(cmd, reshapeError);	% then reshape it to the correct size
   eval(['counter(' num2str(i) ',:) = [' num2str(n+1-magic) ', length(' dataname ') ];']);
end
cd ..

chop = min(counter(:,2));							% shortest spectrum wins
samp = sum(counter(:,1)) - length(counter);	% number of spectra

spec = zeros(samp, chop);
chop = num2str(chop);
for i = 1:length(outputFile),
   filename = char(inputFile(i));					% which source file?
   dataname = truncateName(filename)				% which variable holds the data?
   n = num2str(columnIndex(i) + 1);					% add 1 for the M/Z values
   eval(['spec(' num2str(i) ', :) = ' dataname '(' n ', 1:' chop ');']);	% get the intensities
   eval(['mass = ' dataname '(1, 1:' chop ');']);
end

cd(startDir);
