#include <stdio.h>
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <ctype.h>



	int
diff_within_between1(nsam,ns,list,dij)
	int nsam, ns, **dij  ;
	int **list;
{
	int i, j ;
	double diff();
	
	for(i=0;i<(nsam-1);i++)
		for(j=i+1;j<nsam;j++){
			 dij[i][j]=  diff(ns,list[i],list[j])  ;
			 dij[j][i]=dij[i][j];
			 }
}



main( int argc, char * argv[])
{
	int nsam, i, ns,   count ;
	int **list, **cmatrix();
	int nval ;
	int  **dij ;
	FILE *pf, *fopen(), *pfout ;
	int  k,  j, numsites, diff_within_between1() ;
		
       if( argc < 2 ) {
            printf("usage:seqtomatrix number_of_sequences number_of_sites\n");
            exit(1) ;
            }
	nsam = atoi( argv[1]) ;
	numsites = atoi( argv[2] );

	nval = (nsam*(nsam-1))/2 ;
	dij = (int **)malloc((size_t)nsam*sizeof( int * ) ) ;
	if( (dij = (int **)malloc((size_t)nsam*sizeof( int * ) ) ) == NULL)
		perror( "malloc error2\n") ;
	for(i=0;i<nsam;i++){
		if( (dij[i] = (int *)malloc( (size_t)nsam*sizeof(int))) == NULL)
			perror( "malloc error3\n") ;
	   }

 	if( (list = cmatrix(nsam,numsites+1)) == NULL)
			perror( "malloc error8\n") ;
	pf = stdin ;
	pfout = stdout ;

	ns = get_gametes(nsam, numsites, list,pf );

	diff_within_between1(nsam,ns,list,dij);
	fprintf(pfout,"Number of sequences %d\n",nsam);
	fprintf(pfout,"OTUs ");
	for(i=0; i<nsam; i++) fprintf(pfout,"\t%d",i+1);
	fprintf(pfout,"\n");
	for(i=0;i<nsam-1; i++){
		fprintf(pfout," %d  ",i+1);
		for( j=0; j<=i; j++) fprintf(pfout,"\t");
		for(j=i+1;j<nsam; j++) fprintf(pfout,"\t%d",dij[i][j]);
		fprintf(pfout,"\n");
		}

}

	double
diff(ns,gam1, gam2)
	int *gam1, *gam2 ;
{

	int  i;
	double count;
	
	count = 0. ;
	for( i=0; i<ns ; i++){
		 if( (gam1[i]>=0 && gam2[i]>=0) && (gam1[i] != gam2[i]) ) count += 1. ;
		 }
	return( count );
}
	
	

/* allocates space for gametes (character strings) */
	int **
cmatrix(nsam,len)
	int nsam, len;
{
	int i;
	int **m;

	if( ! ( m = (int **) malloc( (unsigned) nsam*sizeof( int* ) ) ) )
	   perror("alloc error in cmatrix") ;
	for( i=0; i<nsam; i++) {
		if( ! ( m[i] = (int *) malloc( (size_t) len*sizeof( int ) )))
			perror("alloc error in cmatric. 2");
		}
	return( m );
}

/* Data should consist of nsam lines in a file, where each line is of the form:
  id# 1 1 0 1 1 0 1 , where id# is any word (it will be ignored), followed by
  integers representing the alleles at each of the numsites sites, each integer 
  being separated by whitespace.  */

	int
get_gametes(nsam, ns, list,pf )
	int nsam,  ns;
	int **list;
	FILE *pf;
{
	int  i, j, k ;
	char gamname[35] ;

  for(j=0;j<nsam;j++){
	if(  fscanf(pf," %s",gamname) == 0 ) {perror( " read error 1"); exit(1) ;}
	for(i=0;i<ns;i++){
		if( fscanf(pf," %d",&(list[j][i]) ) == 0 ) { perror( " read error 2"); exit(1)  ;}
		}
	list[j][ns] = 0 ;
	}
	return( ns );
}



