% Ella Bingham, March 2006
%
% Reorders the columns of data, according to a user-chosen method.
%
% Inputs:
%
% data - a data matrix with sites as rows and taxa as columns. It is
% advisable (but not necessary) to have the rows sorted, as the
% columns will be sorted here, and a figure of sorted data will be drawn.
%
% taxoninfo - auxiliary information about taxa present in the data matrix
%
% sorting_method - 'spectral' for spectral ordering of taxa, 'firstocc'
% for ordering based on the first occurrence of the taxon in the input
% data (assumes the rows of input data are sorted somehow). NOTES: (1) in
% spectral ordering, some taxa may be outliers due to an
% almost-disconnected nature of the data matrix; the outliers are not
% removed here. (2) 'spectral' and 'firstocc' may return opposite
% orderings as the spectral ordering does not distinguish the direction
% of time.
% 
% similarity_measure - 'wdot' or 'dot' if "sorting_method" was chosen as
% 'spectral'; otherwise obsolete and you can use similarity_method=[].
%
% Outputs:
% 
% columnsorted_data, sorted_taxoninfo - data and auxiliary information,
% in the same format as in the input, but taxa (columns of data; entries
% of taxoninfo) are sorted.

function [columnsorted_data,sorted_taxoninfo] = sort_taxa(data,taxoninfo,sorting_method,similarity_measure)

[rows,cols] = size(data);
switch sorting_method
 case 'spectral'
  [spcoeff_t,simm_t,lapm_t] = laplacian(data',similarity_measure); 
  [junk,ind_t] = sort(spcoeff_t);
 case 'firstocc'
  for taxon=1:cols
     firstoccurrence(taxon) = min(find(data(:,taxon)));
  end
  [junk,ind_t] = sort(firstoccurrence);
end
columnsorted_data = data(:,ind_t);

% Plot the spectral ordered data. Assume rows were sorted in the input data.
figure, imagesc(1-columnsorted_data), colormap(gray), 
xlabel('Taxa'), ylabel('Sites')

% Sort taxoninfo
sorted_taxoninfo = taxoninfo;
names = fieldnames(taxoninfo);
if ~isempty(names)
   for field=1:max(size(names))
      fieldcontents = getfield(taxoninfo,char(names(field)));
      sorted_taxoninfo = setfield(sorted_taxoninfo,char(names(field)),fieldcontents(ind_t));
   end
end

