%Author: Tara Gianoulis
%Create Date: March 31, 2008
%Hack to color code for ipath figure, still too many magic numbers, v3
%NOTE: still some bugs related to setting isScaled=1, scaled version not
%thoroughly tested
%**************************************************************************
%Argument List
%X - dataset 
%colId -  column containing value the color gradient is to be built ofr
%Set isScaled to 1 for a scaled color gradient and not 1 for not scaled
%filename - name of file to write too
%Assumes a max of 64 bit color
%cmap is the actual color map
%This can be generated in matlab by executing 
%cmap=round(colormap*255);
%This can be customized within Matlab or any other 3 column RGB codes are
%accepted)
%Remember though that matlab colormap refers to things in this manner 
%RGB Code for aquamarine is [127/255 1 212/255]
%**************************************************************************
%Returns: Id SanityCheckValue RGB Code tab delimited
%The result of this is a sanity check and still needs further processing to be acceptable for ipath format
%ipath format is:
%column 1 - ID (e.g. for module M00101, for KEGG 00230) 
%All others need to be mapped to one of these two formats to work
%column 2 - W$linewidth (where $linewidth is line width, 10 works well)
%column 3 - RGB(0,0,191)
%So an example row would look like this (separated by a single space NOT a
%tab)
%M00101 W10 RGB(0,0,191)
%**************************************************************************

function [colorMapped]=mapfromColorGradient_moreGeneral(X, colId, isScaled, cmap, filename)

    %Empty edge vector
    edgeVector=[];
    con=64;

    %a value of 1 gives the scaled color gradient and 0 not scaled
    if isScaled==1
        numerator=1;
    else
        numerator=max(abs(X(:,2)));
    end

    %Sets increments to color-code
    incrementor=numerator/con
    i=numerator;
    j=1;
    while(i>0)
        i=i-incrementor;
        edgeVector(j)=i;
        j=j+1;
    end

    edgeVector=sort(edgeVector, 'ascend');
    size(edgeVector)

    %Places the values into bins generated from color incrementor
    [sortValue, sortIndex]=sort(abs(X(:,colId)),'ascend');

    %This is for the scaled ones
    if isScaled==1
      scaled=abs(X(sortIndex,colId))/max(abs(X(:,colId)));
      N=histc(scaled,edgeVector)  
    else
    %This is for the not scaled ones
        N=histc(abs(X(:,colId)),edgeVector)
    end

    %Takes care of edge effect
    if(sum(N)<size(X,1))
        N(con)=N(con)+1;
        sum(N)
    end
        size(N)

    cindex=1:size(edgeVector,2);
    cindex=cindex';
    cindex=[cindex N];
    sum(cindex(:,2))

    elementMap=[];
    %Assigns boundaries to particular elements
    j=1;
    for i=1:con
        k=1;
        while(k<=cindex(i,2) && cindex(i,2)>0)
            elementMap(j,:)=[j cindex(i,1)];
            j=j+1;
            k=k+1      
        end
    end


    %Retrieves color index from mapping - 1 is expected to be the
    %identifier and colId is the value as a sanity check
    
    
    if isScaled==1
    %Retrieves color index from mapping for SCALED
        colorMapped=[X(sortIndex,1) scaled mapscaled(elementMap(:,2),:)]
    else
        colorMapped=[X(sortIndex,[1, colId]) cmap(elementMap(:,2),:)];
    end
    dlmwrite(filename, colorMapped,'delimiter','\t');
    return colorMapped;
end
    