lib/MolecularFeaturizer.cpp
// Transfer atomic features into molecular features
Transfer atomic features into molecular features
Transfer atomic features into molecular features
bool MolecularFeaturizer(
    Molecule* molall,
    Molecule* mol2all,
    std::vector<std::vector<double> >& features,
    std::vector<std::vector<double> >& mf_traj,
    string fname,
    const std::vector<string>& atmTypes,
    const std::vector<string>& SYBYL,
    bool verbose)
{
  Molecule* mol = molall->clone();
  Molecule* mol2 = mol2all->clone();
  cout << "Generating molecular features..." << endl;
  for (int k = 0; k < mol2->getNAtom(); k ++){
    if ((mol->getAtom(k)->getResName() != mol2->getAtom(k)->getResName()) || (mol->getAtom(k)->getAtmName() != mol2->getAtom(k)->getAtmName())){
      cout << "Mol2 atoms do not match with selected molecule!" << endl;
	  if ((mol->getAtom(k)->getResName() != mol2->getAtom(k)->getResName())){
		cout << "Residue name mismatch at atom " << k << ": " << mol->getAtom(k)->getResName() << " and " << mol2->getAtom(k)->getResName() << endl;
	  }
	  if ((mol->getAtom(k)->getAtmName() != mol2->getAtom(k)->getAtmName())){
		cout << "Atom name mismatch at atom " << k << ": " << mol->getAtom(k)->getAtmName() << " and " << mol2->getAtom(k)->getAtmName() << endl;
	  }
      return false;
    }
  }
  if (mol2->getNAtom() != mol->getNAtom() ){
    cout << "mol2 size " << mol2->getNAtom() << " does not match mol size " << mol->getNAtom() << endl;
  }
  unsigned int i, j, k;
  vector<vector<double> > transfeatures(features[0].size(),
                                  vector<double>(features.size()));
  for (i = 0; i < features.size(); ++i){
    for (j = 0; j < features[0].size(); ++j){
      transfeatures[j][i] = features[i][j];
    }
  }
  std::vector<std::vector<double> > mol_features;
  std::vector<double> mfeats;
  mfeats.clear();
  int Natom = atmTypes.size();
  for (i=0; i< SYBYL.size(); i++){
    mol_features.push_back(std::vector<double> (transfeatures[0].size(), 0.0));
  }
  for (i=0; i< SYBYL.size(); i++){
    for (j=0; j< Natom; j++){
      if (atmTypes[j] == SYBYL[i]){
        for (k=0; k < mol_features[0].size(); k++){
          mol_features[i][k] += transfeatures[j][k];
        }
      }
    }
  }
  for (i = 0; i < mol_features.size(); i ++){
    for (j = 0; j < mol_features[0].size(); j ++){
      mfeats.push_back(mol_features[i][j]);
    }
  }
  mf_traj.push_back(mfeats);
  if (verbose){
    ofstream outFile;
    outFile.open(fname.c_str());
    for (i = 0; i < mol_features.size(); i ++){
      for (j = 0; j < mol_features[0].size(); j ++){
        outFile << mol_features[i][j] << " ";
      }
    }
  }
  cout << "Molecular features done." << endl;
  return true;
}
bool MolecularFeaturizer ( Molecule * molall , Molecule * mol2all , std :: vector < std :: vector < double > > & features , std :: vector < std :: vector < double > > & mf_traj , string fname , const std :: vector < string > & atmTypes , const std :: vector < string > & SYBYL , bool verbose ) { Molecule * mol = molall -> clone ( ) ; Molecule * mol2 = mol2all -> clone ( ) ; cout << " " << endl ; for ( int k = 0 ; k < mol2 -> getNAtom ( ) ; k ++ ) { if ( ( mol -> getAtom ( k ) -> getResName ( ) != mol2 -> getAtom ( k ) -> getResName ( ) ) || ( mol -> getAtom ( k ) -> getAtmName ( ) != mol2 -> getAtom ( k ) -> getAtmName ( ) ) ) { cout << " " << endl ; if ( ( mol -> getAtom ( k ) -> getResName ( ) != mol2 -> getAtom ( k ) -> getResName ( ) ) ) { cout << " " << k << " " << mol -> getAtom ( k ) -> getResName ( ) << " " << mol2 -> getAtom ( k ) -> getResName ( ) << endl ; } if ( ( mol -> getAtom ( k ) -> getAtmName ( ) != mol2 -> getAtom ( k ) -> getAtmName ( ) ) ) { cout << " " << k << " " << mol -> getAtom ( k ) -> getAtmName ( ) << " " << mol2 -> getAtom ( k ) -> getAtmName ( ) << endl ; } return false ; } } if ( mol2 -> getNAtom ( ) != mol -> getNAtom ( ) ) { cout << " " << mol2 -> getNAtom ( ) << " " << mol -> getNAtom ( ) << endl ; } unsigned int i , j , k ; vector < vector < double > > transfeatures ( features [ 0 ] . size ( ) , vector < double > ( features . size ( ) ) ) ; for ( i = 0 ; i < features . size ( ) ; ++ i ) { for ( j = 0 ; j < features [ 0 ] . size ( ) ; ++ j ) { transfeatures [ j ] [ i ] = features [ i ] [ j ] ; } } std :: vector < std :: vector < double > > mol_features ; std :: vector < double > mfeats ; mfeats . clear ( ) ; int Natom = atmTypes . size ( ) ; for ( i = 0 ; i < SYBYL . size ( ) ; i ++ ) { mol_features . push_back ( std :: vector < double > ( transfeatures [ 0 ] . size ( ) , 0.0 ) ) ; } for ( i = 0 ; i < SYBYL . size ( ) ; i ++ ) { for ( j = 0 ; j < Natom ; j ++ ) { if ( atmTypes [ j ] == SYBYL [ i ] ) { for ( k = 0 ; k < mol_features [ 0 ] . size ( ) ; k ++ ) { mol_features [ i ] [ k ] += transfeatures [ j ] [ k ] ; } } } } for ( i = 0 ; i < mol_features . size ( ) ; i ++ ) { for ( j = 0 ; j < mol_features [ 0 ] . size ( ) ; j ++ ) { mfeats . push_back ( mol_features [ i ] [ j ] ) ; } } mf_traj . push_back ( mfeats ) ; if ( verbose ) { ofstream outFile ; outFile . open ( fname . c_str ( ) ) ; for ( i = 0 ; i < mol_features . size ( ) ; i ++ ) { for ( j = 0 ; j < mol_features [ 0 ] . size ( ) ; j ++ ) { outFile << mol_features [ i ] [ j ] << " " ; } } } cout << " " << endl ; return true ; }
Transfer atomic features into molecular features
Transfer atomic features into molecular features
// transpose features // define Natom = Number of atoms // initialize mol_features to all zeros // add features to mol_features if atmType match // open file to write