Exemplo n.º 1
0
void NodeSetTest::test_clear()
{
  NodeSet set;
  set.set_corner_node(1);
  set.clear();
  CPPUNIT_ASSERT( !set.get_bits() );
  set.set_mid_edge_node(2);
  set.clear();
  CPPUNIT_ASSERT( !set.get_bits() );
  set.set_mid_region_node();
  set.clear();
  CPPUNIT_ASSERT( !set.get_bits() );
}
Exemplo n.º 2
0
void NodeSetTest::test_have_any()
{
  NodeSet set;
  set.clear();
  
  CPPUNIT_ASSERT( !set.have_any_corner_node() );
  set.set_corner_node(0);
  CPPUNIT_ASSERT( set.have_any_corner_node() );
  set.clear_corner_node(0);
  CPPUNIT_ASSERT( !set.have_any_corner_node() );
  set.set_corner_node( NodeSet::NUM_CORNER_BITS-1 );
  CPPUNIT_ASSERT( set.have_any_corner_node() );
  CPPUNIT_ASSERT( !set.have_any_mid_node() );

  CPPUNIT_ASSERT( !set.have_any_mid_edge_node() );
  set.set_mid_edge_node(0);
  CPPUNIT_ASSERT( set.have_any_mid_edge_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear_mid_edge_node(0);
  CPPUNIT_ASSERT( !set.have_any_mid_edge_node() );
  CPPUNIT_ASSERT( !set.have_any_mid_node() );
  set.set_mid_edge_node( NodeSet::NUM_EDGE_BITS-1 );
  CPPUNIT_ASSERT( set.have_any_mid_edge_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear();

  CPPUNIT_ASSERT( !set.have_any_mid_face_node() );
  set.set_mid_face_node(0);
  CPPUNIT_ASSERT( set.have_any_mid_face_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear_mid_face_node(0);
  CPPUNIT_ASSERT( !set.have_any_mid_face_node() );
  CPPUNIT_ASSERT( !set.have_any_mid_node() );
  set.set_mid_face_node( NodeSet::NUM_FACE_BITS-1 );
  CPPUNIT_ASSERT( set.have_any_mid_face_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear();

  CPPUNIT_ASSERT( !set.have_any_mid_region_node() );
  set.set_mid_region_node(0);
  CPPUNIT_ASSERT( set.have_any_mid_region_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear_mid_region_node(0);
  CPPUNIT_ASSERT( !set.have_any_mid_region_node() );
  CPPUNIT_ASSERT( !set.have_any_mid_node() );
  set.set_mid_region_node( NodeSet::NUM_REGION_BITS-1 );
  CPPUNIT_ASSERT( set.have_any_mid_region_node() );
  CPPUNIT_ASSERT( set.have_any_mid_node() );
  set.clear();
}
Exemplo n.º 3
0
void NodeSetTest::test_num_before()
{
  const EntityTopology type = HEXAHEDRON;
  NodeSet set;
  set.clear();
  set.set_mid_face_node( 2 );
  CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_mid_face( 2 ) );
  CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_mid_face( 3 ) );
  set.set_corner_node( 0 );
  CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_mid_face( 2 ) );
  CPPUNIT_ASSERT_EQUAL( 2u, set.num_before_mid_face( 3 ) );
  CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_corner( 0 ) );
  CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_corner( 1 ) );
  set.set_all_corner_nodes( type );
  CPPUNIT_ASSERT_EQUAL( TopologyInfo::corners(type), set.num_before_mid_edge(0) );
  CPPUNIT_ASSERT_EQUAL( TopologyInfo::corners(type)-1, set.num_before_corner(TopologyInfo::corners(type)-1) );
  CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_corner(0) );
  CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_corner(1) );
  CPPUNIT_ASSERT_EQUAL( 2u, set.num_before_corner(2) );
  CPPUNIT_ASSERT_EQUAL( 3u, set.num_before_corner(3) );
  CPPUNIT_ASSERT_EQUAL( 4u, set.num_before_corner(4) );
  
  const unsigned total_nodes = 27;
  set.set_all_nodes( type );
  CPPUNIT_ASSERT_EQUAL( total_nodes - 1, set.num_before_mid_region( 0 ) );
  set.clear_mid_edge_node( 0 );
  CPPUNIT_ASSERT_EQUAL( total_nodes - 2, set.num_before_mid_region( 0 ) );
  set.clear_mid_edge_node( 1 );
  CPPUNIT_ASSERT_EQUAL( total_nodes - 3, set.num_before_mid_region( 0 ) );
}
Exemplo n.º 4
0
void TriLagrangeShapeTest::test_deriv_edges()
{
  NodeSet ns;
  
  ns.clear();
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.clear();
  ns.set_mid_edge_node(1);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.clear();
  ns.set_mid_edge_node(2);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.set_mid_edge_node(1);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );

  ns.clear_mid_edge_node(0);
  test_edge_derivs( 0, ns );
  test_edge_derivs( 1, ns );
  test_edge_derivs( 2, ns );
}
Exemplo n.º 5
0
void TriLagrangeShapeTest::test_coeff_edges()
{
  NodeSet ns;
  
  ns.clear();
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.clear();
  ns.set_mid_edge_node(1);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.clear();
  ns.set_mid_edge_node(2);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.set_mid_edge_node(0);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.set_mid_edge_node(1);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );

  ns.clear_mid_edge_node(0);
  test_edge_coeff( 0, ns );
  test_edge_coeff( 1, ns );
  test_edge_coeff( 2, ns );
}
Exemplo n.º 6
0
NodeSet TriLagrangeShape::sample_points( NodeSet ns ) const
{
  if (ns.have_any_mid_node()) {
    ns.set_all_corner_nodes(TRIANGLE);
  }
  else {
    ns.clear();
    ns.set_mid_face_node(0);
  }
  return ns;
}
Exemplo n.º 7
0
void State::divideTrees(const State& a_other, NodePairSet& a_commonRoots, NodeSet& a_myUniqueRoots, NodeSet& a_otherUniqueRoots)
{
	NodeSet otherRootSet = a_other.getRootSet(); // Copy the entire set
	a_commonRoots.clear();
	a_myUniqueRoots.clear();
	a_otherUniqueRoots.clear();

	for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++)
	{
		bool isUnique = true;
		string rootName;
		if (!((*iter)->getUniqueName(rootName)))
		{
			error("State::divideTrees - Multiple roots (in this state) with the same name " + rootName + " exist! " + string(*this), true);
		}

		for (NodeSetIter otherIter = otherRootSet.begin(); otherIter != otherRootSet.end(); otherIter++)
		{
			string otherRootName;
			if (!((*otherIter)->getUniqueName(otherRootName)))
			{
				error("State::divideTrees - Multiple roots (in joined state) with the same name " + otherRootName + " exist! " + string(a_other), true);
			}

			if (rootName == otherRootName)
			{
				a_commonRoots.insert(make_pair((*iter), (*otherIter)));
				otherRootSet.erase(otherIter);
				isUnique = false;
				break;
			}
		}

		if (isUnique)
		{
			a_myUniqueRoots.insert(*iter);
		}
	}
	a_otherUniqueRoots = otherRootSet;
}
Exemplo n.º 8
0
void NodeSetTest::test_num_nodes()
{
  NodeSet set;
  set.clear();
  set.set_corner_node(1);
  CPPUNIT_ASSERT_EQUAL( 1u, set.num_nodes() );
  set.set_corner_node(3);
  CPPUNIT_ASSERT_EQUAL( 2u, set.num_nodes() );
  set.set_mid_region_node(0);
  CPPUNIT_ASSERT_EQUAL( 3u, set.num_nodes() );
  set.set_mid_edge_node(1);
  set.set_mid_edge_node(2);
  CPPUNIT_ASSERT_EQUAL( 5u, set.num_nodes() );
  set.set_all_nodes(HEXAHEDRON);
  CPPUNIT_ASSERT_EQUAL( 27u, set.num_nodes() );
  set.clear();
  set.set_all_nodes(TETRAHEDRON);
  CPPUNIT_ASSERT_EQUAL( 15u, set.num_nodes() );
  set.clear();
  set.set_all_nodes(TRIANGLE);
  CPPUNIT_ASSERT_EQUAL( 7u, set.num_nodes() );
  set.clear();
  set.set_all_nodes(QUADRILATERAL);
  CPPUNIT_ASSERT_EQUAL( 9u, set.num_nodes() );
  set.clear();
  set.set_all_nodes(PYRAMID);
  CPPUNIT_ASSERT_EQUAL( 19u, set.num_nodes() );
  set.clear();
  set.set_all_nodes(PRISM);
  CPPUNIT_ASSERT_EQUAL( 21u, set.num_nodes() );
}
Exemplo n.º 9
0
void NodeSetTest::test_set_all()
{
  const EntityTopology types[] = { TRIANGLE, QUADRILATERAL,
                                   TETRAHEDRON, HEXAHEDRON,
                                   PRISM, PYRAMID };
  const int num_types = sizeof(types)/sizeof(types[0]);

  for (int i = 0; i < num_types; ++i) {
    NodeSet set;
    set.set_all_corner_nodes( types[i] );
    check_range_set( types[i], set, 0 );
    set.clear();
    set.set_all_mid_edge_nodes( types[i] );
    check_range_set( types[i], set, 1 );
    set.clear();
    set.set_all_mid_face_nodes( types[i] );
    check_range_set( types[i], set, 2 );
    set.clear();
    set.set_all_mid_region_nodes( types[i] );
    check_range_set( types[i], set, 3 );
  }
}
Exemplo n.º 10
0
void TriLagrangeShapeTest::test_deriv_center()
{
  NodeSet ns;
  
  ns.clear();
  test_mid_derivs( ns );
  ns.set_mid_edge_node(0);
  test_mid_derivs( ns );
  ns.clear();
  ns.set_mid_edge_node(1);
  test_mid_derivs( ns );
  ns.set_mid_edge_node(0);
  test_mid_derivs( ns );
  ns.clear();
  ns.set_mid_edge_node(2);
  test_mid_derivs( ns );
  ns.set_mid_edge_node(0);
  test_mid_derivs( ns );
  ns.set_mid_edge_node(1);
  test_mid_derivs( ns );
  ns.clear_mid_edge_node(0);
  test_mid_derivs( ns );
}
Exemplo n.º 11
0
void NodeSetTest::test_set_node()
{
  NodeSet set;
  for (unsigned i = 0; i < NodeSet::NUM_CORNER_BITS; ++i) {
    set.set_corner_node( i );
    CPPUNIT_ASSERT_EQUAL( 1u << (NodeSet::CORNER_OFFSET + i), set.get_bits() );
    set.clear();
  }
  for (unsigned i = 0; i < NodeSet::NUM_EDGE_BITS; ++i) {
    set.set_mid_edge_node( i );
    CPPUNIT_ASSERT_EQUAL( 1u << (NodeSet::EDGE_OFFSET + i), set.get_bits() );
    set.clear();
  }
  for (unsigned i = 0; i < NodeSet::NUM_FACE_BITS; ++i) {
    set.set_mid_face_node( i );
    CPPUNIT_ASSERT_EQUAL( 1u << (NodeSet::FACE_OFFSET + i), set.get_bits() );
    set.clear();
  }
  for (unsigned i = 0; i < NodeSet::NUM_REGION_BITS; ++i) {
    set.set_mid_region_node( i );
    CPPUNIT_ASSERT_EQUAL( 1u << (NodeSet::REGION_OFFSET + i), set.get_bits() );
    set.clear();
  }
}
Exemplo n.º 12
0
int main(int argc, char* argv[])
{   	
	struct timeval startTime, endTime;
	
	gettimeofday(&startTime, NULL);
	
	string inFileName = "", cutoffFileName = "";
		
	printf("\n----------------------------------------------------------------------\n");
	printf("                  DENDROGRAM CUTTING GENETIC DISTANCES                  \n");

	int i, numReads;

	getOptions(argc, argv, inFileName, cutoffFileName, numReads);
	
	FILE * cutoffFile = NULL;
	vector<float> cutoffs;
	float cutoff;
	cutoffFile = fopen(cutoffFileName.c_str(),"r");
	while(!feof(cutoffFile)) {
		fscanf(cutoffFile, "%f\n", &cutoff);
		cutoffs.push_back(cutoff);
		cout << cutoff << endl;
	}
	fclose(cutoffFile);	

	leaves=(TreeNode**)malloc(sizeof(TreeNode*)*(2*numReads+1));

	if(leaves==0)
	{   
		cout<<"Error: Not enough memory" << endl;
		exit(-1);
	}
	
	for(i = 0; i < numReads; ++i) 
		leaves[i] = new TreeNode(i);	
	
	for(i = numReads+1; i < 2*numReads+1; ++i) 
		leaves[i] = 0;
	
	int idX, idY;
	float dist;		
	string mergeFileName=inFileName;
	mergeFileName.append("_Merge");	
	
	FILE * mergeFile;
	mergeFile = fopen(mergeFileName.c_str(), "r");
	
	newId = numReads;	
	while ( fscanf(mergeFile, "%d %d %f", &idX, &idY, &dist) == 3 ) 
		merge(leaves[idX-1], leaves[idY-1], dist);	
			
	fclose(mergeFile);
	cout << "DONE!" << endl;
	cout << "current node: " << newId << endl;	

	// get root nodes and orphan nodes
	NodeSet roots;
	IDList orphanNodes;	
	TreeNode *aLeaf = 0;
     	
	for(i=0; i< numReads; ++i)
	{   
		aLeaf = leaves[i];
		if(aLeaf->parent==0)  // find nodes with no parent
			orphanNodes.push_back(i); 				
		else
			roots.insert(aLeaf->topParent); 										
	}

 	// print output to files
 	
	string clusterListName, clusterName;

	clusterListName=inFileName;
	clusterListName.append(".Cluster_List");
	clusterName=inFileName;
	clusterName.append(".Cluster");

	printClusters(roots, orphanNodes, clusterListName, clusterName, cutoffs);		
	
	// clear memory
	emptyTree(roots);	
	roots.clear();	
	orphanNodes.clear();	
	free(leaves);
	
	gettimeofday(&endTime, NULL);	
	long elapsedTime = (endTime.tv_sec - startTime.tv_sec) * 1000u + (endTime.tv_usec - startTime.tv_usec) / 1.e3 + 0.5;

	printf("Time taken: %.3f s\n", elapsedTime/1.e3);
	printf("\n----------------------------------------------------------------------\n");
	return 0;
}
Exemplo n.º 13
0
int main(int argc, char* argv[])
{ 
	struct rlimit r;
	getrlimit(RLIMIT_NOFILE, &r);
	cout << "current rlimit: " << r.rlim_cur << endl;
	r.rlim_cur = 2048;
	setrlimit(RLIMIT_NOFILE, &r);
	cout << "change rlimit to: " << r.rlim_cur << endl;
	  
	struct timeval startTime, endTime;
	
	gettimeofday(&startTime, NULL);
	
	int numFiles=1;
	vector<string> pairNameVector, distNameVector;

	FILE **distFileList, **pairFileList;
	unsigned int ** inPairArray;
	float ** inDistArray;

	string inFileName = "";
		
	printf("\n----------------------------------------------------------------------\n");
	printf("                  COMPLETE CLUSTERING GENETIC DISTANCES                  \n");


	float stepSize = 0.01f, endLevel = 0.10f;	
	int i;
	int numReads=0;
		
	getOptions(argc, argv, inFileName, numReads, numFiles, endLevel, stepSize);
	getDistNameList(inFileName, pairNameVector, distNameVector, numFiles);

	if(pairNameVector.size()==0)
	{   
		cout<<"Error: No distance file loaded."<<endl;
		exit(-1);
	}
	
	int fileId;
	
	pairFileList=(FILE**)malloc(sizeof(FILE*)*pairNameVector.size());
	distFileList=(FILE**)malloc(sizeof(FILE*)*distNameVector.size());
	
	if(distFileList==NULL || pairFileList==NULL)
	{   
		cout<<"Error: Not enough memory" << endl;
		exit(-1);
	}

	for(fileId = 0; fileId < numFiles; ++fileId)
	{
		pairFileList[fileId]=fopen(pairNameVector[fileId].c_str(),"rb");
		if(pairFileList[fileId]==NULL)
		{   
			cout<<"Error: Cannot open file" << pairNameVector[fileId].c_str() << endl;
			exit(-1);
		}
	}
	
	for(fileId = 0; fileId < numFiles; ++fileId)
	{
		distFileList[fileId]=fopen(distNameVector[fileId].c_str(),"rb");
		if(distFileList[fileId]==NULL)
		{   
			cout<<"Error: Cannot open file" << distNameVector[fileId].c_str() << endl;
			exit(-1);
		}
	}

	if(numFiles!=distNameVector.size() || numFiles <= 0)
	{   
		cout<<"Error: invalid number of files!EXIT..."<<endl;
		exit(-1);
	}
	cout<<"Use "<<numFiles<<" distance file(s)."<<endl;
	
	unsigned long long totalNumPairs = 0;

	multimap<float, DistPair> nodeMap;
	multimap<float, DistPair>::iterator iter;
	unsigned int idX, idY;
	float dist;
	inPairArray = (unsigned int **) malloc(sizeof(unsigned int*) * numFiles);
	inDistArray = (float **) malloc(sizeof(float*) * numFiles);
	int * indices = (int*) malloc(sizeof(int) * numFiles);
	int * readSizes = (int*) malloc(sizeof(int) * numFiles);
	bool * EOFTags = (bool*) malloc(sizeof(bool) * numFiles);
	bool suc;

	for(fileId=0; fileId<numFiles; fileId++)
	{   
		// initialize
		inPairArray[fileId] = (unsigned int*) malloc(sizeof(unsigned int) * BUF_SIZE * 2);
		inDistArray[fileId] = (float*) malloc(sizeof(float) * BUF_SIZE);
		indices[fileId] = 0;
		readSizes[fileId] = 0;
		EOFTags[fileId] = false;				

		// add the first pair of each file to the nodeMap
		suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);				
	
		if (suc)
			nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));
	}		
				

	LinkMap::iterator mapIter;	
						

	vActiveNodes.resize(2*numReads+1);

	leaves=(TreeNode**)malloc(sizeof(TreeNode*)*numReads);

	if(leaves==0)
	{   
		cout<<"Error: Not enough memory" << endl;
		exit(-1);
	}
	
	for(i = 0; i < numReads; ++i) {
		vActiveNodes[i] = leaves[i] = new TreeNode(i);	
	}
	
	for(i = numReads+1; i < 2*numReads+1; ++i) {
		vActiveNodes[i] = 0;
	}
	
	newId = numReads;	
	
	cout << "numReads: " << numReads << "\tmaxNumEdges: " << MAX_NUM_EDGES << endl;
	cout << "endLevel: " << endLevel << endl;
			
	while(totalNumEdges < MAX_NUM_EDGES && !nodeMap.empty())
	{   
		// get the first item in the nodeMap
		iter = nodeMap.begin();
		fileId = iter->second.fileId;        

		// write to output
		idX = iter->second.idX;
		idY =  iter->second.idY;
		dist = iter->first;
		
		absorb(idX, idY, dist);	
		
		// remove the current item from the nodeMap
		nodeMap.erase(iter);
			
		suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);				

		if (suc) 
			nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));	
	}
			
	while(!nodeMap.empty())
	{				
		// get the first item in the nodeMap
		iter = nodeMap.begin();
		fileId = iter->second.fileId;        

		// write to output
		idX = iter->second.idX;
		idY =  iter->second.idY;
		dist = iter->first;

		lookAhead(idX, idY, dist);	
		// remove the current item from the nodeMap
		nodeMap.erase(iter);
			
		suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);				

		if (suc) 
			nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));	
	}
	
	cout << "DONE!" << endl;
	cout << "current node: " << newId << "\tnum unlinked: " << totalUnlinked << endl;	

	// get root nodes and orphan nodes
	NodeSet roots;
	IDList orphanNodes;	
	TreeNode *aLeaf = 0;
     	
	for(i=0; i< numReads; ++i)
	{   
		aLeaf = leaves[i];
		if(aLeaf->parent==0)  // find nodes with no parent
			orphanNodes.push_back(i); 				
		else
			roots.insert(aLeaf->topParent); 										
	}

 	// print output to files
 	
	string clusterListName, clusterName;

	clusterListName=inFileName;
	clusterListName.append(".Cluster_List");
	clusterName=inFileName;
	clusterName.append(".Cluster");

	printClusters(roots, orphanNodes, clusterListName, clusterName, stepSize, endLevel);			
	
	// clear memory
	emptyTree(roots);	
	roots.clear();	
	orphanNodes.clear();	
	free(leaves);
	vActiveNodes.clear();
	
	// clean up
	for(fileId=0; fileId<numFiles; ++fileId) {
		free(inDistArray[fileId]);
		free(inPairArray[fileId]);
	}
	free(inDistArray);
	free(inPairArray);
	free(indices);
	free(readSizes);
	free(EOFTags);
	free(pairFileList);
	free(distFileList);	
	
	gettimeofday(&endTime, NULL);	
	long elapsedTime = (endTime.tv_sec - startTime.tv_sec) * 1000u + (endTime.tv_usec - startTime.tv_usec) / 1.e3 + 0.5;
	
	printf("totalNumPairs: %llu\n", totalNumPairs);
	printf("Time taken: %.3f s\n", elapsedTime/1.e3);
	printf("\n----------------------------------------------------------------------\n");
	return 0;
}
Exemplo n.º 14
0
int main(int argc, char* argv[])
{
    struct rlimit r;
    getrlimit(RLIMIT_NOFILE, &r);
    cout << "current rlimit: " << r.rlim_cur << endl;
    r.rlim_cur = 1024 * 16;
    setrlimit(RLIMIT_NOFILE, &r);
    cout << "change rlimit to: " << r.rlim_cur << endl;

    struct timeval startTime, endTime;

    gettimeofday(&startTime, NULL);

    int numFiles=1;
    vector<string> pairNameVector, distNameVector;

    FILE **distFileList, **pairFileList;
    unsigned int ** inPairArray;
    float ** inDistArray;

    string inFileName = "", outFileName = "";

    printf("\n----------------------------------------------------------------------\n");
    printf("                  AVERAGE CLUSTERING GENETIC DISTANCES                  \n");

    float endLevel = -1;
    unsigned long long maxNumEdges=0;
    int i, iteration = 0;
    int numReads=0;

    getOptions(argc, argv, inFileName, numReads, numFiles, endLevel, outFileName);
    if (endLevel < 0 || endLevel > 1)
        endLevel = 1/log2((double)numReads);

    getDistNameList(inFileName, pairNameVector, distNameVector, numFiles);

    FILE * outFile = NULL;
    FILE * outFile1 = NULL;

    if (outFileName.length() > 0) {
        string outFileName1 = outFileName;
        outFileName1.append("_matlab");
        cout << outFileName << endl;
        cout << outFileName1 << endl;
        outFile = fopen(outFileName.c_str(), "w");
        outFile1 = fopen(outFileName1.c_str(), "w");
        if(outFile==NULL || outFile1==NULL)
        {
            cout<<"Error: Cannot open output file" << endl;
            cout << outFileName << endl;
            cout << outFileName1 << endl;
            exit(-1);
        }
    }
    FILE * mergeFile = NULL;
    string mergeFileName;

    mergeFileName=inFileName;
    mergeFileName.append("_Align_Merge");
    mergeFile = fopen(mergeFileName.c_str(), "w");

    if(pairNameVector.size()==0)
    {
        cout<<"Error: No distance file loaded."<<endl;
        exit(-1);
    }

    int fileId;

    pairFileList=(FILE**)malloc(sizeof(FILE*)*pairNameVector.size());
    distFileList=(FILE**)malloc(sizeof(FILE*)*distNameVector.size());

    if(distFileList==NULL || pairFileList==NULL)
    {
        cout<<"Error: Not enough memory" << endl;
        exit(-1);
    }

    for(fileId = 0; fileId < numFiles; ++fileId)
    {
        pairFileList[fileId]=fopen(pairNameVector[fileId].c_str(),"rb");
        if(pairFileList[fileId]==NULL)
        {
            cout<<"Error: Cannot open file" << pairNameVector[fileId].c_str() << endl;
            exit(-1);
        }
    }

    for(fileId = 0; fileId < numFiles; ++fileId)
    {
        distFileList[fileId]=fopen(distNameVector[fileId].c_str(),"rb");
        if(distFileList[fileId]==NULL)
        {
            cout<<"Error: Cannot open file" << distNameVector[fileId].c_str() << endl;
            exit(-1);
        }
    }

    if(numFiles!=distNameVector.size() || numFiles <= 0)
    {
        cout<<"Error: invalid number of files!EXIT..."<<endl;
        exit(-1);
    }
    cout<<"Use "<<numFiles<<" distance file(s)."<<endl;
    cout<<"endLevel: " << endLevel << endl;

    unsigned long long totalNumPairs = 0;

    multimap<float, DistPair> nodeMap;
    multimap<float, DistPair>::iterator iter;
    unsigned int idX, idY;
    float dist;
    inPairArray = (unsigned int **) malloc(sizeof(unsigned int*) * numFiles);
    inDistArray = (float **) malloc(sizeof(float*) * numFiles);
    int * indices = (int*) malloc(sizeof(int) * numFiles);
    int * readSizes = (int*) malloc(sizeof(int) * numFiles);
    bool * EOFTags = (bool*) malloc(sizeof(bool) * numFiles);
    bool suc;

    for(fileId=0; fileId<numFiles; fileId++)
    {
        // initialize
        inPairArray[fileId] = (unsigned int*) malloc(sizeof(unsigned int) * BUF_SIZE * 2);
        inDistArray[fileId] = (float*) malloc(sizeof(float) * BUF_SIZE);
        indices[fileId] = 0;
        readSizes[fileId] = 0;
        EOFTags[fileId] = false;

        // add the first pair of each file to the nodeMap
        suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);

        if (suc)
            nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));
    }

    unsigned int minExactIndex;
    TreeNode *nodeX=0, *nodeY=0;
    vector<float>::iterator minExactIter;
    float minExactDist, minInexactDist;
    LinkMap::iterator mapIter;

    vActiveNodes.resize(2*numReads+1);
    vExactNodes.resize(2*numReads+1);
    vExactDist.resize(2*numReads+1);
    vInexactDist.resize(2*numReads+1);

    leaves=(TreeNode**)malloc(sizeof(TreeNode*)*numReads);

    if(leaves==0)
    {
        cout<<"Error: Not enough memory" << endl;
        exit(-1);
    }

    for(i = 0; i < numReads; ++i) {
        vActiveNodes[i] = leaves[i] = new TreeNode(i);
        vExactNodes[i] = 0;
    }

    for(i = numReads+1; i < 2*numReads+1; ++i) {
        vActiveNodes[i] = 0;
        vExactNodes[i] = 0;
    }

    newId = numReads;

    fill(vExactDist.begin(), vExactDist.end(), 1.0f);
    fill(vInexactDist.begin(), vInexactDist.end(), 1.0f);

    maxNumEdges = numReads;
    cout << "numReads: " << numReads << "\tmaxNumEdges: " << maxNumEdges << endl;

    //while (!nodeMap.empty())
    while (!allLoaded)
    {
        // the clustering can't continue and the edges exceed the capacity of the RAM
        if (totalNumEdges < maxNumEdges)
        {
            while(totalNumEdges <= maxNumEdges && !nodeMap.empty())
            {
                // get the first item in the nodeMap
                iter = nodeMap.begin();
                fileId = iter->second.fileId;

                // write to output
                idX = iter->second.idX;
                idY =  iter->second.idY;
                dist = iter->first;

                if (outFile != NULL) {
                    fprintf(outFile, "%d %d %f\n", idX, idY, dist);
                    fprintf(outFile1, "%d %d %f\n", idX+1, idY+1, dist);
                }

                if (dist < endLevel || fabs(dist-endLevel) < EPSILON)
                    absorb(idX, idY, dist);

                // remove the current item from the nodeMap
                nodeMap.erase(iter);

                suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);
                if (suc)
                    nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));
            }

            if (dist > lambda)
                lambda = dist;
        }
        else
        {
            maxNumEdges *= 2;
            cout << "new maxNumEdges: " << maxNumEdges << "\tnewId: " << newId << "\tlambda: " << lambda << endl;

            if (maxNumEdges > UPPER_BOUND_NUM_EDGES)
            {
                /*
                cout << "LOOK AHEAD: current node: " << newId << "\tnum edges: " << totalNumEdges << endl;

                while(!nodeMap.empty())
                {
                	// get the first item in the nodeMap
                	iter = nodeMap.begin();
                	fileId = iter->second.fileId;

                	// write to output
                	idX = iter->second.idX;
                	idY =  iter->second.idY;
                	dist = iter->first;

                	lookAhead(idX, idY, dist);
                	// remove the current item from the nodeMap
                	nodeMap.erase(iter);

                	suc = loadAPair(readSizes[fileId], indices[fileId], EOFTags[fileId], idX, idY, dist, pairFileList[fileId], distFileList[fileId], inPairArray[fileId], inDistArray[fileId]);
                	if (suc)
                		nodeMap.insert(pair<float, DistPair>(dist,DistPair(idX,idY,fileId)));
                }
                */
                endLevel = lambda;
                allLoaded = true;
                cout << "new endLevel: " << endLevel << endl;
            }
        }

        if (nodeMap.empty())
            allLoaded = true;

        updateAllMin(endLevel);

        minInexactDist = *min_element(vInexactDist.begin(), vInexactDist.end());
        minExactIter = min_element(vExactDist.begin(),vExactDist.end());
        minExactDist = *minExactIter;

        //cout << "lambda_" << iteration << " = " << lambda << "\t" << lambda/2 << "\t" << minInexactDist << endl;

        while ((minExactDist < 1.0f) && (minExactDist < minInexactDist || fabs(minExactDist-minInexactDist) < EPSILON))
        {
            minExactIndex = minExactIter-vExactDist.begin();

            nodeX = vActiveNodes[minExactIndex]->topParent;
            nodeY = vExactNodes[minExactIndex]->topParent;

            merge(nodeX, nodeY, minExactDist, endLevel);
            fprintf(mergeFile, "%d %d %.6f\n", nodeX->ID+1, nodeY->ID+1, minExactDist);

            minInexactDist = *min_element(vInexactDist.begin(), vInexactDist.end());
            minExactIter = min_element(vExactDist.begin(),vExactDist.end());
            minExactDist = *minExactIter;
        }

        //cout << "cannot progress: " << newId << "\t" << totalNumEdges << "\t" << minInexactDist << "\t" << minExactDist << "\t" << minExactDist - lambda/2 << "\n";
        ++iteration;
    }

    cout << "DONE!" << endl;
    cout << "current node: " << newId << "\tnum unlinked: " << totalUnlinked << endl;

    // get root nodes and orphan nodes
    NodeSet roots;
    IDList orphanNodes;
    TreeNode *aLeaf = 0;

    for(i=0; i< numReads; ++i)
    {
        aLeaf = leaves[i];
        if(aLeaf->parent==0)  // find nodes with no parent
            orphanNodes.push_back(i);
        else
            roots.insert(aLeaf->topParent);
    }

    // print output to files

    string clusterListName, clusterName;

    clusterListName=inFileName;
    clusterListName.append(".Cluster_List");
    clusterName=inFileName;
    clusterName.append(".Cluster");

    printClusters(roots, orphanNodes, clusterListName, clusterName, endLevel);

    // clear memory
    emptyTree(roots);
    roots.clear();
    orphanNodes.clear();
    free(leaves);
    vActiveNodes.clear();
    vExactNodes.clear();
    vExactDist.clear();

    // clean up
    for(fileId=0; fileId<numFiles; ++fileId) {
        free(inDistArray[fileId]);
        free(inPairArray[fileId]);
    }
    free(inDistArray);
    free(inPairArray);
    free(indices);
    free(readSizes);
    free(EOFTags);
    free(pairFileList);
    free(distFileList);

    gettimeofday(&endTime, NULL);
    long elapsedTime = (endTime.tv_sec - startTime.tv_sec) * 1000u + (endTime.tv_usec - startTime.tv_usec) / 1.e3 + 0.5;

    if (outFile != NULL) {
        fclose(outFile);
        fclose(outFile1);
    }
    fclose(mergeFile);

    printf("totalNumPairs: %llu\n", totalNumPairs);
    printf("Time taken: %.3f s\n", elapsedTime/1.e3);
    printf("\n----------------------------------------------------------------------\n");
    return 0;
}
Exemplo n.º 15
0
  void BayesBall::requisiteNodes( const DAG& dag,
                                  const NodeSet& query,
                                  const NodeSet& hardEvidence,
                                  const NodeSet& softEvidence,
                                  NodeSet& requisite ) {
    // for the moment, no node is requisite
    requisite.clear ();
    
    // create the marks (top = first and bottom = second )
    NodeProperty<std::pair<bool, bool>> marks ( dag.size() );
    const std::pair<bool, bool> empty_mark( false, false );

    // indicate that we will send the ball to all the query nodes (as children):
    // in list nodes_to_visit, the first element is the next node to send the
    // ball to and the Boolean indicates whether we shall reach it from one of
    // its children (true) or from one parent (false)
    List<std::pair<NodeId, bool>> nodes_to_visit;
    for ( const auto node : query ) {
      nodes_to_visit.insert( std::pair<NodeId, bool>( node, true ) );
    }

    // perform the bouncing ball until there is no node in the graph to send
    // the ball to
    while ( ! nodes_to_visit.empty() ) {
      // get the next node to visit
      NodeId node = nodes_to_visit.front().first;

      // if the marks of the node do not exist, create them
      if ( ! marks.exists( node ) )
        marks.insert( node, empty_mark );

      // bounce the ball toward the neighbors
      if ( nodes_to_visit.front().second ) {  // visit from a child
        nodes_to_visit.popFront();
        requisite.insert ( node );

        if ( hardEvidence.exists( node ) ) {
          continue;
        }

        if ( not marks[node].first ) {
          marks[node].first = true;  // top marked
          for ( const auto par : dag.parents( node ) ) {
            nodes_to_visit.insert( std::pair<NodeId, bool>( par, true ) );
          }
        }

        if ( not marks[node].second ) {
          marks[node].second = true;  // bottom marked
          for ( const auto chi : dag.children( node ) ) {
            nodes_to_visit.insert( std::pair<NodeId, bool>( chi, false ) );
          }
        }
      }
      else {  // visit from a parent
        nodes_to_visit.popFront();

        const bool is_hard_evidence = hardEvidence.exists( node );
        const bool is_evidence = is_hard_evidence or softEvidence.exists( node );

        if ( is_evidence && ! marks[node].first ) {
          marks[node].first = true;
          requisite.insert ( node );

          for ( const auto par : dag.parents( node ) ) {
            nodes_to_visit.insert( std::pair<NodeId, bool>( par, true ) );
          }
        }

        if ( ! is_hard_evidence && ! marks[node].second ) {
          marks[node].second = true;

          for ( const auto chi : dag.children( node ) ) {
            nodes_to_visit.insert( std::pair<NodeId, bool>( chi, false ) );
          }
        }
      }
    }
  }