/* This program carries out the "nearest neighbor test" described in Hudson, R.R. 2000 "A new statistic for detecting genetic differentiation" Genetics (in press). Data consists of a matrix of pairwise differences between sampled sequences, as described in readme. An example data file is sample.dat . The program can usually be compiled by gcc -o snn snn.c . And then run by snn y 1000 20 27 sample.out . On the command line, 20 is the sample size from locality one, and 27 is the sample size from locality two. 1000 is the number of permutations to carry out. y indicates that the input data matrix should be copied to the output. */ #include #include #include #include #include main( int argc, char * argv[] ) { int nperms, nlocs, nsam ; double **dij, pval, snno, permt(), **tdij ; int i, loc, wf, *ni, tni[3], loc1, loc2, j, sti, stj, endi, endj ; if( argc < 4 ) { printf( "usage: snn y/n(print data?) n_permutations n1 n2 \n"); exit(1) ; } nperms = atoi( argv[2] ) ; nlocs = 2 ; if( (ni = (int *)malloc( (size_t) (nlocs+1)*sizeof(int) )) == NULL) perror( "malloc error1\n") ; nsam = 0 ; for( i=0; i 0 ; i--){ j = ran1()*(i + 1) ; temp = ran_vect[j]; ran_vect[j] = ran_vect[i] ; ran_vect[i] = temp ; } } double ran1() { double drand48(); return( (double)drand48() ); } double snnf(int nsam, int npop, int *config, int **dij, int *ran_ind) { int i, ind, mall, nmall, nmwithin , pop, indstart ; double p ; p = 0. ; for(ind= pop=0; pop