示例#1
0
文件: blame.cpp 项目: AljGaber/imp
void assign_blame(const RestraintsTemp &rs,
                  const ParticlesTemp &ps, FloatKey attribute) {
  IMP_FUNCTION_LOG;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    if (ps[i]->has_attribute(attribute)) {
      ps[i]->set_value(attribute, 0);
    } else {
      ps[i]->add_attribute(attribute, 0, false);
    }
  }
  Restraints drs;
  for (unsigned int i = 0; i < rs.size(); ++i) {
    Pointer<Restraint> rd = rs[i]->create_decomposition();
    if (rd) {
      drs.push_back(rd);
    }
  }
  IMP_NEW(RestraintsScoringFunction, rsf, (drs));
  rsf->evaluate(false);
  DependencyGraph dg = get_dependency_graph(IMP::internal::get_model(rs));
  // attempt to get around boost/gcc bug and the most vexing parse
  DependencyGraphVertexIndex dgi((IMP::get_vertex_index(dg)));
  ControlledBy controlled_by;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    ParticlesTemp cps = get_dependent_particles(ps[i], ps, dg, dgi);
    IMP_INTERNAL_CHECK(cps.size() > 0, "No dependent particles for " << ps[i]);
    for (unsigned int j = 0; j < cps.size(); ++j) {
      controlled_by[cps[j]] = ps[i];
    }
  }
  for (unsigned int i = 0; i < drs.size(); ++i) {
    distribute_blame(drs[i], controlled_by, attribute, 1.0);
  }
}
示例#2
0
void MolecularDynamicsMover::do_reject() {
  IMP_OBJECT_LOG;
  ParticlesTemp ps = md_->get_simulation_particles();
  unsigned nparts = ps.size();
  IMP_USAGE_CHECK(coordinates_.size() == ps.size(),
                  "The list of particles that move has been changed!");
  IMP_USAGE_CHECK(velocities_.size() == ps.size(),
                  "The list of particles that move has been changed!");
  for (unsigned i = 0; i < nparts; i++) {
    bool isnuisance = Nuisance::get_is_setup(ps[i]);
    bool isxyz = core::XYZ::get_is_setup(ps[i]);
    IMP_USAGE_CHECK(isnuisance || isxyz,
                    "Particle " << ps[i] << " is neither nuisance nor xyz!");
    if (isnuisance) {
      IMP_USAGE_CHECK(coordinates_[i].size() == 1,
                      "wrong size for coordinates_[" << i << "] !");
      IMP_USAGE_CHECK(velocities_[i].size() == 1, "wrong size for velocities_["
                                                      << i << "] !");
      Nuisance(ps[i]).set_nuisance(coordinates_[i][0]);
      ps[i]->set_value(FloatKey("vel"), velocities_[i][0]);
    }
    if (isxyz) {
      IMP_USAGE_CHECK(coordinates_[i].size() == 3,
                      "wrong size for coordinates_[" << i << "] !");
      IMP_USAGE_CHECK(velocities_[i].size() == 3, "wrong size for velocities_["
                                                      << i << "] !");
      core::XYZ(ps[i]).set_coordinate(0, coordinates_[i][0]);
      core::XYZ(ps[i]).set_coordinate(1, coordinates_[i][1]);
      core::XYZ(ps[i]).set_coordinate(2, coordinates_[i][2]);
      ps[i]->set_value(FloatKey("vx"), velocities_[i][0]);
      ps[i]->set_value(FloatKey("vy"), velocities_[i][1]);
      ps[i]->set_value(FloatKey("vz"), velocities_[i][2]);
    }
  }
}
示例#3
0
文件: blame.cpp 项目: AljGaber/imp
display::Geometries create_blame_geometries(const RestraintsTemp &rs,
                                            const ParticlesTemp &ps,
                                            double max, std::string name) {
  IMP_FUNCTION_LOG;
  FloatKey key("blame temporary key");
  assign_blame(rs, ps, key);
  if (max == NO_MAX) {
    max = -NO_MAX;
    for (unsigned int i = 0; i < ps.size(); ++i) {
      max = std::max(ps[i]->get_value(key), max);
    }
    IMP_LOG_TERSE("Maximum blame value is " << max << std::endl);
  }
  display::Geometries ret;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    double colorv;
    if (max == 0) {
      colorv = 0;
    } else {
      colorv =
          display::get_linear_color_map_value(0, max, ps[i]->get_value(key));
    }
    display::Color c = display::get_hot_color(colorv);
    IMP_NEW(XYZRGeometry, g, (ps[i]));
    if (!name.empty()) {
      g->set_name(name);
    }
    g->set_color(c);
    ret.push_back(g);
  }
  return ret;
}
示例#4
0
void destroy(Hierarchy d) {
  ParticlesTemp all;
  //core::Hierarchy h=d;

  core::gather(d, True(), std::back_inserter(all));
  for (unsigned int i=0; i< all.size(); ++i) {
    if (Bonded::particle_is_instance(all[i])) {
     Bonded b(all[i]);
      while (b.get_number_of_bonds() > 0) {
        destroy_bond(b.get_bond(b.get_number_of_bonds()-1));
      }
    }
    Hierarchy hc(all[i]);
    while (hc.get_number_of_children() > 0) {
      hc.remove_child(hc.get_child(hc.get_number_of_children()-1));
    }
  }

  // If this Hierarchy has a parent, remove the relationship
  Hierarchy parent = d.get_parent();
  if (parent) {
    parent.remove_child(d);
  }

  for (unsigned int i=0; i< all.size(); ++i) {
    all[i]->get_model()->remove_particle(all[i]);
  }
}
示例#5
0
double get_radius_of_gyration(const ParticlesTemp& ps) {
  IMP_USAGE_CHECK(ps.size() > 0, "No particles provided");
  bool mass = Mass::get_is_setup(ps[0]);
  bool radii = core::XYZR::get_is_setup(ps[0]);
  algebra::Vector3D cm(0, 0, 0);
  double total = 0;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    double weight = get_weight(mass, radii, ps[i]);
    total += weight;
    cm += core::XYZ(ps[i]).get_coordinates() * weight;
  }
  cm /= total;
  double ret = 0;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    double c;
    if (radii) {
      c = .6 * square(core::XYZR(ps[i]).get_radius());
    } else {
      c = 0;
    }
    double d = get_squared_distance(core::XYZ(ps[i]).get_coordinates(), cm);
    ret += get_weight(mass, radii, ps[i]) * (d + c);
  }
  return std::sqrt(ret / total);
}
float get_actual_radius_of_gyration(ParticlesTemp ps) {
  algebra::Vector3D cm(0,0,0);
  for (unsigned int i=0; i< ps.size(); ++i) {
    cm+= core::XYZ(ps[i]).get_coordinates();
  }
  cm/=ps.size();
  double ret=0;
  for (unsigned int i=0; i < ps.size(); ++i) {
    double d= get_distance(core::XYZ(ps[i]).get_coordinates(),cm);
    ret+= d;
  }
  return ret/ps.size();
}
示例#7
0
IMPEM_BEGIN_NAMESPACE

FitRestraint::FitRestraint(
   ParticlesTemp ps,
   DensityMap *em_map,
   FloatPair norm_factors,
   FloatKey weight_key,
   float scale,
   bool use_rigid_bodies,
   KernelType kt
                           ): Restraint(IMP::internal::get_model(ps),
                                        "Fit restraint"),kt_(kt)
{
  use_rigid_bodies_=use_rigid_bodies;
  IMP_LOG(TERSE,"Load fit restraint with the following input:"<<
          "number of particles:"<<ps.size()<<" scale:"<<scale<<
           "\n");
  // special_treatment_of_particles_outside_of_density_=
  //   special_treatment_of_particles_outside_of_density;
  target_dens_map_ = em_map;
  weight_key_=weight_key;
  norm_factors_=norm_factors;
  IMP_IF_CHECK(USAGE) {
    for (unsigned int i=0; i< ps.size(); ++i) {
      IMP_USAGE_CHECK(core::XYZR::particle_is_instance(ps[i]),
                      "Particle " << ps[i]->get_name()
                      << " is not XYZR"
                      << std::endl);
      IMP_USAGE_CHECK(ps[i]->has_attribute(weight_key),
                "Particle " << ps[i]->get_name()
                << " is missing the mass "<< weight_key
                << std::endl);
    }
  }
  scalefac_ = scale;
  store_particles(ps);
  IMP_LOG(TERSE,"after adding "<< all_ps_.size()<<" particles"<<std::endl);
  model_dens_map_ = new SampledDensityMap(*em_map->get_header(),kt_);
  model_dens_map_->set_particles(get_as<ParticlesTemp>(all_ps_),weight_key);
  kernel_params_=model_dens_map_->get_kernel_params();
  dist_mask_=new DistanceMask(model_dens_map_->get_header());
  IMP_LOG(TERSE,"going to initialize_model_density_map"<<std::endl);
  initialize_model_density_map(weight_key);
  IMP_LOG(TERSE,"going to initialize derivatives"<<std::endl);
   // initialize the derivatives
  dv_.insert(dv_.end(),all_ps_.size(),algebra::Vector3D(0.,0.,0.));

  // normalize the target density data
  //target_dens_map->std_normalize();
  IMP_LOG(TERSE, "Finish initialization" << std::endl);
}
示例#8
0
IMPCORE_BEGIN_NAMESPACE

FixedRefiner::FixedRefiner(const ParticlesTemp &ps)
  : Refiner("FixedRefiner%d", true) {
  IMP_USAGE_CHECK(ps.size()>0, "cannot refine with empty particle list");
  IMP_LOG_VERBOSE("Created fixed particle refiner with "
                  << ps.size() << " particles" << std::endl);
  m_ = ps[0]->get_model();
  for(unsigned int i = 0; i < ps.size(); i++){
    IMP_USAGE_CHECK(m_ == ps[i]->get_model(),
                    "refiner assumes all particles are from the same model");
    pis_.push_back(ps[i]->get_index());
  }
}
示例#9
0
void CollisionCrossSection::set_model_particles(
    const ParticlesTemp &ps) {

  IMP_LOG_TERSE("CollisionCrossSection: Model particles set"
                << std::endl);

  for (unsigned int i = 0; i < ps.size(); ++i) {
    IMP_USAGE_CHECK(
        (core::XYZR::get_is_setup(ps[i]) && atom::Mass::get_is_setup(ps[i])),
        "Particle " << i << " does not have the required attributes"
                    << std::endl);
  }
  masks_manager_->create_masks(ps);
  // Compute projections
  collision_cross_section_ = 0.0;
  for (unsigned int i = 0; i < n_projections_; ++i) {
    ProjectingOptions options(pixel_size_, resolution_);
    do_project_particles(ps, average_projection_, regs_[i].get_rotation(),
                         pixel_size_ * regs_[i].get_shift_3d(), options,
                         masks_manager_);
    collision_cross_section_ += get_projected_area(average_projection_);
  }
  collision_cross_section_ /= static_cast<double>(n_projections_);
  particles_set_ = true;
}
示例#10
0
IMPCORE_BEGIN_NAMESPACE

FixedRefiner::FixedRefiner(const ParticlesTemp &ps):
  Refiner("FixedRefiner%d"), ps_(ps){
  IMP_LOG(VERBOSE, "Created fixed particle refiner with " << ps.size()
          << " particles" << std::endl);
}
示例#11
0
void MolecularDynamicsMover::save_coordinates() {
  IMP_OBJECT_LOG;
  ParticlesTemp ps = md_->get_simulation_particles();
  unsigned nparts = ps.size();
  coordinates_.clear();
  coordinates_.reserve(nparts);
  velocities_.clear();
  velocities_.reserve(nparts);
  for (unsigned i = 0; i < nparts; i++) {
    bool isnuisance = Nuisance::get_is_setup(ps[i]);
    bool isxyz = core::XYZ::get_is_setup(ps[i]);
    IMP_USAGE_CHECK(isnuisance || isxyz,
                    "Particle " << ps[i] << " is neither nuisance nor xyz!");
    if (isnuisance) {
      std::vector<double> x(1, Nuisance(ps[i]).get_nuisance());
      coordinates_.push_back(x);
      std::vector<double> v(1, ps[i]->get_value(FloatKey("vel")));
      velocities_.push_back(v);
    }
    if (isxyz) {
      std::vector<double> coords;
      core::XYZ d(ps[i]);
      coords.push_back(d.get_coordinate(0));
      coords.push_back(d.get_coordinate(1));
      coords.push_back(d.get_coordinate(2));
      coordinates_.push_back(coords);
      std::vector<double> v;
      v.push_back(ps[i]->get_value(FloatKey("vx")));
      v.push_back(ps[i]->get_value(FloatKey("vy")));
      v.push_back(ps[i]->get_value(FloatKey("vz")));
      velocities_.push_back(v);
    }
  }
}
unsigned int MSConnectivityRestraint::ParticleMatrix::add_type(
    const ParticlesTemp &ps) {
  protein_by_class_.push_back(Ints());
  for (unsigned int i = 0; i < ps.size(); ++i) {
    unsigned int n = particles_.size();
    particles_.push_back(ParticleData(ps[i], current_id_));
    protein_by_class_.back().push_back(n);
  }
  return current_id_++;
}
示例#13
0
文件: io.cpp 项目: drussel/imp
base::Vector<char>
write_particles_to_buffer(const ParticlesTemp &particles,
                          const FloatKeys &keys) {
  if (particles.empty() || keys.empty()) {
    return base::Vector<char>();
  }
  unsigned int size= particles.size()*keys.size()*sizeof(double);
  base::Vector<char> ret(size);
  write_particles_to_buffer(particles, keys, &ret.front(), size);
  return ret;
}
示例#14
0
void ParticlesDataPoints::populate_data_points(ParticlesTemp ps) {
  ps_ = get_as<Particles>(ps);
  int dim = atts_.size();
  for(unsigned int i=0;i<ps.size();i++) {
    data_.push_back(Array1DD(dim));
    for(int d=0;d<dim;d++) {
      data_[i][d] = double(ps[i]->get_value(atts_[d]));
    }
    vecs_.push_back(core::XYZ(ps[i]).get_coordinates());
  }
}
void DerivativesToRefined
::apply(Particle *p,
        DerivativeAccumulator &da) const
{
  ParticlesTemp ps = refiner_->get_refined(p);

  for (unsigned int i=0; i< ps.size(); ++i) {
    for (unsigned int j=0; j< ks_.size(); ++j) {
      Float f= p->get_derivative(ks_[j]);
      ps[i]->add_to_derivative(ks_[j], f, da);
    }
  }
}
示例#16
0
InteractionGraph get_interaction_graph(ScoringFunctionAdaptor rsi,
                                       const ParticlesTemp &ps) {
  if (ps.empty()) return InteractionGraph();
  InteractionGraph ret(ps.size());
  Restraints rs =
      create_decomposition(rsi->create_restraints());
  // Model *m= ps[0]->get_model();
  boost::unordered_map<ModelObject *, int> map;
  InteractionGraphVertexName pm = boost::get(boost::vertex_name, ret);
  DependencyGraph dg = get_dependency_graph(ps[0]->get_model());
  DependencyGraphVertexIndex index = IMP::get_vertex_index(dg);
  /*IMP_IF_LOG(VERBOSE) {
    IMP_LOG_VERBOSE( "dependency graph is \n");
    IMP::internal::show_as_graphviz(dg, std::cout);
    }*/
  for (unsigned int i = 0; i < ps.size(); ++i) {
    ParticlesTemp t = get_dependent_particles(
        ps[i], ParticlesTemp(ps.begin(), ps.end()), dg, index);
    for (unsigned int j = 0; j < t.size(); ++j) {
      IMP_USAGE_CHECK(map.find(t[j]) == map.end(),
                      "Currently particles which depend on more "
                          << "than one particle "
                          << "from the input set are not supported."
                          << "  Particle \"" << t[j]->get_name()
                          << "\" depends on \"" << ps[i]->get_name()
                          << "\" and \""
                          << ps[map.find(t[j])->second]->get_name() << "\"");
      map[t[j]] = i;
    }
    IMP_IF_LOG(VERBOSE) {
      IMP_LOG_VERBOSE("Particle \"" << ps[i]->get_name() << "\" controls ");
      for (unsigned int i = 0; i < t.size(); ++i) {
        IMP_LOG_VERBOSE("\"" << t[i]->get_name() << "\" ");
      }
      IMP_LOG_VERBOSE(std::endl);
    }
    pm[i] = ps[i];
  }
  IMP::Restraints all_rs = IMP::get_restraints(rs);
  for (Restraints::const_iterator it = all_rs.begin();
       it != all_rs.end(); ++it) {
    ModelObjectsTemp pl = (*it)->get_inputs();
    add_edges(ps, pl, map, *it, ret);
  }
  /* Make sure that composite score states (eg the normalizer for
     rigid body rotations) don't induce interactions among unconnected
     particles.*/
  ScoreStatesTemp ss = get_required_score_states(rs);
  for (ScoreStatesTemp::const_iterator it = ss.begin(); it != ss.end(); ++it) {
    ModelObjectsTemps interactions = (*it)->get_interactions();
    for (unsigned int i = 0; i < interactions.size(); ++i) {
      add_edges(ps, interactions[i], map, *it, ret);
    }
  }
  IMP_INTERNAL_CHECK(boost::num_vertices(ret) == ps.size(),
                     "Wrong number of vertices " << boost::num_vertices(ret)
                                                 << " vs " << ps.size());
  return ret;
}
示例#17
0
void RestraintCache::add_restraints(const RestraintsAdaptor &rs) {
  IMP_OBJECT_LOG;
  if (rs.empty()) return;
  Model *m = rs[0]->get_model();
  DependencyGraph dg = get_dependency_graph(m);
  ParticleStatesTable *pst = cache_.get_generator().get_particle_states_table();
  DepMap dependencies;
  ParticlesTemp allps = pst->get_particles();
  DependencyGraphVertexIndex index = IMP::get_vertex_index(dg);
  for (unsigned int i = 0; i < allps.size(); ++i) {
    ParticlesTemp depp =
        get_dependent_particles(allps[i], allps, dg, index);
    for (unsigned int j = 0; j < depp.size(); ++j) {
      dependencies[depp[j]].push_back(allps[i]);
    }
    dependencies[allps[i]].push_back(allps[i]);
    IMP_LOG_TERSE("Particle " << Showable(allps[i]) << " controls "
                              << dependencies[allps[i]] << std::endl);
  }

  for (unsigned int i = 0; i < rs.size(); ++i) {
    Pointer<Restraint> r = rs[i]->create_decomposition();
    IMP_IF_LOG(TERSE) {
      IMP_LOG_TERSE("Before:" << std::endl);
      IMP_LOG_WRITE(TERSE, show_restraint_hierarchy(rs[i]));
    }
    if (r) {
      IMP_LOG_TERSE("after:" << std::endl);
      IMP_LOG_WRITE(TERSE, show_restraint_hierarchy(r));
      add_restraint_internal(r, next_index_, nullptr,
                             std::numeric_limits<double>::max(), Subset(),
                             dependencies);
    }
    ++next_index_;
  }
  IMP_IF_LOG(TERSE) {
    IMP_LOG_WRITE(TERSE, show_restraint_information(IMP_STREAM));
  }
}
示例#18
0
文件: Refiner.cpp 项目: salilab/imp
ParticleIndexes Refiner::get_refined_indexes(Model *m,
                                             ParticleIndex pi) const {
  ParticlesTemp ps = get_refined( m->get_particle(pi) );
  IMP_IF_CHECK(USAGE_AND_INTERNAL) {
    for(unsigned int i = 0; i < ps.size(); i++) {
      if (ps[i]->get_model() != m) {
        IMP_THROW("Refined particles model does not match parent model - "
                  "this is critical if get_refined_indexes() is used.",
                  IMP::ValueException);
      }
    }
  }
  return get_indexes(ps);
}
示例#19
0
ParticlePairsTemp get_possible_interactions(const ParticlesTemp &ps,
                                            double max_distance,
                                            ParticleStatesTable *pst) {
  if (ps.empty()) return ParticlePairsTemp();
  ParticleStatesList psl;
  ParticlesTemp all= pst->get_particles();
  unsigned int max=0;
  for (unsigned int i=0; i< all.size(); ++i) {
    psl.push_back( pst->get_particle_states(all[i]));
    max= std::max(psl[i]->get_number_of_particle_states(), max);
  }
  algebra::BoundingBox3Ds bbs(ps.size());
  for (unsigned int i=0; i< max; ++i) {
    for (unsigned int j=0; j< all.size(); ++j) {
      psl[j]->load_particle_state(std::min(i,
                 psl[j]->get_number_of_particle_states()-1),
                                      all[j]);
    }
    ps[0]->get_model()->update();
    for (unsigned int j=0; j< ps.size(); ++j) {
      core::XYZ d(ps[j]);
      bbs[j]+= d.get_coordinates();
    }
  }
  for (unsigned int j=0; j< ps.size(); ++j) {
    core::XYZR d(ps[j]);
    bbs[j]+= d.get_radius() + max_distance;
  }
  IMP_NEW(core::GridClosePairsFinder, gcpf, ());
  gcpf->set_distance(max_distance);
  IntPairs ips= gcpf->get_close_pairs(bbs);
  ParticlePairsTemp ret(ips.size());
  for (unsigned int i=0; i< ips.size(); ++i) {
    ret[i]= ParticlePair(ps[ips[i].first], ps[ips[i].second]);
  }
  return ret;
}
示例#20
0
Ints get_partial_index(const ParticlesTemp &particles,
               const Subset &subset, const Subsets &excluded) {
  for (unsigned int i=0; i< excluded.size(); ++i) {
    bool all=true;
    for (unsigned int j=0; j< particles.size(); ++j) {
      if (!std::binary_search(excluded[i].begin(), excluded[i].end(),
                              particles[j])) {
        all=false;
        break;
      }
    }
    if (all) {
      return Ints();
    }
  }
  Ints ret(particles.size(), -1);
  for (unsigned int i=0; i< particles.size(); ++i) {
    Subset::const_iterator it= std::lower_bound(subset.begin(),
                                                subset.end(), particles[i]);
    if (it!= subset.end() && *it == particles[i]) {
      ret[i]= it-subset.begin();
    }
  }
  IMP_IF_LOG(VERBOSE) {
    IMP_LOG(VERBOSE, "Returning ");
    for (unsigned int i=0; i< ret.size(); ++i) {
      IMP_LOG(VERBOSE, ret[i] << " ");
    }
    IMP_LOG(VERBOSE, "for ");
     for (unsigned int i=0; i< particles.size(); ++i) {
       IMP_LOG(VERBOSE, particles[i]->get_name() << " ");
     }
     IMP_LOG(VERBOSE, " subset " << subset << std::endl);
  }
  return ret;
}
示例#21
0
IMPMULTIFIT_BEGIN_NAMESPACE
void add_surface_index(core::Hierarchy mh, Float apix, FloatKey shell_key,
                       FloatKey, FloatKey) {
  ParticlesTemp ps = core::get_leaves(mh);
  Pointer<em::SurfaceShellDensityMap> shell_map =
      new em::SurfaceShellDensityMap(ps, apix);

  for (unsigned int i = 0; i < ps.size(); i++) {
    IMP_INTERNAL_CHECK(!ps[i]->has_attribute(shell_key),
                       "Particle " << ps[i]->get_name()
                                   << " already has shell attribute"
                                   << std::endl);
    ps[i]->add_attribute(
        shell_key, shell_map->get_value(core::XYZ(ps[i]).get_coordinates()));
  }
}
示例#22
0
Subset RestraintCache::get_subset(Restraint *r,
                                  const DepMap &dependencies) const {
  ParticlesTemp ups = IMP::get_input_particles(r->get_inputs());
  std::sort(ups.begin(), ups.end());
  ups.erase(std::unique(ups.begin(), ups.end()), ups.end());
  ParticlesTemp outps;
  for (unsigned int i = 0; i < ups.size(); ++i) {
    DepMap::const_iterator it = dependencies.find(ups[i]);
    if (it != dependencies.end()) {
      outps = outps + it->second;
    }
  }
  std::sort(outps.begin(), outps.end());
  outps.erase(std::unique(outps.begin(), outps.end()), outps.end());
  return Subset(outps);
}
示例#23
0
文件: project.cpp 项目: salilab/imp
void do_project_particles(const ParticlesTemp &ps, cv::Mat &m2,
                          const algebra::Rotation3D &R,
                          const algebra::Vector3D &translation,
                          const ProjectingOptions &options,
                          MasksManagerPtr masks) {
  IMP_LOG_VERBOSE("Projecting particles" << std::endl);
  if (m2.empty()) {
    IMP_THROW("Cannot project on a empty matrix", ValueException);
  }
  if (masks == MasksManagerPtr()) {
    // Create the masks
    masks = MasksManagerPtr(
        new MasksManager(options.resolution, options.pixel_size));
    masks->create_masks(ps);
  }
  // Centroid
  unsigned long n_particles = ps.size();
  // core::XYZRsTemp xyzrs(ps);
  core::XYZRs xyzrs(ps);
  algebra::Vector3D centroid = core::get_centroid(xyzrs);

  // clear data before creating a new projection
  if (options.clear_matrix_before_projecting) m2.setTo(0.0);
  // Project
  double invp = 1.0 / options.pixel_size;

  for (unsigned long i = 0; i < n_particles; i++) {
    // Coordinates respect to the centroid
    algebra::Vector3D p = xyzrs[i].get_coordinates() - centroid;
    // Pixel after transformation to project in Z axis
    // Not necessary to compute pz, is going to be ignored
    double pix_x = invp * (R.get_rotated_one_coordinate(p, 0) + translation[0]);
    double pix_y = invp * (R.get_rotated_one_coordinate(p, 1) + translation[1]);

    IMP_USAGE_CHECK(!IMP::isnan(pix_x) || !IMP::isnan(pix_y),
                    "do_project_particles: "
                        << n_particles << " resolution " << options.resolution
                        << " pixel size " << options.pixel_size << std::endl);

    // Apply mask
    ProjectionMaskPtr mask = masks->find_mask(atom::Mass(ps[i]).get_mass());
    algebra::Vector2D pix(pix_x, pix_y);
    mask->apply(m2, pix);
  }
  IMP_LOG_VERBOSE("END of do_project_particles" << std::endl);
}
示例#24
0
IMP::core::RigidBody create_compatible_rigid_body(Hierarchy h,
                                               Hierarchy reference) {
  ParticlesTemp hl= get_leaves(h);
  ParticlesTemp rl= get_leaves(reference);
  algebra::Transformation3D tr
    = algebra::get_transformation_aligning_first_to_second(rl, hl);
  algebra::Transformation3D rtr
    = core::RigidMember(reference).get_rigid_body().\
    get_reference_frame().get_transformation_to();
  algebra::Transformation3D rbtr= tr*rtr;

  Particle *rbp= new Particle(h->get_model());
  rbp->set_name(h->get_name()+" rigid body");
  ParticlesTemp all = rb_process(h);
  core::RigidBody rbd
    = core::RigidBody::setup_particle(rbp, algebra::ReferenceFrame3D(rbtr));
  for (unsigned int i=0; i< all.size(); ++i) {
    rbd.add_member(all[i]);
  }
  rbd.set_coordinates_are_optimized(true);
  IMP_INTERNAL_CHECK(h.get_is_valid(true), "Invalid hierarchy produced");
  return rbd;
}
示例#25
0
ParticleIndexesAdaptor::ParticleIndexesAdaptor(const ParticlesTemp &ps)
    : tmp_(new ParticleIndexes(ps.size())), val_(tmp_.get()) {
  *tmp_ = get_indexes(ps);
}
double
get_slack_estimate(const ParticlesTemp& ps,
                   double upper_bound,
                   double step,
                   const RestraintsTemp &restraints,
                   bool derivatives,
                   Optimizer *opt,
                   ClosePairContainer *cpc) {
  std::vector<Data> datas;
  for (double slack=0; slack< upper_bound; slack+= step) {
    IMP_LOG(VERBOSE, "Computing for " << slack << std::endl);
    datas.push_back(Data());
    datas.back().slack=slack;
    {
      boost::timer imp_timer;
      int count=0;
      base::SetLogState sl(opt->get_model(), SILENT);
      do {
        cpc->set_slack(slack);
        cpc->update();
        ++count;
      } while (imp_timer.elapsed()==0);
      datas.back().ccost= imp_timer.elapsed()/count;
      IMP_LOG(VERBOSE, "Close pair finding cost "
              << datas.back().ccost << std::endl);
    }
    {
      boost::timer imp_timer;
      double score=0;
      int count=0;
      int iters=1;
      base::SetLogState sl(opt->get_model(), SILENT);
      do {
        for ( int j=0; j< iters; ++j) {
          for (unsigned int i=0; i< restraints.size(); ++i) {
            score+=restraints[i]->evaluate(derivatives);
            // should restore
          }
        }
        count += iters;
        iters*=2;
      } while (imp_timer.elapsed()==0);
      datas.back().rcost= imp_timer.elapsed()/count;
      IMP_LOG(VERBOSE, "Restraint evaluation cost "
              << datas.back().rcost << std::endl);
    }
  }
  int ns=100;
  int last_ns=1;
  int opt_i=-1;
  std::vector<Floats > dists(1, Floats(1,0.0));
  std::vector< std::vector<algebra::Vector3D> >
    pos(1, std::vector<algebra::Vector3D>(ps.size()));
  for (unsigned int j=0; j< ps.size(); ++j) {
    pos[0][j]=core::XYZ(ps[j]).get_coordinates();
  }
  do {
    IMP_LOG(VERBOSE, "Stepping from " << last_ns << " to " << ns << std::endl);
    dists.resize(ns, Floats(ns, 0.0));
    for ( int i=0; i< last_ns; ++i) {
      dists[i].resize(ns, 0.0);
    }
    pos.resize(ns,  std::vector<algebra::Vector3D>(ps.size()));
    base::SetLogState sl(opt->get_model(), SILENT);
    for ( int i=last_ns; i< ns; ++i) {
      opt->optimize(1);
      for (unsigned int j=0; j< ps.size(); ++j) {
        pos[i][j]=core::XYZ(ps[j]).get_coordinates();
      }
    }
    for ( int i=last_ns; i< ns; ++i) {
      for ( int j=0; j< i; ++j) {
        double md=0;
        for (unsigned int k=0; k< ps.size(); ++k) {
          md= std::max(md, algebra::get_distance(pos[i][k], pos[j][k]));
        }
        dists[i][j]=md;
        dists[j][i]=md;
      }
    }
    // estimate lifetimes from slack
    for (unsigned int i=0; i< datas.size(); ++i) {
      Ints deaths;
      for ( int j=0; j< ns; ++j) {
        for ( int k=j+1; k < ns; ++k) {
          if (dists[j][k]> datas[i].slack) {
            deaths.push_back(k-j);
            break;
          }
        }
      }
      std::sort(deaths.begin(), deaths.end());
      // kaplan meier estimator
      double ml=0;
      if (deaths.empty()) {
        ml= ns;
      } else {
        //double l=1;
        IMP_INTERNAL_CHECK(deaths.size() < static_cast<unsigned int>(ns),
                           "Too much death");
        double S=1;
        for (unsigned int j=0; j< deaths.size(); ++j) {
          double n= ns-j;
          double t=(n-1.0)/n;
          ml+=(S-t*S)*deaths[j];
          S*=t;
        }
      }
      datas[i].lifetime=ml;
      IMP_LOG(VERBOSE, "Expected life of " << datas[i].slack
              << " is " << datas[i].lifetime << std::endl);
    }

    /**
       C(s) is cost to compute
       R(s) is const to eval restraints
       L(s) is lifetime of slack
       minimize C(s)/L(s)+R(s)
    */
    // smooth
    for (unsigned int i=1; i< datas.size()-1; ++i) {
      datas[i].rcost=(datas[i].rcost+ datas[i-1].rcost+datas[i+1].rcost)/3.0;
      datas[i].ccost=(datas[i].ccost+ datas[i-1].ccost+datas[i+1].ccost)/3.0;
      datas[i].lifetime=(datas[i].lifetime
                      + datas[i-1].lifetime+datas[i+1].lifetime)/3.0;
    }
    double min= std::numeric_limits<double>::max();
    for (unsigned int i=0; i< datas.size(); ++i) {
      double v= datas[i].rcost+ datas[i].ccost/datas[i].lifetime;
      IMP_LOG(VERBOSE, "Cost of " << datas[i].slack << " is " << v
              << " from " << datas[i].rcost
              << " " << datas[i].ccost << " " << datas[i].lifetime
              << std::endl);
      if (v < min) {
        min=v;
        opt_i=i;
      }
    }
    last_ns=ns;
    ns*=2;
    IMP_LOG(VERBOSE, "Opt is " << datas[opt_i].slack << std::endl);
    // 2 for the value, 2 for the doubling
    // if it more than 1000, just decide that is enough
  } while (datas[opt_i].lifetime > ns/4.0 && ns <1000);
  return datas[opt_i].slack;
}
示例#27
0
文件: pdb.cpp 项目: salilab/imp
void write_pdb(const ParticlesTemp& ps, TextOutput out) {
  IMP_FUNCTION_LOG;
  int last_index = 0;
  bool use_input_index = true;
  for (unsigned int i = 0; i < ps.size(); ++i) {
    if (Atom(ps[i]).get_input_index() != last_index + 1) {
      use_input_index = false;
      break;
    } else {
      ++last_index;
    }
  }
  for (unsigned int i = 0; i < ps.size(); ++i) {
    if (Atom::get_is_setup(ps[i])) {
      Atom ad(ps[i]);
      Residue rd = get_residue(ad);
      // really dumb and slow, fix later
      char chain;
      Chain c = get_chain(rd);
      if (c) {
        chain = c.get_id()[0];
      } else {
        chain = ' ';
      }
      int inum = i+1;
      if (i>=99999) inum=99999;
      out.get_stream() << get_pdb_string(
                              core::XYZ(ps[i]).get_coordinates(),
                              use_input_index ? ad.get_input_index()
                                              : static_cast<int>(inum),
                              ad.get_atom_type(), rd.get_residue_type(), chain,
                              rd.get_index(), rd.get_insertion_code(),
                              ad.get_occupancy(), ad.get_temperature_factor(),
                              ad.get_element());

      if (!out) {
        IMP_THROW("Error writing to file in write_pdb", IOException);
      }
    }
    else if (Residue::get_is_setup(ps[i])) {    // if C-alpha residue is available
      Residue rd = IMP::atom::Residue(ps[i]);
      // comment 1 by SJ - TODO: How to retrieve the correct chain information without an hierarchy?
      char chain;
      Chain c = get_chain(rd);
      if (c) {
        chain = c.get_id()[0];
      } else {
        chain = ' ';
      }

      // comment 2 by SJ - TODO: The C-alpha residues are not sorted yet. We need to sort the residues similarly to what PMI does.
      out.get_stream() << get_pdb_string(
                              core::XYZ(ps[i]).get_coordinates(),
                              static_cast<int>(i + 1),
                              IMP::atom::AT_CA, rd.get_residue_type(), chain,
                              rd.get_index(), ' ',
                              1.0, IMP::core::XYZR(ps[i]).get_radius());
    }
    else {  // if a coarse-grained BEAD is available
      Ints resindexes = IMP::atom::Fragment(ps[i]).get_residue_indexes();
      int resindex = (int)resindexes.front() + (int)(resindexes.size()/2);
      // comment 1 by SJ - TODO: How to retrieve the correct chain information without an hierarchy?
      char chain = ' ';

      // comment 3 by SJ - TODO: The BEADs are not sorted yet. We need to sort the residues similarly to what PMI does.
      // comment 4 by SJ - TODO: currently IMP does not allow "BEA" as a residue name, while PMI allows it. Thus "UNK" was used instead.
      out.get_stream() << get_pdb_string(
                              core::XYZ(ps[i]).get_coordinates(),
                              static_cast<int>(i + 1),
                              IMP::atom::AT_CA, IMP::atom::UNK, chain,
                              (int)resindex, ' ',
                              1.0, IMP::core::XYZR(ps[i]).get_radius());
    }
  }
}
示例#28
0
SubsetGraph get_restraint_graph(ScoringFunctionAdaptor in,
                                const ParticleStatesTable *pst) {
  RestraintsTemp rs =
      IMP::create_decomposition(in->create_restraints());
  // ScoreStatesTemp ss= get_required_score_states(rs);
  SubsetGraph ret(rs.size());  // + ss.size());
  IMP_LOG_TERSE("Creating restraint graph on " << rs.size() << " restraints."
                                               << std::endl);
  boost::unordered_map<Particle *, int> map;
  SubsetGraphVertexName pm = boost::get(boost::vertex_name, ret);
  DependencyGraph dg = get_dependency_graph(rs[0]->get_model());
  DependencyGraphVertexIndex index = IMP::get_vertex_index(dg);
  /*IMP_IF_LOG(VERBOSE) {
    IMP_LOG_VERBOSE( "dependency graph is \n");
    IMP::internal::show_as_graphviz(dg, std::cout);
    }*/
  Subset ps = pst->get_subset();
  for (unsigned int i = 0; i < ps.size(); ++i) {
    ParticlesTemp t = get_dependent_particles(
        ps[i], ParticlesTemp(ps.begin(), ps.end()), dg, index);
    for (unsigned int j = 0; j < t.size(); ++j) {
      IMP_USAGE_CHECK(map.find(t[j]) == map.end(),
                      "Currently particles which depend on more "
                          << "than one particle "
                          << "from the input set are not supported."
                          << "  Particle \"" << t[j]->get_name()
                          << "\" depends on \"" << ps[i]->get_name()
                          << "\" and \""
                          << ps[map.find(t[j])->second]->get_name() << "\"");
      map[t[j]] = i;
    }
    IMP_IF_LOG(VERBOSE) {
      IMP_LOG_VERBOSE("Particle \"" << ps[i]->get_name() << "\" controls ");
      for (unsigned int i = 0; i < t.size(); ++i) {
        IMP_LOG_VERBOSE("\"" << t[i]->get_name() << "\" ");
      }
      IMP_LOG_VERBOSE(std::endl);
    }
  }
  for (unsigned int i = 0; i < rs.size(); ++i) {
    ParticlesTemp pl = IMP::get_input_particles(rs[i]->get_inputs());
    std::sort(pl.begin(), pl.end());
    pl.erase(std::unique(pl.begin(), pl.end()), pl.end());
    Subset os(pl);
    for (unsigned int j = 0; j < pl.size(); ++j) {
      pl[j] = ps[map[pl[j]]];
    }
    std::sort(pl.begin(), pl.end());
    pl.erase(std::unique(pl.begin(), pl.end()), pl.end());
    Subset s(pl);
    IMP_LOG_VERBOSE("Subset for restraint " << rs[i]->get_name() << " is " << s
                                            << " from " << os << std::endl);
    pm[i] = s;
  }
  /*ScoreStatesTemp ss= get_required_score_states(rs);
    for (ScoreStatesTemp::const_iterator it= ss.begin();
    it != ss.end(); ++it) {
    ParticlesTemp pl= (*it)->get_input_particles();
    add_edges(ps, pl, map, *it, ret);
    ParticlesTemp opl= (*it)->get_output_particles();
    add_edges(ps, opl, map, *it, ret);
    }
    IMP_INTERNAL_CHECK(boost::num_vertices(ret) == ps.size(),
    "Wrong number of vertices "
    << boost::num_vertices(ret)
    << " vs " << ps.size());*/
  for (unsigned int i = 0; i < boost::num_vertices(ret); ++i) {
    for (unsigned int j = 0; j < i; ++j) {
      if (get_intersection(pm[i], pm[j]).size() > 0) {
        boost::add_edge(i, j, ret);
        IMP_LOG_VERBOSE("Connecting " << rs[i]->get_name() << " with "
                                      << rs[j]->get_name() << std::endl);
      }
    }
  }
  return ret;
}