home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / BarlaA / sw / matlab / Cromwell / collateSpectra.m < prev    next >
Text File  |  2007-12-04  |  4KB  |  101 lines

  1. function [spec, mass] = collateSpectra(dirName, rawOrCorrected, oneOrMany, skipLines)
  2. % collateSpectra(DIRECTORY, RAWORCORRECTED, ONEORMANY, SKIPLINES)
  3. %
  4. % This function collects an entire set of spectra into a single matrix.
  5. % The DIRECTORY argument pointws the function to the top-level direcctory
  6. % containing the data. The argument RAWORCORRECTED determines whether we
  7. % are collating the raw spectra or the baseline-corrected and normalized
  8. % spectra. If RAWORCORRECTED takes the value "raw", then the data is found
  9. % in the subdirectory "RawSpectra" and is described by the file "RawFiles.txt";
  10. % the format of this file is defined in the online help for batchProcessRawFiles.
  11. % If RAWORCORRECTED taskes on the value "corrected", then the data is found in
  12. % the subdirectory "CorrectedSpectra" and is described by the file named
  13. % "CorrectedFiles.txt"; this has the same format as the corresponding raw file.
  14. % The argument ONEORMANY, which can only take on the values "one" or "many",
  15. % tells us if the data files contiaing a single spectrum or multiple spectra.
  16. % Finally, the SKIPLINES argument tells us how many header lines there are at
  17. % the top of each data file.
  18. %
  19. % The function returns two items
  20. %    SPEC = a matrix, with one row per spectrum and one column for each time point.
  21. %    MASS = a vector with (some estimate) of the common masses.
  22. %
  23. % Note: this routine does not do any fine tuning of the alignment across spectra.
  24. % It implicitly assumesd all the masses are the same, and that the time points in
  25. % different spectra coincide with the same masses. Further, everything is truncated
  26. % to the length of the shortest spectrum in the set.
  27.  
  28. % Copyright (c) 2003, 2004 UT M.D. Anderson Cancer Center. All rights reserved.
  29. % See the accompanying file "license.txt" for details.
  30.  
  31. % The original version of this file was created by Kevin Coombes on 1 April 2003.
  32. % $Revision: 2.2 $
  33. % Last modified by $Author: krc $ on $Date: 2003/10/14 22:40:19 $.
  34.  
  35. startDir = pwd;
  36. cd(dirName);
  37.  
  38. % are we doing raw spectra or corrected?
  39. if strcmp(rawOrCorrected, 'raw'),
  40.    description = 'RawFiles.txt';
  41.    subdir = 'RawSpectra';
  42.    magic = 1;
  43. elseif strcmp(rawOrCorrected(1:3), 'cor'),
  44.    description = 'CorrectedFiles.txt';
  45.    subdir = 'CorrectedSpectra';
  46.    magic = 2;
  47.    if nargin < 3,
  48.       oneOrMany = 'one';
  49.    end
  50. else
  51.    return
  52. end
  53.  
  54. % get the list of source files
  55. if (strcmp(oneOrMany, 'one')),
  56.    inputFile = textread(description, '%s', 'whitespace', '\b\r\n\t');
  57.    outputFile = inputFile;
  58.    columnIndex = repmat(1, size(inputFile));
  59.    columnHead = repmat('Intensity', size(inputFile));
  60.    distinctInputFile = inputFile
  61. elseif (strcmp(oneOrMany, 'many')),
  62.    [inputFile, columnIndex, columnHead, outputFile] = textread(description, '%s%d%s%s', 'delimiter', '\t');
  63.     distinctInputFile = unique(inputFile)
  64. else
  65.    return;
  66. end
  67.  
  68. % read in the raw data from all the source files
  69. readError = ['error(''could not read source file'')'];
  70. reshapeError = ['error(''bad sizes passed to reshape'')'];
  71. counter = zeros(length(distinctInputFile), 2);    % number of data points in each spectrum
  72. cd(subdir)
  73. for i = 1:length(distinctInputFile),
  74.    filename = char(distinctInputFile(i))
  75.    dataname = truncateName(filename)    % danger: part before the first period must be unique
  76.    numColumns(i) = magic + sum(strcmp(inputFile, filename)); % how many columns do we expect?
  77.    n = numColumns(i)
  78.    nstr = num2str(numColumns(i));
  79.    cmd = [dataname '= textread(''' filename ''', ''%f'', ''headerlines'',' num2str(skipLines) ');']
  80.    eval(cmd, readError);        % read it as one long column
  81.    cmd = [dataname '= reshape(' dataname ', [' nstr ', length(' dataname ')/' nstr ']);'];
  82.    eval(cmd, reshapeError);    % then reshape it to the correct size
  83.    eval(['counter(' num2str(i) ',:) = [' num2str(n+1-magic) ', length(' dataname ') ];']);
  84. end
  85. cd ..
  86.  
  87. chop = min(counter(:,2));                            % shortest spectrum wins
  88. samp = sum(counter(:,1)) - length(counter);    % number of spectra
  89.  
  90. spec = zeros(samp, chop);
  91. chop = num2str(chop);
  92. for i = 1:length(outputFile),
  93.    filename = char(inputFile(i));                    % which source file?
  94.    dataname = truncateName(filename)                % which variable holds the data?
  95.    n = num2str(columnIndex(i) + 1);                    % add 1 for the M/Z values
  96.    eval(['spec(' num2str(i) ', :) = ' dataname '(' n ', 1:' chop ');']);    % get the intensities
  97.    eval(['mass = ' dataname '(1, 1:' chop ');']);
  98. end
  99.  
  100. cd(startDir);
  101.