function [sequencedistances,numberexcluded]=transcript_distances_many_sequences(tpminput,minmax,numreps)
%description: method of finding confidence intervals for pseudotime sequence assigned to cells based on transcripts
    %takes as input an array of tpm data for transcripts as generated by hi seq, with transcripts as rows and cells as columns
    %the cells (columns) should be sorted according to pseudotime when input
    %removes all transcripts with maximum value below a threshold
    %establishes a unit distance for each transcript, by taking its maximum minus its minimum
    %finds the distance from each cell to the next, taking for each transcript the (abs value of the) difference of its values in the two cells, normalized to the unit distance for that transcript,
    %and then summing values for all transcripts
    %this sum is the 'distance' between those two cells.  the total distance, the sum of distances between each successive pair, is computed for the whole sequence of cells
    %
    %the sequence is then randomly shuffled and distance through the sequence is recomputed, again and again
    %the distance for any given sequence can be compared to this distribution of distances
    %
    %the distance for the input array as received is the first entry in the returned vector of distances
%called by: command line
%calls:
%notes: 
%code confirmed date:

numcells=size(tpminput,2);
a=max(tpminput'); b=a<minmax; numberexcluded=length(find(b)); tpmgoodrna=tpminput;  tpmgoodrna(b,:)=[];
c=max(tpmgoodrna'); d=min(tpmgoodrna'); unitdistances=c-d; unitdistancearray=repmat(unitdistances,numcells-1,1);
y=abs(diff(tpmgoodrna'))./unitdistancearray;
tpminputdistances=sum(sum(y'));
sumdistances=nan(1,numreps);
parfor j=1:numreps
    shuffledtpmarray=tpmgoodrna(:,randperm(numcells));
    y=abs(diff(shuffledtpmarray'))./unitdistancearray;
    sumdistances(j)=sum(sum(y')); 
end   
sortsumdistances=sort(sumdistances,'descend');
sequencedistances=[tpminputdistances,sortsumdistances];