/** * \file DataPointsAssignment.cpp * \brief Tools for data points assignment, after anchor point segmentation * * Copyright 2007-2013 IMP Inventors. All rights reserved. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include IMPMULTIFIT_BEGIN_NAMESPACE namespace { bool sort_data_points_first_larger_than_second( const std::pair &a, const std::pair &b) { return a.first>b.first; } } DataPointsAssignment::DataPointsAssignment (const IMP::statistics::internal::XYZDataPoints *data, const IMP::statistics::internal::ClusteringEngine *cluster_engine) { cluster_engine_ = cluster_engine; data_ = data; IMP_USAGE_CHECK(data_->get_number_of_data_points() > 0, "DataPointsAssignment::DataPointsAssignment zero points,"<< "nothing to assign"); IMP_LOG_VERBOSE("going to set clusters"<< std::endl); set_clusters(); IMP_LOG_VERBOSE("going to set edges"<< std::endl); set_edges(); IMP_LOG_VERBOSE("finish assignment"<< std::endl); } algebra::Vector3Ds DataPointsAssignment::get_cluster_vectors(int cluster_id) const { IMP_USAGE_CHECK( static_cast(cluster_id) mdl = new Model(); ParticlesTemp full_set;//all points of the cluster for (int i=0;iget_number_of_data_points();i++) { if (cluster_engine_->is_part_of_cluster(i,cluster_ind)) { core::XYZR x = core::XYZR::setup_particle(new Particle(mdl), algebra::Sphere3D(data_->get_vector(i),1)); atom::Mass::setup_particle(x,1); full_set.push_back(x); } } Pointer full_map = em::particles2density(full_set,3,1.5); //map the particles to their voxels std::map voxel_particle_map; for(unsigned int i=0;iget_voxel_by_location(v)]=v; } full_map->set_was_used(true); IntsList conn_comp=get_connected_components(full_map,0.001,0.8); IMP_LOG_TERSE("Number of connected components:"<get_center(cluster_ind); cluster_set.push_back(algebra::Vector3D(cen[0],cen[1],cen[2])); IMP_LOG_VERBOSE("setting cluster " << cluster_ind << " with " << cluster_set.size() << " points " << std::endl); return cluster_set; } /** This function creates list of particles for each cluster */ void DataPointsAssignment::set_clusters() { cluster_sets_.clear(); for(int i=0;iget_number_of_clusters();i++) { cluster_sets_.push_back(set_cluster(i)); } } void DataPointsAssignment::set_edges(double voxel_size) { //create projected density maps for each cluster std::vector > dmaps; std::vector boxes; Pointer mdl = new Model(); for(int i=0;iget_number_of_clusters();i++) { algebra::Vector3Ds vecs =get_cluster_vectors(i); ParticlesTemp ps(vecs.size()); for(unsigned int j=0;j segment_map = em::particles2density(ps,voxel_size*1.5,voxel_size); segment_map->set_was_used(true); dmaps.push_back(segment_map); }//end create maps //define edges for(int i=0;iget_number_of_clusters();i++) { for(int j=i+1;jget_number_of_clusters();j++) { if (!algebra::get_interiors_intersect(boxes[i],boxes[j])) continue; algebra::Vector3Ds vecs = get_cluster_vectors(j); //check if the vectors are inside the i'th map bool touching=false; for(unsigned int k=0;(kis_part_of_volume(vecs[k])) continue; if (dmaps[i]->get_value(vecs[k])>0.01) touching=true; } if (touching) { edges_.push_back(IntPair(i,j)); } }//for j }//for i } void DataPointsAssignment::connect_clusters(int c1, int c2) { IMP_USAGE_CHECK(c1 != c2, "DataPointsAssignment::connect_centers can"<< " not connect a cluster to itself"); int min_c = std::min(c1,c2); int max_c = std::max(c1,c2); if (edges_map_.find(IntPair(min_c,max_c)) == edges_map_.end()) { edges_map_[IntPair(min_c,max_c)]=1; edges_.push_back(IntPair(min_c,max_c)); } } void write_segments_as_pdb(const DataPointsAssignment &dpa, const std::string &filename){ for( int i=0;i segment_map( new em::DensityMap(*(dmap->get_header()))); segment_map->reset_data(0.); // segment_map->update_voxel_size(apix); algebra::Vector3Ds vecs =dpa.get_cluster_vectors(segment_id); for(unsigned int i=0;iset_value( vecs[i][0],vecs[i][1],vecs[i][2],dmap->get_value(vecs[i])); } em::write_map(segment_map,filename.c_str(),new em::MRCReaderWriter()); segment_map=static_cast(nullptr); } algebra::Vector3D get_segment_maximum(const DataPointsAssignment &dpa, em::DensityMap *dmap, int segment_id){ algebra::Vector3Ds vecs =dpa.get_cluster_xyz(segment_id); std::vector > data_for_sorting; for(algebra::Vector3Ds::iterator it = vecs.begin(); it != vecs.end(); it++) { data_for_sorting.push_back( std::pair(dmap->get_value(*it),*it)); } std::sort(data_for_sorting.begin(),data_for_sorting.end(), sort_data_points_first_larger_than_second); return data_for_sorting[0].second; } algebra::Vector3D get_segment_maximum(const DataPointsAssignment &dpa, DensGrid *dmap, int segment_id){ algebra::Vector3Ds vecs =dpa.get_cluster_xyz(segment_id); std::vector > data_for_sorting; for(algebra::Vector3Ds::iterator it = vecs.begin(); it != vecs.end(); it++) { data_for_sorting.push_back( std::pair((*dmap)[dmap->get_nearest_index(*it)], *it)); } std::sort(data_for_sorting.begin(),data_for_sorting.end(), sort_data_points_first_larger_than_second); return data_for_sorting[0].second; } /* void write_max_cmm(const std::string &cmm_filename, em::DensityMap *dmap, const std::string &marker_set_name, const DataPointsAssignment &dpa) { algebra::Vector3Ds centers; Floats radii; for( int i=0;iget_center(i); centers.push_back(algebra::Vector3D(xyz[0],xyz[1],xyz[2])); out< mrc_filenames; for( int i=0;i(cluster_ind) gs = // display_helper(dpa); // IMP_NEW(display::ChimeraWriter,w,(chimera_filename)); // for( int i=0;iadd_geometry(gs[i]); // } // w=nullptr; // } IMPMULTIFIT_END_NAMESPACE