function[conditions, genes, expression]  = load_m3d_normalized_data(filename)
%function[conditions, genes, expression]  = load_m3d_normalized_data(filename)
%where filename is the name of a file containing the normalized expression data in
%the format available for bulk download from Many Microbe Microarrays Database 
%http://m3d.bu.edu/norm/

disp('I am going to try and load the data.');
disp('Warning: either matlab is a slow file parser or J is a bad programmer');
disp('either way be prepared to wait a few minutes for this file to be read.');

% open the m3d file
fid = fopen(filename);

% scan the header line with all of the experiment names
header = fgetl(fid);
conditions = textscan(header, '%s');
conditions = conditions{1};

% check if the first row, first column had a definition on it; if so remove
if (findstr('genes', conditions{1}))
	conditions(1) = [];
end;

% now we know how many columns to expect
numConditions = length(conditions);

% now read the rest of the file, extracting the gene name and numerical data
infile = 1;
count=1;
while (infile)
	g = fscanf(fid, '%s', 1);

	if (g)
		infile=1;
		genes{count} = g;
		num = fscanf(fid, '%f', numConditions);
		% add the numbers to the expression matrix
		expression(count,:) = num;
	else 
		infile=0;
	end;

	count = count+1;
end;

fclose(fid);
