Ejemplo n.º 1
0
void ArraySorter::QuickSort2(int *arr, int left, int right)
{
	if((left + 10) <= right)
	{
		int pivot = Median3(arr, left, right);
		int i = left, j = right-1;
		for(;;)
		{
			while(arr[++i] < pivot) 
			{
				// Nada mucho
			}
			while(pivot < arr[--j]) 
			{
				// Empty
			}
			
			if( i < j )
			{
				swap(arr, i, j);
			}
			else
			{
				break;
			}
		}
		swap(arr, i, right-1);
		QuickSort2(arr, left, i-1);
		QuickSort2(arr, i+1, right);
	}
	else //Run insertion sort
	{
		InsertionSort(arr, right+1, left, 0);
	}
}
Ejemplo n.º 2
0
void QuickSort2(int a[], int l, int r)
{
    int q;
 //   if(l < r)
    {
        q = Partition(a, l,r);

		//randomize quick sort: 
	//	q = RandomizeRartition(a, l, r);

         QuickSort2(a, l, q- 1);
         QuickSort2(a,  q, r);
         
    }
}
Ejemplo n.º 3
0
EXPORT BOOL WINAPI sortval( HSPEXINFO *hei, int p2, int p3, int p4 )
{
	//
	//		sortval val, order (type$202)
	//
	int a,i;
	int *p;
	double *dp;
	PVal *p1;
	APTR ap;
	int order;

	ap = hei->HspFunc_prm_getva( &p1 );		// パラメータ1:変数
	order = hei->HspFunc_prm_getdi( 0 );	// パラメータ2:数値

	i=p1->len[1];
	if (i<=0) return -1;
	switch(p1->flag) {
	case 3:						// double
		dp=(double *)p1->pt;
		DataIni( i );
		for(a=0;a<i;a++) {
			dtmp[a].as.dkey=dp[a];
			dtmp[a].info=a;
		}
		BubbleSortDouble( dtmp, i, order );
		for(a=0;a<i;a++) {
			//dp[a]=dtmp[a].as.dkey;
			hei->HspFunc_prm_setva( p1, a, HSPVAR_FLAG_DOUBLE, &(dtmp[a].as.dkey) );	// 変数に値を代入
		}
		break;
	case 4:						// int
		p=(int *)p1->pt;
		DataIni( i );
		for(a=0;a<i;a++) {
			dtmp[a].as.ikey=p[a];
			dtmp[a].info=a;
		}
		QuickSort2( dtmp, i, order );
		for(a=0;a<i;a++) {
			p[a]=dtmp[a].as.ikey;
		}
		break;
	default:
		return -1;
	}

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char const *argv[])
{
	int i;
	int n = 0;

	int a[16] = { 11,10,9,8,7,6,5,4,3,2,1,0, 13, 15, 12, 14};
	for (i = 0; i < 16; ++i)
	{
		printf("%d\t", a[i]);
		/* code */
	}
	printf("\n");

	//insert sort:
//	InsertSort(a, 11);

	//merge sort:
//	MergeSort(a, 0, 11);


	//bubble sort:
//	BubbleSort(a, 13);

	//quick sort:
//	QuickSort(a, 0, 15);

	//quick sort 2:
	QuickSort2(a, 0, 15);

	//heap sort:
//	HeapSort(a, 16);

	for (i = 0; i < 16; ++i)
	{
		printf("%d\t", a[i]);
		/* code */
	}
	printf("\n");

	printf("exchange times: %d\n", n);

	return 0;
}
Ejemplo n.º 5
0
void Vector::Sort(Vector &freeRider) {
  QuickSort2(data, freeRider.data, dim, sizeof(double),
             COMPAREFUNC CompareDouble);
}
Ejemplo n.º 6
0
int main ( int argc, char* argv[] )
{

	FILE *fp1, *fp2, *fp3;

	char c = 'A', d='A';

	char * temp = new char [100];
	char * file = new char [100];

	int a, b, i, j, k, l, m;
	double x=0, y=0, z=0;

	int num_frag=1; // total number of fragment under study
	//int numind;
	int numsnp;

	file = argv[1];
	//file = "chrom4_FRI2.txt";
	fp1 = fopen( file, "r");

	fscanf( fp1, "%d", &numind );
	fscanf( fp1, "%d", &numsnp );
	//numind/=2;
	int ** hap = new int * [numind];
	int ** hapallele = new int * [numind];
	for ( i=0; i<numind; i++ )
	{
		hap[i]=new int [numsnp];
		hapallele[i]=new int [numsnp];
		for ( j=0; j<numsnp; j++ )
		{
			hap[i][j]=0;
			hapallele[i][j]=-99;
		}
	}
	int * snppos=new int [numsnp];
	double * pvalue=new double [numsnp];for ( j=0; j<numsnp; j++ ){pvalue[j]=-99;}
	double ** stat=new double * [numsnp];for ( j=0; j<numsnp; j++ ){stat[j]=new double [2];}
	double ** distance = new double * [numind];
	for ( i=0; i<numind; i++ ){distance[i]=new double [numind];for ( j=0; j<numind; j++ ){distance[i][j]=0;}}

	int ** cladeslist = new int * [numind];for ( i=0; i<numind; i++ ){cladeslist[i]=new int [numind];for ( j=0; j<numind; j++ ){cladeslist[i][j]=0;}}
	int * cladesusage = new int [numind];for ( i=0;i<numind;i++ ){cladesusage[i]=0;}
	int * numperclade = new int [numind];for ( i=0;i<numind;i++ ){numperclade[i]=0;}
	int * cladeacc = new int [numind];for ( i=0;i<numind;i++ ){cladeacc[i]=0;}
	int numclades=0;

	double * pheno = new double [numind];
	for ( i=0; i<numind; i++ )
	{
//		for ( k=0; k<2; k++)
//		{
			for ( j=0; j<numsnp; j++ )
			{
				fscanf( fp1, "%d",&l );
				hap[i][j]=l;
//				hap[i][j]+=l;
			}
//		}
		for ( j=0; j<numsnp; j++ )
		{
			if ( hap[i][j]>100 )
			{
				hap[i][j]=0;
			}
//			else
//			{
//				hap[i][j]++; // 0 : missing data
//			}
		}

	}
	for( i=0; i<numsnp; i++ )
	{
		fscanf( fp1, "%d", &snppos[i] );
	}
	for ( i=0; i<numind; i++ )
	{
		fscanf( fp1, "%lf", &pheno[i] );
	//	fscanf( fp1, "%lf", &x );
	}
	fclose( fp1 );

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	double ** score = new double * [numind];for ( i=0; i<numind; i++ ){score[i]=new double [3];}
	double * sumranks = new double [50];for ( i=0; i!=50; i++ ){sumranks[i]=0;}
	double tie=0;
//*
	for ( i=0; i<numind; i++ )
	{
		score[i][0]=(double)i;
		score[i][1]=(double)pheno[i];
		score[i][2]=(double)0;	
	}

	// sorts the array according to phenotype
	score = QuickSort2( score, 0, numind-1 );			
	// given sorted values, determines rank score.
	// if phenotypes are equal, average of rank scores are computed
	for ( i=0; i<numind; i++ )
	{
		if ( i==(numind-1) )
		{
			score[i][2]=(double)i+1;
			break;
		}
		if ( score[i][1]!=score[i+1][1] && i<numind )
		{
			score[i][2]=(double)i+1;
		}
		else
		{
			z=(double)i+1;
			a=i;
			m=1;
			while( score[i][1]==score[i+1][1] && i<=(numind-2) )
			{
				m++;
				i++;
				z+=(double)i+1;
				if ( i==(numind-1) )
				{
					break;
				}
			}
			z/=(double)m;
			if ( m>1 )
			{
				tie+=(pow(m,3)-m);
			}

			for ( k=0; k!=m; k++ )
			{
				score[a+k][2]=z;
			}
		}
	}

	sprintf( temp, "%s_phenranks.txt", file );	
	fp1=fopen( temp, "w" );
	for ( i=0; i<numind; i++ )
	{
		fprintf( fp1, "%.0f\t%f\t%f\n", score[i][0], score[i][1], score[i][2] );
	}
	fclose( fp1 );
//*/

	int numacc=i;
	int level=0;
	int index=0;
	double ave_dist1, ave_dist2;
	double R1, R2;
	double min_dist;
	int child1, child2;
	int num_taxa;
	int snp;

	for ( snp=0; snp<numsnp; snp++ )
	//for ( snp=6; snp<7; snp++ )
	{

		for ( i=0; i<numind; i++ ){for ( j=0; j<numind; j++ ){cladeslist[i][j]=0;}}
		for ( i=0;i<numind;i++ ){cladesusage[i]=0;}
		for ( i=0;i<numind;i++ ){numperclade[i]=0;}
		for ( i=0;i<numind;i++ ){cladeacc[i]=0;}
		int numclades=0;
		
		NJ ** data2; // entire dataset
		data2 = new NJ * [2];
		for ( i=0; i<2; i++ )
		{
			data2[i]=new NJ [numind+1-i];
		}
		
		for ( i=0; i<numind; i++ )
			for ( j=0; j<numind; j++ )			
				distance[i][j]=0;
					
		// computes a distance metric based on scoring scheme:
		// match : +1
		// mismatch : -2
		// aligned to missing data: 0
		// computes a distance metric based on haplotype sharing
		DM_share( hap, distance, snp, numind, numsnp, snppos );

		//sprintf( temp, "%s_clades_%d.txt", file, snppos[snp] );				
		//fp2=fopen( temp, "w" );
//*
		sprintf( temp, "%s_dist_%d.txt", file, snppos[snp] );
		fp3=fopen( temp, "w" );

		for ( i=0; i<numind; i++ )
		{
			fprintf( fp3, "%d ", i );
			for ( j=0; j<numind; j++ )
			{
				fprintf( fp3, "%.3f ", distance[i][j] );
			}
			fprintf( fp3, "\n" );
		}
		fclose( fp3 );
//*/
		// transfers information from distance matrix to class data2
		num_taxa=numind;
		for ( i=0; i<num_taxa; i++ )
		{
			for (j=0; j<num_taxa ; j++ )
			{
				if ( j==0 )
				{
					data2[0][i].accession[0]=i;
					data2[0][i].trace=i;
				}
				data2[0][i].distance[j]=distance[i][j];
				data2[0][i].child[0]=NULL;
				data2[0][i].child[1]=NULL;
				data2[0][i].delta[j]=0;
				data2[0][i].num_acc=1;
				data2[0][i].num_child=0;	
				data2[0][i].edge_c1=0;
				data2[0][i].edge_c2=0;

			}
		}

		level=0;
		// initialization step for neighbor joining
		// compute corrected distance for all pairs i and j
		// the corrected distance is the pairwise distance between i and j subtracted by 
		// Ri and Rj, where Ri and R j are the averaged distance to all other accessions for 
		// i and j, respectively
		min_dist=1e8;
		for ( i=0; i<num_taxa; i++ )
		{
			for ( j=(i+1); j<num_taxa; j++ )
			{
				ave_dist1=0;
				ave_dist2=0;
				// averaged distance of i to all other non j nodes
				for( a=0; a<num_taxa; a++ )
				{
					if ( a!=i && a!=j )
					{
						ave_dist1+=data2[0][i].distance[a];
					}
				}
				ave_dist1/=(num_taxa-2);
				// averaged distance of j to all other non i nodes
				for( a=0; a<num_taxa; a++ )
				{
					if ( a!=i && a!=j )
					{
						ave_dist2+=data2[0][j].distance[a];
					}
				}
				ave_dist2/=(num_taxa-2);

				// computes the corrected distance
				data2[0][i].delta[j]=data2[0][i].distance[j]-ave_dist1-ave_dist2;
				
				//finds the pair that gives the minimum corrected distance
				if ( data2[0][i].delta[j] <= min_dist )
				{
					min_dist = data2[0][i].delta[j];
					child1=i;
					child2=j;
					R1=ave_dist1;
					R2=ave_dist2;
				}
			}
		}

		// initial output of each accession
		// repeat until number of accession is 2
		while ( num_taxa>2 )
		{	
			// reference index for first position of the pair that gave the minimum corrected distance
			index=child1;
			if ( child1>child2 )
			{
				index=child2;
				j=child1;
			}
			
			// data2 merge for next level, i.e. takes all accessions and merges the
			// minimum corrected distance pair
			for ( i=0; i<num_taxa; i++ )
			{
				// when a mergeable cell is found, 
				if ( child1==i || child2==i )
				{
					if ( i==index )
					{
						// first child, i.e. mergeable cell
						// transfer address to parent cell
						data2[1][index].child[0] = &data2[0][i];
						data2[1][index].num_child=2;
						// computes edge of parent cell to child 1
						data2[1][index].edge_c1=(data2[0][i].distance[j]+R1-R2)/2;
						// keeps track of an accession as it moves along the tree
						data2[1][i].trace=i;
						// stores information of accessions being merged
						
						for ( a=0; a<data2[0][index].num_acc; a++ )
						{
							data2[1][index].accession[a]=data2[0][i].accession[a];
						}
						data2[1][index].num_acc=data2[0][i].num_acc;
					}
					else
					{
						// found second child, i.e. mergeable cell
						data2[1][index].child[1] = &data2[0][i];
						data2[1][index].edge_c2=( data2[0][i].distance[i]-data2[1][index].edge_c1 );
					
						// stores information of accessions being merged
						for ( b=0; b<data2[0][i].num_acc; b++ )
						{
							data2[1][index].accession[data2[0][index].num_acc+b]=data2[0][i].accession[b];

						}
						data2[1][index].num_acc+=data2[0][i].num_acc;

						if ( data2[1][index].num_acc>=5 && data2[1][index].num_acc<numind-5 )
						{							
							numperclade[numclades]=data2[1][index].num_acc;
							for ( a=0; a<data2[1][index].num_acc; a++ )
							{
								//fprintf( fp2, "%d ", data2[1][index].accession[a] );
								cladeslist[numclades][a]=data2[1][index].accession[a];
							}
							//fprintf( fp2, "\n" );
							numclades++;
						}

						// shift adjustment to account for removed later child, just passing address of previous node
						for ( b=i; b<num_taxa; b++ )
						{
							data2[1][b].child[0] = &data2[0][b+1];
							data2[1][b].num_child=1;
							data2[1][b].trace=b;
							for ( a=0; a<data2[0][b+1].num_acc; a++ )
							{
								data2[1][b].accession[a]=data2[0][b+1].accession[a];
							}
							data2[1][b].num_acc=data2[0][b+1].num_acc;
						}						
						i=num_taxa;	
						
					}				
				}
				// when no mergeable child is found, just pass address of previous node
				else
				{
					data2[1][i].child[0] = &data2[0][i];
					data2[1][i].num_child=1;
					data2[1][i].trace=i;
					for ( a=0; a<data2[0][i].num_acc; a++ )
					{
						data2[1][i].accession[a]=data2[0][i].accession[a];
					}
					data2[1][i].num_acc=data2[0][i].num_acc;
				}
			}


			num_taxa--;
			// generate new distance matrix 
			for ( i=0; i<num_taxa; i++ )
			{
				for (j=i; j<num_taxa ; j++ )
				{
					if ( i==j )
					{
						data2[1][i].distance[j]=0;
					}
					// when either accession i or j matches minimum pair k and l that formed index,
					// then must recalculate distance between i or j to index adjusting for the merge
					// in other words, to compute the distance between i=index and j
					// add the distance(j, k) and distance(j, l)
					// subtract with distance(k, l) and divide by 2
					else if ( i==index && j!=index )
					{
						data2[1][i].distance[j] = 
							( 
							data2[1][i].child[0]->distance[data2[1][j].child[0]->trace] +
							data2[1][i].child[1]->distance[data2[1][j].child[0]->trace] -
							data2[1][i].child[0]->distance[data2[1][i].child[1]->trace] 
							)/2.0;
					}
					else if ( i!=index && j==index )
					{
						data2[1][i].distance[j] = 
							( 
							data2[1][j].child[0]->distance[data2[1][i].child[0]->trace] +
							data2[1][j].child[1]->distance[data2[1][i].child[0]->trace] -
							data2[1][j].child[0]->distance[data2[1][j].child[1]->trace] 
							)/2.0;
					}
					// when neither of accesion i or j are from the minimum pair,
					// then the distance remains unchanged between i and j
					else if ( i!=index && j!=index )
					{
						data2[1][i].distance[j] = 
						data2[1][i].child[0]->distance[data2[1][j].child[0]->trace];						
					}
					data2[1][j].distance[i]=data2[1][i].distance[j];
				}
			}

			for ( i=0; i<num_taxa; i++ )
			{
				data2[0][i]=data2[1][i];
			}
		
			//finds the minimum pair for new distances computed after merging
			min_dist=1e8;
			//finds the minimum pair for new distances computed after merging
			for ( i=0; i<num_taxa; i++ ){data2[level][i].sumdist=0;for ( j=0; j<num_taxa; j++ ){data2[level][i].sumdist+=data2[level][i].distance[j];}}
			for ( i=0; i<num_taxa; i++ )
			{
				for ( j=(i+1); j<num_taxa; j++ )
				{
					// averaged distance of i to all other non j nodes
					ave_dist1=data2[level][i].sumdist;
					ave_dist1-=data2[level][i].distance[i];
					ave_dist1-=data2[level][i].distance[j];
					ave_dist1/=(num_taxa-2);

					// averaged distance of j to all other non i nodes
					ave_dist2=data2[level][j].sumdist;
					ave_dist2-=data2[level][j].distance[i];
					ave_dist2-=data2[level][j].distance[j];
					ave_dist2/=(num_taxa-2);

					data2[level][i].delta[j]=data2[level][i].distance[j]-ave_dist1-ave_dist2;

					if ( data2[level][i].delta[j] <= min_dist )
					{
						min_dist = data2[level][i].delta[j];
						child1=i;
						child2=j;
						R1=ave_dist1;
						R2=ave_dist2;
					}
				}
			}
			
		}
		// fclose( fp2 );

		pvalue[snp] = test( hapallele, snp, pheno, numind, 
			cladeslist, cladesusage, numperclade, numclades, cladeacc, 
			score, sumranks, tie, stat );
	
		printf( "%d %f\n", snp, -log(pvalue[snp]) );			
	
		sprintf( temp, "%s_results.txt", file);
		fp3=fopen( temp, "a" );
		fprintf( fp3, "%d %.20f %f %.10f %.0f\n", snp, pvalue[snp], -log(pvalue[snp]), stat[snp][0], stat[snp][1] );
		fclose( fp3 );

		for ( i=0; i<2; i++ )
		{
			delete [] data2[i];
		}
		delete []data2;

	}

	sprintf( temp, "%s_results.txt", file);
	fp3=fopen( temp, "w" );
	for ( i=0; i<numsnp; i++ )
	{
		fprintf( fp3, "%d %.20f %f %.10f %.0f\n", i, pvalue[i], -log(pvalue[i]), stat[i][0], stat[i][1] );
	}
	fclose( fp3 );

	sprintf( temp, "%s_hap_results.txt", file );
	fp3=fopen( temp, "w" );
	for ( i=0; i<numind; i++ )
	{
		fprintf( fp3, "%d ", i );
		for ( j=0; j<numsnp; j++ )
		{
			fprintf( fp3, "%d ", hapallele[i][j] );
		}
		fprintf( fp3, "\n" );

	}
	fclose( fp3 );

	sprintf( temp, "%s_haps.txt", file);
	fp3=fopen( temp, "w" );
	for ( i=0; i<numind; i++ )
	{
		fprintf( fp3, "%d ", i );
		for ( j=0; j<numsnp; j++ )
		{
			fprintf( fp3, "%d ", hap[i][j] );
		}
		fprintf( fp3, "%.0f ", pheno[i] );
		fprintf( fp3, "\n" );

	}
	fclose( fp3 );

	

 	return 0;
}
Ejemplo n.º 7
0
void FloatVector::Sort(FloatVector & freeRider)
   {
   QuickSort2(data, freeRider.data, dim, sizeof(float),
              COMPAREFUNC CompareFloat);
   }
Ejemplo n.º 8
0
void ArraySorter::QuickSort(int* arr, int n)
{
	// You must implement this function
	QuickSort2(arr, 0, n-1);
}