Exemple #1
0
  int ReadPQS_geom(istream &ifs, OBMol &mol, const char *title,
                   int input_style, double bohr_to_angstrom)
  {
    int atom_count=0;
    double x, y, z;
    char buffer[BUFF_SIZE];
    string str;
    OBAtom *atom;
    vector<string> vs;

    mol.Clear();
    mol.BeginModify();

    while (ifs.getline(buffer,BUFF_SIZE) && !card_found(buffer))
      {
        if (buffer[0]!='$')
          {
            tokenize(vs, buffer);
            if (vs.size() < 1) return false; // timvdm 18/06/2008
            atom=mol.NewAtom();
            str=vs[0];
            if (input_style==0)
              {
                if (vs.size() < 4) return false; // timvdm 18/06/2008
                atom->SetAtomicNum(OBElements::GetAtomicNum(str.c_str()));
                x=atof((char*) vs[1].c_str())*bohr_to_angstrom;
                y=atof((char*) vs[2].c_str())*bohr_to_angstrom;
                z=atof((char*) vs[3].c_str())*bohr_to_angstrom;
              }
            else
              {
                if (vs.size() < 5) return false; // timvdm 18/06/2008
                str.replace (0,2,"");
                atom->SetAtomicNum(OBElements::GetAtomicNum(str.c_str()));
                x=atof((char*) vs[2].c_str())*bohr_to_angstrom;
                y=atof((char*) vs[3].c_str())*bohr_to_angstrom;
                z=atof((char*) vs[4].c_str())*bohr_to_angstrom;
              }
            atom->SetVector(x, y, z);
            atom_count++;
          }
      }

    mol.ConnectTheDots();
    mol.PerceiveBondOrders();

    mol.EndModify();
    mol.SetTitle(title);

    return atom_count;
  }
/* FUNCTION: basicExportTest */
void OpenBabelImportExportTest::basicExportTest() {

	// Create a water molecule for frame 0
	//
	int frameSetId = entityManager->addFrameSet();
	int frameIndex = entityManager->addFrame(frameSetId);
	NXMoleculeSet* rootMoleculeSet =
		entityManager->getRootMoleculeSet(frameSetId, frameIndex);
	OBMol* molecule = rootMoleculeSet->newMolecule();
	OBAtom* atomO = molecule->NewAtom();
	atomO->SetAtomicNum(etab.GetAtomicNum("O")); // Oxygen
	atomO->SetVector(0.00000000, 0.00000000, 0.37000000); // Angstroms
	OBAtom* atomH1 = molecule->NewAtom();
	atomH1->SetAtomicNum(etab.GetAtomicNum("H")); // Hydrogen
	atomH1->SetVector(0.78000000, 0.00000000, -0.18000000);
	OBAtom* atomH2 = molecule->NewAtom();
	atomH2->SetAtomicNum(etab.GetAtomicNum("H")); // Hydrogen
	atomH2->SetVector(-0.78000000, 0.00000000, -0.18000000);
	OBBond* bond = molecule->NewBond();
	bond->SetBegin(atomO);
	bond->SetEnd(atomH1);
	bond = molecule->NewBond();
	bond->SetBegin(atomO);
	bond->SetEnd(atomH2);
	
	// Write it with the OpenBabelImportExport plugin
	NXCommandResult* commandResult =
		entityManager->exportToFile("testOpenBabel.pdb", frameSetId, -1);
	if (commandResult->getResult() != NX_CMD_SUCCESS)
		printf("\n%s\n", qPrintable(GetNV1ResultCodeString(commandResult)));
	CPPUNIT_ASSERT(commandResult->getResult() == NX_CMD_SUCCESS);
}
Exemple #3
0
bool ReadFeat(istream &ifs,OBMol &mol, const char *title)
{
  char buffer[BUFF_SIZE];
  int i,natoms;

  ifs.getline(buffer,BUFF_SIZE);
  sscanf(buffer,"%d",&natoms);

  mol.ReserveAtoms(natoms);
  mol.BeginModify();

  if (!ifs.getline(buffer,BUFF_SIZE)) return(false);
  mol.SetTitle(buffer);

  double x,y,z;
  char type[20];
  OBAtom *atom;
  for (i = 0; i < natoms;i++)
  {
    if (!ifs.getline(buffer,BUFF_SIZE)) return(false);
    sscanf(buffer,"%s %lf %lf %lf",
	   type,
	   &x,
	   &y,
	   &z);
    CleanAtomType(type);
    atom = mol.NewAtom();
    atom->SetVector(x,y,z);
    atom->SetAtomicNum(etab.GetAtomicNum(type));
  }

  mol.EndModify();
  return(true);
}
bool PubChemFormat::EndElement(const string& name)
{
  unsigned int i;
	if(name=="PC-Atoms")
	{
		for(i=0;i<AtNum.size();++i)
		{
			OBAtom* pAtom = _pmol->NewAtom();
			pAtom->SetAtomicNum(AtNum[i]);
		}
	}
	else if(name=="PC-Bonds")
	{
		for(i=0;i<BondBeginAtIndx.size();++i)
			_pmol->AddBond(BondBeginAtIndx[i],BondEndAtIndx[i],BondOrder[i]);
	}
	else if(name=="PC-Conformer")
	{
		++ConformerIndx;
		if(Coordz.size()!=Coordx.size())
			Coordz.resize(Coordx.size());
		for(i=0;i<CoordIndx.size();++i)
		{
			OBAtom* pAtom = _pmol->GetAtom(CoordIndx[i]);
			pAtom->SetVector(Coordx[i],Coordy[i],Coordz[i]);
		}
	}
	else if(name=="PC-Compound") //this is the end of the molecule we are extracting
	{
		_pmol->EndModify();
		return false;//means stop parsing
	}
	return true;
}
 /**
 Method reads coordinates from input stream (ifs) and
 writes it into supplied OBMol object (molecule).
 Input stream must be set to begining of coordinates
 table in nwo file. (Line after "Output coordinates...")
 Stream will be set at next line after geometry table.
 If one of input arguments is NULL method returns without
 any changes
 If "molecule" already contain geometry - method will write
 new geometry as conformer.
 If "molecule" contain geometry which incompatible with read
 data method returns without changes.
 */
 void NWChemOutputFormat::ReadCoordinates(istream* ifs, OBMol* molecule)
 {
   if ((molecule == NULL) || (ifs == NULL))
       return;
   vector<string> vs;
   char buffer[BUFF_SIZE];
   double x, y, z;
   unsigned int natoms = molecule->NumAtoms();
   bool from_scratch = false;
   double* coordinates;
   if (natoms == 0)
       from_scratch = true;
   else
       coordinates = new double[3*natoms];
   ifs->getline(buffer,BUFF_SIZE);	// blank
   ifs->getline(buffer,BUFF_SIZE);	// column headings
   ifs->getline(buffer,BUFF_SIZE);	// ---- ----- ----
   ifs->getline(buffer,BUFF_SIZE);
   tokenize(vs,buffer);
   unsigned int i=0;
   while (vs.size() == 6)
   {
       x = atof((char*)vs[3].c_str());
       y = atof((char*)vs[4].c_str());
       z = atof((char*)vs[5].c_str());
       if (from_scratch)
       {
           // set atomic number
           OBAtom* atom = molecule->NewAtom();
           atom->SetAtomicNum(atoi(vs[2].c_str()));
           atom->SetVector(x,y,z);
       }
       else
       {
           // check atomic number
           if ((i>=natoms) || (molecule->GetAtom(i+1)->GetAtomicNum() != atoi(vs[2].c_str())))
           {
               delete[] coordinates;
               return;
           }
           coordinates[i*3] = x;
           coordinates[i*3+1] = y;
           coordinates[i*3+2] = z;
           i++;
       }
       if (!ifs->getline(buffer,BUFF_SIZE))
         break;
       tokenize(vs,buffer);
   }
   if (from_scratch) 
   {
       return;
   }
   if (i != natoms) {
       delete[] coordinates;
       return;
   }
   molecule->AddConformer(coordinates);
 }
Exemple #6
0
  /**
  Method reads partial charges from input stream (ifs)
  and writes them to supplied OBMol object (molecule)
  Input stream must be set to begining of charges
  table in nwo file. (Line after "Mulliken analysis of the total density")
  Stream will be set at next line after charges table.
  If reading charges failed or "molecule" contains
  data incompatible with read charges then "molecule"
  wont be changed.
  */
  void NWChemOutputFormat::ReadPartialCharges(istream* ifs, OBMol* molecule)
  {
    if ((molecule == NULL) || (ifs == NULL))
        return;
    vector<string> vs;
    char buffer[BUFF_SIZE];
    bool from_scratch = false;
    vector<int> charges;
    vector<double> partial_charges;
    unsigned int natoms = molecule->NumAtoms();

    if (natoms == 0)
        from_scratch = true;
    ifs->getline(buffer,BUFF_SIZE); // ---- ----- ----
    ifs->getline(buffer,BUFF_SIZE);	// blank
    ifs->getline(buffer,BUFF_SIZE);	// column headings
    ifs->getline(buffer,BUFF_SIZE);	// ---- ----- ----
    ifs->getline(buffer,BUFF_SIZE);
    tokenize(vs, buffer);

    // N Symbol    Charge     PartialCharge+Charge   ShellCharges
    // 0   1          2                3                4,etc
    unsigned int i = 1;
    while (vs.size() >= 4)
    {
        int charge = atoi(vs[2].c_str());
        if (!from_scratch)
        {
            if (i > natoms)
                return;
            if (molecule->GetAtom(i++)->GetAtomicNum() != charge)
                return;
        }
        else
            charges.push_back(charge);
        partial_charges.push_back(atof(vs[3].c_str()) - charge);
        ifs->getline(buffer,BUFF_SIZE);
        tokenize(vs, buffer);
    }

    if (from_scratch)
        molecule->ReserveAtoms(partial_charges.size());
    else if (partial_charges.size() != natoms)
        return;
    for(unsigned int j=0;j<partial_charges.size();j++)
    {
        OBAtom* atom;
        if (from_scratch)
        {
            atom = molecule->NewAtom();
            atom->SetAtomicNum(charges[j]);
        }
        else
        {
            atom = molecule->GetAtom(j+1);
        }
        atom->SetPartialCharge(partial_charges[j]);
    }
  }
Exemple #7
0
bool AmberPrepFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
{

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
        return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    char buffer[BUFF_SIZE];
    string str,str1;
    OBAtom *atom;
    OBInternalCoord *coord;
    vector<string> vs;
    vector<OBInternalCoord*> internals;

    mol.BeginModify();

    while	(ifs.getline(buffer,BUFF_SIZE))
    {
        tokenize(vs,buffer);
        if (vs.size() == 10)
        {
            atom = mol.NewAtom();
            coord = new OBInternalCoord();
            if (mol.NumAtoms() > 1)
                coord->_a = mol.GetAtom(atoi(vs[4].c_str()));
            if (mol.NumAtoms() > 2)
                coord->_b = mol.GetAtom(atoi(vs[5].c_str()));
            if (mol.NumAtoms() > 3)
                coord->_c = mol.GetAtom(atoi(vs[6].c_str()));
            coord->_dst = atof(vs[7].c_str());
            coord->_ang = atof(vs[8].c_str());
            coord->_tor = atof(vs[9].c_str());
            internals.push_back(coord);

            atom->SetAtomicNum(etab.GetAtomicNum(vs[1].c_str()));

            if (!ifs.getline(buffer,BUFF_SIZE))
                break;
            tokenize(vs,buffer);
        }
    }

    if (internals.size() > 0)
      InternalToCartesian(internals,mol);

    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    mol.EndModify();
    mol.SetTitle(title);
    return(true);
}
Exemple #8
0
  bool BallStickFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    int i,natoms;
    char buffer[BUFF_SIZE];

    if (!ifs.getline(buffer,BUFF_SIZE))
      return(false);
    if (!ifs.getline(buffer,BUFF_SIZE))
      return(false);
    sscanf(buffer,"%d",&natoms);
    mol.ReserveAtoms(natoms);
    mol.BeginModify();

    double x,y,z;
    OBAtom *atom;
    vector<string> vs;
    vector<string>::iterator j;

    for (i = 1; i <= natoms;i ++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        tokenize(vs,buffer);
        if (vs.size() < 4)
          return(false);
        if (vs[0].size() > 1)
          vs[0][1] = tolower(vs[0][1]);
        atom = mol.NewAtom();
        x = atof((char*)vs[1].c_str());
        y = atof((char*)vs[2].c_str());
        z = atof((char*)vs[3].c_str());
        atom->SetVector(x,y,z); //set coordinates
        atom->SetAtomicNum(etab.GetAtomicNum(vs[0].c_str()));

        for (j = vs.begin()+4;j != vs.end();j++)
          mol.AddBond(atom->GetIdx(),atoi((char*)j->c_str()),1);
      }
    
    // clean out any remaining blank lines
    while(ifs.peek() != EOF && ifs.good() && 
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
      ifs.getline(buffer,BUFF_SIZE);

    mol.EndModify();
    mol.SetTitle(title);
    return(true);
  }
Exemple #9
0
bool ReadAlchemy(istream &ifs,OBMol &mol, const char *title)
{
  int i;
  int natoms,nbonds;
  char buffer[BUFF_SIZE];

  ifs.getline(buffer,BUFF_SIZE);
  sscanf(buffer,"%d %*s %d", &natoms, &nbonds);
  if (!natoms) return(false);

  mol.ReserveAtoms(natoms);
  ttab.SetFromType("ALC");

  string str;
  double x,y,z;
  OBAtom *atom;
  vector<string> vs;

  for (i = 1; i <= natoms; i ++)
  {
    if (!ifs.getline(buffer,BUFF_SIZE)) return(false);
    tokenize(vs,buffer);
    if (vs.size() != 6) return(false);
    atom = mol.NewAtom();
    x = atof((char*)vs[2].c_str());
    y = atof((char*)vs[3].c_str());
    z = atof((char*)vs[4].c_str());
    atom->SetVector(x,y,z); //set coordinates

    //set atomic number
    ttab.SetToType("ATN"); ttab.Translate(str,vs[1]);
    atom->SetAtomicNum(atoi(str.c_str()));
    //set type
    ttab.SetToType("INT"); ttab.Translate(str,vs[1]); 
    atom->SetType(str);
  }

  char bobuf[100];
  string bostr;
  int bgn,end,order;

  for (i = 0; i < nbonds; i++)
  {
    if (!ifs.getline(buffer,BUFF_SIZE)) return(false);
    sscanf(buffer,"%*d%d%d%s",&bgn,&end,bobuf);
    bostr = bobuf; order = 1;
    if      (bostr == "DOUBLE")   order = 2;
    else if (bostr == "TRIPLE")   order = 3;
    else if (bostr == "AROMATIC") order = 5;
    mol.AddBond(bgn,end,order);
  }

  mol.SetTitle(title);
  return(true);
}
Exemple #10
0
//------------------------------------------------------------------------------
bool OBMoldenFormat::ReadMolecule( OBBase* pOb, OBConversion* pConv )
{
    OBMol* pmol = dynamic_cast< OBMol* >(pOb);
    if( pmol == 0 ) return false;

    istream& ifs = *pConv->GetInStream();

    pmol->BeginModify();
    pmol->SetDimension( 3 );
    string lineBuffer;
    getline( ifs, lineBuffer );

    while( ifs && lineBuffer.find( "[Atoms]" ) == string::npos 
    	   && lineBuffer.find( "[ATOMS]" ) == string::npos )
    {
        getline( ifs, lineBuffer );
    }

    if( !ifs ) return false;

    //[Atoms] AU OR Angs
    double factor = 1.; // Angstrom
    if( lineBuffer.find( "AU" ) != string::npos ) factor = 0.529177249; // Bohr

    while( ifs )
    {
        getline( ifs, lineBuffer );
        if( lineBuffer.size() == 0 ) continue;
        if( lineBuffer.find( '[' ) != string::npos ) break;
        istringstream is( lineBuffer );
        string atomName;
        int atomId;
        int atomicNumber;
        double x, y, z;
        is >> atomName >> atomId >> atomicNumber >> x >> y >> z;
        OBAtom* atom = pmol->NewAtom();
        if( !atom ) break;
        atom->SetAtomicNum( atomicNumber );
        atom->SetVector( x * factor, y * factor, z * factor );
    }

    if( !pConv->IsOption( "b", OBConversion::INOPTIONS ) ) pmol->ConnectTheDots();
    if (!pConv->IsOption( "s", OBConversion::INOPTIONS )
        && !pConv->IsOption( "b", OBConversion::INOPTIONS ) )
    {
        pmol->PerceiveBondOrders();
    }
    pmol->EndModify();

    return true;
}
Exemple #11
0
bool ReadBiosymCAR(istream &ifs,OBMol &mol, const char *title)
{
  char buffer[BUFF_SIZE];
  string str;
  double x,y,z;
  OBAtom *atom;
  vector<string> vs;

  mol.BeginModify();

  while	(ifs.getline(buffer,BUFF_SIZE))
    {
      if(strstr(buffer,"PBC") != NULL)
	{
	  if(strstr(buffer,"ON") != NULL)
	    {
	      ifs.getline(buffer,BUFF_SIZE);
	      ifs.getline(buffer,BUFF_SIZE);
	      ifs.getline(buffer,BUFF_SIZE);
	    }
	  else
	    {
	      ifs.getline(buffer,BUFF_SIZE);
	      ifs.getline(buffer,BUFF_SIZE);
	    }
	  break;
	}
    }

  while	(ifs.getline(buffer,BUFF_SIZE))
    {
      if(strstr(buffer,"end") != NULL)
	break;

      atom = mol.NewAtom();

      tokenize(vs,buffer);
      atom->SetAtomicNum(etab.GetAtomicNum(vs[7].c_str()));
      x = atof((char*)vs[1].c_str());
      y = atof((char*)vs[2].c_str());
      z = atof((char*)vs[3].c_str());
      atom->SetVector(x,y,z);
    }
  mol.EndModify();

  mol.ConnectTheDots();
  mol.PerceiveBondOrders();
  mol.SetTitle(title);
  return(true);
}
Exemple #12
0
bool ReadHIN(istream &ifs,OBMol &mol, const char *title)
{ 
  // Right now only read in the first molecule
  int i;
  int max, bo;
  char buffer[BUFF_SIZE];
  string str,str1;
  double x,y,z;
  OBAtom *atom;
  vector<string> vs;

  ifs.getline(buffer, BUFF_SIZE);
  while (strstr(buffer,"mol") == NULL)
    ifs.getline(buffer, BUFF_SIZE);
  ifs.getline(buffer, BUFF_SIZE);
  
  mol.BeginModify();
  while (strstr(buffer,"endmol") == NULL)
    {
      tokenize(vs,buffer); // Don't really know how long it'll be
      if (vs.size() < 11) break;
      atom = mol.NewAtom();
      atom->SetAtomicNum(etab.GetAtomicNum(vs[3].c_str()));
      x = atof((char*)vs[7].c_str());
      y = atof((char*)vs[8].c_str());
      z = atof((char*)vs[9].c_str());
      atom->SetVector(x,y,z);
      
      max = 11 + 2 * atoi((char *)vs[10].c_str());
      for (i = 11; i < max; i+=2)
	{
	  switch(((char*)vs[i+1].c_str())[0]) // First char in next token
	    {
	    case 's': bo = 1; break;
	    case 'd': bo = 2; break;
	    case 't': bo = 3; break;
	    case 'a': bo = 5; break;
	    default : bo = 1; break;
	    }
	  mol.AddBond(mol.NumAtoms(), atoi((char *)vs[i].c_str()), bo);
	}
      ifs.getline(buffer, BUFF_SIZE);
    }
  mol.EndModify();

  mol.SetTitle(title);
  return(true);
}
void testIdsAddAtom()
{
  OBMol mol;
  // add 5 atoms
  for (int i = 0; i < 5; ++i)
    mol.NewAtom();

  OBAtom a;
  a.SetAtomicNum(6);
  // add a sixth atom
  mol.AddAtom(a);

  OB_REQUIRE( mol.NumAtoms() == 6 );
  OB_REQUIRE( mol.GetAtomById(5) );
  OB_REQUIRE( mol.GetAtomById(5)->GetId() == 5 );
}
Exemple #14
0
void AliasData::DeleteExpandedAtoms(OBMol& mol)
{
  //The atom that carries the AliasData object remains as an Xx atom with no charge;
  //the others are deleted. All the attached hydrogens are also deleted.
  for(unsigned i=0;i<_expandedatoms.size();++i)
  {
    OBAtom* at = mol.GetAtomById(_expandedatoms[i]);
    if(!at)
      continue;
    mol.DeleteHydrogens(at);
    if(at->HasData(AliasDataType))
    {
      at->SetAtomicNum(0);
      at->SetFormalCharge(0);
      at->SetSpinMultiplicity(0);
    }
    else
      mol.DeleteAtom(at);
  }
  _expandedatoms.clear();
}
  bool MacroModFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* defaultTitle = pConv->GetTitle();

    // Get Title
    char buffer[BUFF_SIZE];
    int natoms;
    vector<vector<pair<int,int> > > connections;

    if (ifs.getline(buffer,BUFF_SIZE))
      {
        vector<string> vs;
        tokenize(vs,buffer," \n");

        if ( !vs.empty() && vs.size() > 0)
          sscanf(buffer,"%i%*s",&natoms);

        if (natoms == 0)
          return false;

        if ( !vs.empty() && vs.size() > 1)
          mol.SetTitle(vs[1]);
        else
          {
            string s = defaultTitle;
            mol.SetTitle(defaultTitle);
          }
      }
    else
      return(false);

    mol.BeginModify();
    mol.ReserveAtoms(natoms);
    connections.resize(natoms+1);

    /***********************************************************************/

    // Get Type Bonds, BondOrder, X, Y, Z

    double x,y,z;
    vector3 v;
    char temp_type[10];
    int i,j;
    double charge;
    OBAtom atom;

    ttab.SetFromType("MMD");
    for (i = 1; i <= natoms; i++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          break;

        int end[6], order[6];

        sscanf(buffer,"%9s%d%d%d%d%d%d%d%d%d%d%d%d%lf%lf%lf",
               temp_type,&end[0],&order[0],&end[1],&order[1],&end[2],&order[2],
               &end[3], &order[3], &end[4], &order[4], &end[5], &order[5],
               &x, &y, &z);

        pair<int,int> tmp;
        for ( j = 0 ; j <=5 ; j++ )
          {
            if ( end[j] > 0  && end[j] > i)
              {
                tmp.first = end[j];
                tmp.second = order[j];
                connections[i].push_back(tmp);
              }
          }

        v.SetX(x);
        v.SetY(y);
        v.SetZ(z);
        atom.SetVector(v);

        string str = temp_type,str1;
        ttab.SetToType("ATN");
        ttab.Translate(str1,str);
        atom.SetAtomicNum(atoi(str1.c_str()));
        ttab.SetToType("INT");
        ttab.Translate(str1,str);
        atom.SetType(str1);

        // stuff for optional fields

        buffer[109]='\0';
        sscanf(&buffer[101],"%lf", &charge);
        atom.SetPartialCharge(charge);
        mol.AddAtom(atom);
      }

    for (i = 1; i <= natoms; i++)
      for (j = 0; j < (signed)connections[i].size(); j++)
        mol.AddBond(i, connections[i][j].first, connections[i][j].second);

    mol.EndModify();

    OBBond *bond;
    vector<OBBond*>::iterator bi;
    for (bond = mol.BeginBond(bi);bond;bond = mol.NextBond(bi))
      if (bond->GetBO() == 5 && !bond->IsInRing())
        bond->SetBO(1);

    if ( natoms != (signed)mol.NumAtoms() )
      return(false);

    // clean out remaining blank lines
    while(ifs.peek() != EOF && ifs.good() &&
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
      ifs.getline(buffer,BUFF_SIZE);

    return(true);
  }
Exemple #16
0
  bool ABINITFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    char buffer[BUFF_SIZE];
    string str;
    vector<string> vs;

    OBAtom *atom;
    int natom=-1;
    vector<int> atomicNumbers, atomTypes;
    double x, y, z;
    vector<vector3> atomPositions;
    vector<double> energies;

    // Translation vectors (if present)
    // aka rprim
    vector3 translationVectors[3];
    double acell[3]; // scale of lattice vectors
    int numTranslationVectors = 0;
    int symmetryCode = 0;

    mol.BeginModify();

    while (ifs.getline(buffer,BUFF_SIZE))
      {
        // tokens are listed in alphabetical order for code clarity
        if (strstr(buffer, "acell")) {
          tokenize(vs, buffer);
          if (vs.size() < 4)
            continue; // invalid line

          // acell=  7.6967369631E+00  7.6967369631E+00  7.6967369631E+00
          for (int i = 0; i < 3; ++i) {
            acell[i] = atof(vs[i+1].c_str());
          }
        }
        // Sometimes Cartesian has lower-case letter
        else if 
          ((strstr(buffer, "artesian coordinates")) &&
           (strstr(buffer, "(xcart)")) &&
           (natom > -1) //be sure natom has already been read
           )
          {
          double unit = BOHR_TO_ANGSTROM;
          if (strstr(buffer, "ngstrom"))
            unit = 1.0; // no conversion needed
          for (int i=0;i<natom;i++)
            {
              ifs.getline(buffer,BUFF_SIZE);
              tokenize(vs, buffer);
              if (vs.size() != 3) {
                obErrorLog.ThrowError
                  (__FUNCTION__,
                   "error while parsing coordinates of atoms", obWarning);
                return(false);
              }
              x = atof(vs[0].c_str()) * unit;
              y = atof(vs[1].c_str()) * unit;
              z = atof(vs[2].c_str()) * unit;
              atomPositions.push_back(vector3(x, y, z));
              // get next line
            }
          }
        else if (strstr(buffer, "natom")) {
          tokenize(vs, buffer);
          if (vs.size() != 2)
            continue;
          natom = atoi(vs[1].c_str());
        }
        else if (strstr(buffer, "rprim")) {
          numTranslationVectors = 0;
          int column;
          for (int i = 0; i < 3; ++i) {
            tokenize(vs, buffer);
            if (vs.size() < 3)
              break;

            // first line, rprim takes up a token
            if (i == 0)
              column = 1;
            else
              column = 0;

            x = atof((char*)vs[column].c_str()) * BOHR_TO_ANGSTROM;
            y = atof((char*)vs[column+1].c_str()) * BOHR_TO_ANGSTROM;
            z = atof((char*)vs[column+2].c_str()) * BOHR_TO_ANGSTROM;
            translationVectors[numTranslationVectors++].Set(x, y,z);
            ifs.getline(buffer,BUFF_SIZE);
          }
        }
        else if (strstr(buffer, "Symmetries")) {
          tokenize(vs, buffer, "()");
          // Should be something like (#160)
          symmetryCode = atoi(vs[1].substr(1).c_str());
        }
        else if (strstr(buffer, "typat")) {
          tokenize(vs, buffer);
          atomTypes.clear();
          for (unsigned int i = 1; i < vs.size(); ++i) {
            atomTypes.push_back(atoi(vs[i].c_str()));
          }
        }
        else if (strstr(buffer, "znucl")) {
          tokenize(vs, buffer);
          // make sure znucl is first token
          if (vs[0] != "znucl")
            continue;
          // push back the remaining tokens into atomicNumbers
          atomicNumbers.clear();
          atomicNumbers.push_back(0); // abinit starts typat with 1
          for (unsigned int i = 1; i < vs.size(); ++i)
            atomicNumbers.push_back(int(atof(vs[i].c_str())));
        }
        // xangst
        // forces
      }

    for (int i = 0; i < natom; ++i) {
      atom = mol.NewAtom();
      //set atomic number
      int idx = atom->GetIdx();
      int type = atomTypes[idx - 1];
      atom->SetAtomicNum(atomicNumbers[type]);
      // we set the coordinates by conformers in another loop
    }

    mol.EndModify();

    int numConformers = atomPositions.size() / natom;
    for (int i = 0; i < numConformers; ++i) {
      double *coordinates = new double[natom * 3];
      for (int j = 0; j < natom; ++j) {
        vector3 currentPosition = atomPositions[i*natom + j];
        coordinates[j*3] = currentPosition.x();
        coordinates[j*3 + 1] = currentPosition.y();
        coordinates[j*3 + 2] = currentPosition.z();
      }
      mol.AddConformer(coordinates);
    }
    // Delete first conformer, created by EndModify, bunch of 0s
    mol.DeleteConformer(0);
    // Set geometry to last one
    mol.SetConformer(mol.NumConformers() - 1);

    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    // Attach unit cell translation vectors if found
    if (numTranslationVectors > 0) {
      OBUnitCell* uc = new OBUnitCell;
      uc->SetData(acell[0] * translationVectors[0], acell[1] * translationVectors[1], acell[2] * translationVectors[2]);
      uc->SetOrigin(fileformatInput);
      if (symmetryCode)
        uc->SetSpaceGroup(symmetryCode);
      mol.SetData(uc);
    }

    mol.SetTitle(title);
    return(true);
  }
Exemple #17
0
  bool JaguarOutputFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL) return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    char buffer[BUFF_SIZE];
    string str,str1;
    double x,y,z;
    unsigned int i;
    OBAtom *atom;
    vector<string> vs;

    mol.BeginModify();
    while (ifs.getline(buffer,BUFF_SIZE))
      {
        if (strstr(buffer,"Input geometry:") != NULL
            || strstr(buffer,"symmetrized geometry:") != NULL
            || strstr(buffer,"new geometry:") != NULL
            || strstr(buffer,"final geometry:") != NULL)
          {
            // mol.EndModify();
            mol.Clear();
            mol.BeginModify();
            ifs.getline(buffer,BUFF_SIZE);  //     angstroms
            ifs.getline(buffer,BUFF_SIZE);  // column headings
            ifs.getline(buffer,BUFF_SIZE);
            tokenize(vs,buffer);
            while (vs.size() == 4)
              {
                atom = mol.NewAtom();
                str = vs[0]; // Separate out the Symbol# into just Symbol ...
                for (i = 0;i < str.size();i++)
                  if (isdigit(str[i]))
                    str[i] = '\0';

                atom->SetAtomicNum(etab.GetAtomicNum(str.c_str()));
                x = atof((char*)vs[1].c_str());
                y = atof((char*)vs[2].c_str());
                z = atof((char*)vs[3].c_str());
                atom->SetVector(x,y,z);

                if (!ifs.getline(buffer,BUFF_SIZE)) break;
                tokenize(vs,buffer);
              }
          }
        if (strstr(buffer, "Atomic charges from electrostatic potential") != NULL)
          {
            mol.SetAutomaticPartialCharge(false);
            unsigned int chgcount=0;
            while (chgcount<mol.NumAtoms())
              {
                ifs.getline(buffer,BUFF_SIZE);  // blank line
                ifs.getline(buffer,BUFF_SIZE);  // header line
                ifs.getline(buffer,BUFF_SIZE);  // data line
                tokenize(vs,buffer);
                for (vector<string>::size_type icount=1;icount<vs.size();++icount)
                  {
                    chgcount=chgcount+1;
                    mol.GetAtom(chgcount)->SetPartialCharge(atof((char*)vs[icount].c_str()));
                  }
              }
          }
        else if(strstr(buffer,"Dipole Moments (Debye)") != NULL)
          {
            ifs.getline(buffer,BUFF_SIZE); // actual components   X ###  Y #### Z ###
            tokenize(vs,buffer);
            if (vs.size() >= 8) 
              {
                OBVectorData *dipoleMoment = new OBVectorData;
                dipoleMoment->SetAttribute("Dipole Moment");
                double x, y, z;
                x = atof(vs[1].c_str());
                y = atof(vs[3].c_str());
                z = atof(vs[5].c_str());
                dipoleMoment->SetData(x, y, z);
                dipoleMoment->SetOrigin(fileformatInput);
                mol.SetData(dipoleMoment);
              }
            if (!ifs.getline(buffer,BUFF_SIZE)) break;
          }
      }

    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) 
        && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    mol.EndModify();
    mol.SetTitle(title);
    return(true);
  }
Exemple #18
0
  bool BGFFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    mol.SetTitle( pConv->GetTitle()); //default title is the filename
    mol.BeginModify();

    char buffer[BUFF_SIZE];
    char tmp[16],tmptyp[16];
    vector<string> vs;

    while (ifs.getline(buffer,BUFF_SIZE)) {
      if (EQn(buffer,"CRYSTX",6)) {
        // Parse unit cell
        tokenize(vs,buffer," \n\t,");
        if (vs.size() != 7)
          continue; // something strange

        double A, B, C, Alpha, Beta, Gamma;
        A = atof(vs[1].c_str());
        B = atof(vs[2].c_str());
        C = atof(vs[3].c_str());
        Alpha = atof(vs[4].c_str());
        Beta  = atof(vs[5].c_str());
        Gamma = atof(vs[6].c_str());
        OBUnitCell *uc = new OBUnitCell;
        uc->SetOrigin(fileformatInput);
        uc->SetData(A, B, C, Alpha, Beta, Gamma);
        mol.SetData(uc);
      } else if (EQn(buffer,"FORMAT",6))
        break;
    }

    ttab.SetFromType("DRE");
    ttab.SetToType("INT");
    OBAtom *atom;
    double x,y,z,chrg;
    for (;;)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          break;
        if (EQn(buffer,"FORMAT",6))
          break;

        sscanf(buffer,"%*s %*s %*s %*s %*s %*s %lf %lf %lf %15s %*s %*s %lf",
               &x,&y,&z,
               tmptyp,
               &chrg);
        atom = mol.NewAtom();

        ttab.Translate(tmp,tmptyp);
        atom->SetType(tmp);

        CleanAtomType(tmptyp);
        atom->SetAtomicNum(etab.GetAtomicNum(tmptyp));

        atom->SetVector(x,y,z);
      }
    unsigned int i;
    vector<int> vtmp;
    vector<vector<int> > vcon;
    vector<vector<int> > vord;

    for (i = 0; i < mol.NumAtoms();i++)
      {
        vcon.push_back(vtmp);
        vord.push_back(vtmp);
      }

    unsigned int bgn;
    for (;;)
      {
        if (!ifs.getline(buffer,BUFF_SIZE) || EQn(buffer,"END",3))
          break;

        tokenize(vs,buffer);
        if (vs.empty() || vs.size() < 3 || vs.size() > 10)
          continue;

        if (EQn(buffer,"CONECT",6))
          {
            bgn = atoi((char*)vs[1].c_str()) - 1;
            if (bgn < 1 || bgn > mol.NumAtoms())
              continue;
            for (i = 2;i < vs.size();i++)
              {
                vcon[bgn].push_back(atoi((char*)vs[i].c_str()));
                vord[bgn].push_back(1);
              }
          }
        else
          if (EQn(buffer,"ORDER",5))
            {
              bgn = atoi((char*)vs[1].c_str()) - 1;
              if (bgn < 1 || bgn > mol.NumAtoms())
                continue;
              if (vs.size() > vord[bgn].size()+2)
                continue;
              for (i = 2;i < vs.size();i++)
                vord[bgn][i-2] = atoi((char*)vs[i].c_str());
            }
      }

    unsigned int j;
    for (i = 1;i <= mol.NumAtoms();i++)
      if (!vcon[i - 1].empty())
        for (j = 0;j < vcon[i - 1].size();j++)
          {
            mol.AddBond(i,vcon[i - 1][j],vord[i - 1][j]);
          }

    //load up the next line after the END marker
    ifs.getline(buffer,BUFF_SIZE);

    mol.EndModify();
    return(true);
  }
Exemple #19
0
  bool XSFFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    char buffer[BUFF_SIZE];
    string str;
    double x,y,z;
    OBAtom *atom;
    vector3 translationVectors[3];
    int numTranslationVectors = 0;
    vector<string> vs;
    vector<vector3> atomPositions;
    bool createdAtoms = false;
    int atomicNum;

    mol.BeginModify();

    while (ifs.getline(buffer, BUFF_SIZE))
      {
        if (buffer[0] == '#')
          continue; // comment
        if (strstr(buffer, "ATOMS") != NULL) {
          // Minimum of 4 columns -- AtNum, x, y, z (forces)
          // where AtNum stands for atomic number (or symbol), while X Y Z are
          ifs.getline(buffer, BUFF_SIZE);
          tokenize(vs, buffer);
          while (vs.size() >= 4) {
            if (!createdAtoms) {
              atom = mol.NewAtom();
              //set atomic number
              atomicNum = OBElements::GetAtomicNum(vs[0].c_str());
              if (atomicNum == 0) {
                atomicNum = atoi(vs[0].c_str());
              }
              atom->SetAtomicNum(atomicNum);
            }
            x = atof((char*)vs[1].c_str());
            y = atof((char*)vs[2].c_str());
            z = atof((char*)vs[3].c_str());
            atomPositions.push_back(vector3(x, y, z)); // we may have a movie or animation

            ifs.getline(buffer, BUFF_SIZE);
            tokenize(vs, buffer);
          }
          createdAtoms = true; // don't run NewAtom() anymore
        }
        else if ( strstr(buffer, "PRIMVEC")
                 || strstr(buffer, "CONVVEC") ) {
          // translation vectors
          numTranslationVectors = 0; // if we have an animation
          while (numTranslationVectors < 3 && ifs.getline(buffer,BUFF_SIZE)) {
            tokenize(vs,buffer); // we really need to check that it's 3 entries only
            if (vs.size() < 3) return false; // timvdm 18/06/2008
            x = atof((char*)vs[0].c_str());
            y = atof((char*)vs[1].c_str());
            z = atof((char*)vs[2].c_str());
            translationVectors[numTranslationVectors++].Set(x, y, z);
          }
        }
        else if (strstr(buffer, "PRIMCOORD") != NULL) {
          // read the coordinates
          ifs.getline(buffer, BUFF_SIZE);
          tokenize(vs, buffer);
          if (vs.size() < 2) return false;
          int numAtoms = atoi(vs[0].c_str());
          for (int a = 0; a < numAtoms; ++a) {
            if (!ifs.getline(buffer,BUFF_SIZE))
              break;
            tokenize(vs,buffer);
            if (vs.size() < 4)
              break;

            if (!createdAtoms) {
              atom = mol.NewAtom();
              //set atomic number
              atomicNum = OBElements::GetAtomicNum(vs[0].c_str());
              if (atomicNum == 0) {
                atomicNum = atoi(vs[0].c_str());
              }
              atom->SetAtomicNum(atomicNum);
            }
            x = atof((char*)vs[1].c_str());
            y = atof((char*)vs[2].c_str());
            z = atof((char*)vs[3].c_str());
            atomPositions.push_back(vector3(x, y, z));
          }
        }
      }

    mol.EndModify();

    int natom = mol.NumAtoms();
    int numConformers = atomPositions.size() / natom;
    for (int i = 0; i < numConformers; ++i) {
      double *coordinates = new double[natom * 3];
      for (int j = 0; j < natom; ++j) {
        vector3 currentPosition = atomPositions[i*natom + j];
        coordinates[j*3] = currentPosition.x();
        coordinates[j*3 + 1] = currentPosition.y();
        coordinates[j*3 + 2] = currentPosition.z();
      }
      mol.AddConformer(coordinates);
    }
    // Delete first conformer, created by EndModify, bunch of 0s
    mol.DeleteConformer(0);
    // Set geometry to last one
    mol.SetConformer(mol.NumConformers() - 1);

    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    // Add final properties
    mol.SetTitle(title);
    if (numTranslationVectors == 3) {
      OBUnitCell *uc = new OBUnitCell;
      uc->SetOrigin(fileformatInput);
      uc->SetData(translationVectors[0],
                  translationVectors[1],
                  translationVectors[2]);
      mol.SetData(uc);
    }

    return(true);
  }
Exemple #20
0
  bool CARFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    bool hasPartialCharges = false;
    char buffer[BUFF_SIZE];
    string str;
    double x,y,z;
    OBAtom *atom;
    vector<string> vs;

    mol.BeginModify();

    while (ifs.getline(buffer,BUFF_SIZE))
      {
        if(strstr(buffer,"end") != NULL)
          {
            if (mol.NumAtoms() > 0) // we've already read in a molecule, so exit
              break;
            // else, we hit the end of the previous molecular system
            // (in a multimolecule file)
            ifs.getline(buffer,BUFF_SIZE); // title
            ifs.getline(buffer,BUFF_SIZE); // DATE
          }

        if (strncmp(buffer, "!BIOSYM", 7) == 0)
          {
            continue;
          }

        if(strstr(buffer,"PBC") != NULL)
          {
            if(strstr(buffer,"ON") != NULL)
              {
                ifs.getline(buffer,BUFF_SIZE); // title
                ifs.getline(buffer,BUFF_SIZE); // DATE
                ifs.getline(buffer,BUFF_SIZE); // PBC a b c alpha beta gamma SG

                // parse cell parameters
                tokenize(vs,buffer);
                if (vs.size() == 8)
                  {
                    //parse cell values
                    double A,B,C,Alpha,Beta,Gamma;
                    A = atof((char*)vs[1].c_str());
                    B = atof((char*)vs[2].c_str());
                    C = atof((char*)vs[3].c_str());
                    Alpha = atof((char*)vs[4].c_str());
                    Beta  = atof((char*)vs[5].c_str());
                    Gamma = atof((char*)vs[6].c_str());
                    OBUnitCell *uc = new OBUnitCell;
                    uc->SetOrigin(fileformatInput);
                    uc->SetData(A, B, C, Alpha, Beta, Gamma);
                    uc->SetSpaceGroup(vs[7]);
                    mol.SetData(uc);
                  }
              }
            else // PBC=OFF
              {
                ifs.getline(buffer,BUFF_SIZE); // title
                ifs.getline(buffer,BUFF_SIZE); // !DATE
              }
            continue;
          } // PBC

        // reading real data!
        tokenize(vs,buffer);
        if (vs.size() < 8) {
          break;
        }

        atom = mol.NewAtom();
      
        atom->SetAtomicNum(etab.GetAtomicNum(vs[7].c_str()));
        x = atof((char*)vs[1].c_str());
        y = atof((char*)vs[2].c_str());
        z = atof((char*)vs[3].c_str());
        atom->SetVector(x,y,z);
      
        // vs[0] contains atom label
        // vs[4] contains "type of residue containing atom"
        // vs[5] contains "residue sequence name"
        // vs[6] contains "potential type of atom"
      
        if (vs.size() == 9)
          {
            atom->SetPartialCharge(atof((char*)vs[8].c_str()));
            hasPartialCharges = true;
          }
      }
    
    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    mol.EndModify();
    if (hasPartialCharges)
      mol.SetPartialChargesPerceived();
    mol.SetTitle(title);
    return(true);
  }
Exemple #21
0
  static bool parseAtomRecord(char *buffer, OBMol &mol,int /*chainNum*/)
  /* ATOMFORMAT "(i5,1x,a4,a1,a3,1x,a1,i4,a1,3x,3f8.3,2f6.2,a2,a2)" */
  {
    string sbuf = &buffer[6];
    if (sbuf.size() < 48)
      return(false);

    bool hetatm = (EQn(buffer,"HETATM",6)) ? true : false;
    bool elementFound = false; // true if correct element found in col 77-78

    /* serial number */
    string serno = sbuf.substr(0,5);

    /* atom name */
    string atmid = sbuf.substr(6,4);

    /* chain */
    char chain = sbuf.substr(15,1)[0];

    /* element */
    string element = "  ";
    if (sbuf.size() > 71)
      {
        element = sbuf.substr(70,2);
        if (isalpha(element[1]))
          {
            if (element[0] == ' ')
              {
                element.erase(0, 1);
                elementFound = true;
              }
            else if (isalpha(element[0]))
              {
                elementFound = true;
              }
          }
      }

    if (!elementFound)
      {
        stringstream errorMsg;
        errorMsg << "WARNING: Problems reading a PDB file\n"
                 << "  Problems reading a HETATM or ATOM record.\n"
                 << "  According to the PDB specification,\n"
                 << "  columns 77-78 should contain the element symbol of an atom.\n"
                 << "  but OpenBabel found '" << element << "' (atom " << mol.NumAtoms()+1 << ")";
        obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obWarning);
      }

    // charge - optional
    string scharge;
    if (sbuf.size() > 73)
      {
        scharge = sbuf.substr(72,2);
      }

    //trim spaces on the right and left sides
    while (!atmid.empty() && atmid[0] == ' ')
      atmid = atmid.erase(0, 1);

    while (!atmid.empty() && atmid[atmid.size()-1] == ' ')
      atmid = atmid.substr(0,atmid.size()-1);

    /* residue name */
    string resname = sbuf.substr(11,3);
    if (resname == "   ")
      resname = "UNK";
    else
      {
        while (!resname.empty() && resname[0] == ' ')
          resname = resname.substr(1,resname.size()-1);

        while (!resname.empty() && resname[resname.size()-1] == ' ')
          resname = resname.substr(0,resname.size()-1);
      }

    string type;
    if (!elementFound) {
      // OK, we have to fall back to determining the element from the atom type
      // This is unreliable, but there's no other choice
      if (EQn(buffer,"ATOM",4)) {
        type = atmid.substr(0,2);
        if (isdigit(type[0])) {
          // sometimes non-standard files have, e.g 11HH
          if (!isdigit(type[1])) type = atmid.substr(1,1);
          else type = atmid.substr(2,1);
        } else if ((sbuf[6] == ' ' &&
                   strncasecmp(type.c_str(), "Zn", 2) != 0 &&
                   strncasecmp(type.c_str(), "Fe", 2) != 0) ||
                   isdigit(type[1]))	//type[1] is digit in Platon
          type = atmid.substr(0,1);     // one-character element


        if (resname.substr(0,2) == "AS" || resname[0] == 'N') {
          if (atmid == "AD1")
            type = "O";
          if (atmid == "AD2")
            type = "N";
        }
        if (resname.substr(0,3) == "HIS" || resname[0] == 'H') {
          if (atmid == "AD1" || atmid == "AE2")
            type = "N";
          if (atmid == "AE1" || atmid == "AD2")
            type = "C";
        }
        if (resname.substr(0,2) == "GL" || resname[0] == 'Q') {
          if (atmid == "AE1")
            type = "O";
          if (atmid == "AE2")
            type = "N";
        }
        // fix: #2002557
        if (atmid[0] == 'H' &&
            (atmid[1] == 'D' || atmid[1] == 'E' ||
             atmid[1] == 'G' || atmid[1] == 'H')) // HD, HE, HG, HH, ..
          type = "H";
      } else { //must be hetatm record
        if (isalpha(element[1]) && (isalpha(element[0]) || (element[0] == ' '))) {
          if (isalpha(element[0]))
            type = element.substr(0,2);
          else
            type = element.substr(1,1);

          if (type.size() == 2)
            type[1] = tolower(type[1]);
        } else { // no element column to use
          if (isalpha(atmid[0])) {
            if (atmid.size() > 2 && (atmid[2] == '\0' || atmid[2] == ' '))
              type = atmid.substr(0,2);
            else if (atmid[0] == 'A') // alpha prefix
              type = atmid.substr(1, atmid.size() - 1);
            else
              type = atmid.substr(0,1);
          } else if (atmid[0] == ' ')
            type = atmid.substr(1,1); // one char element
          else
            type = atmid.substr(1,2);

          // Some cleanup steps
          if (atmid == resname) {
            type = atmid;
            if (type.size() == 2)
              type[1] = tolower(type[1]);
          } else
            if (resname == "ADR" || resname == "COA" || resname == "FAD" ||
                resname == "GPG" || resname == "NAD" || resname == "NAL" ||
                resname == "NDP" || resname == "ABA") {
              if (type.size() > 1)
                type = type.substr(0,1);
              //type.erase(1,type.size()-1);
            } else // other residues
              if (isdigit(type[0])){
                type = type.substr(1,1);
              }
              else
                if (type.size() > 1 && isdigit(type[1]))
                  type = type.substr(0,1);
                else
                  if (type.size() > 1 && isalpha(type[1])) {
                    if (type[0] == 'O' && type[1] == 'H')
                      type = type.substr(0,1); // no "Oh" element (e.g. 1MBN)
                    else if(isupper(type[1])) {
                      type[1] = tolower(type[1]);
                    }
                  }
        }

      } // HETATM records
    } // no element column to use

    OBAtom atom;
    /* X, Y, Z */
    string xstr = sbuf.substr(24,8);
    string ystr = sbuf.substr(32,8);
    string zstr = sbuf.substr(40,8);
    vector3 v(atof(xstr.c_str()),atof(ystr.c_str()),atof(zstr.c_str()));
    atom.SetVector(v);
    atom.ForceImplH();

    // useful for debugging unknown atom types (e.g., PR#1577238)
    //    cout << mol.NumAtoms() + 1  << " : '" << element << "'" << " " << etab.GetAtomicNum(element.c_str()) << endl;
    if (elementFound)
      atom.SetAtomicNum(etab.GetAtomicNum(element.c_str()));
    else // use our old-style guess from athe atom type
      atom.SetAtomicNum(etab.GetAtomicNum(type.c_str()));

    if ( (! scharge.empty()) && "  " != scharge )
      {
        if ( isdigit(scharge[0]) && ('+' == scharge[1] || '-' == scharge[1]) )
          {
            const char reorderCharge[3] = { scharge[1], scharge[0], '\0' };
            const int charge = atoi(reorderCharge);
            atom.SetFormalCharge(charge);
          }
        else
          {
            stringstream errorMsg;
            errorMsg << "WARNING: Problems reading a PDB file\n"
                     << "  Problems reading a HETATM or ATOM record.\n"
                     << "  According to the PDB specification,\n"
                     << "  columns 79-80 should contain charge of the atom\n"
                     << "  but OpenBabel found '" << scharge << "' (atom " << mol.NumAtoms()+1 << ").";
            obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obWarning);
          }
      }
    else {
      atom.SetFormalCharge(0);
    }

    /* residue sequence number */
    string resnum = sbuf.substr(16,4);
    OBResidue *res  = (mol.NumResidues() > 0) ? mol.GetResidue(mol.NumResidues()-1) : NULL;
    if (res == NULL
        || res->GetName() != resname
        || res->GetNumString() != resnum
        || res->GetChain() != chain)
      {
        vector<OBResidue*>::iterator ri;
        for (res = mol.BeginResidue(ri) ; res ; res = mol.NextResidue(ri))
          if (res->GetName() == resname
              && res->GetNumString() == resnum
              && static_cast<int>(res->GetChain()) == chain)
            break;

        if (res == NULL) {
          res = mol.NewResidue();
          res->SetChain(chain);
          res->SetName(resname);
          res->SetNum(resnum);
        }
      }

    if (!mol.AddAtom(atom))
      return(false);
    else {
      OBAtom *atom = mol.GetAtom(mol.NumAtoms());

      res->AddAtom(atom);
      res->SetSerialNum(atom, atoi(serno.c_str()));
      res->SetAtomID(atom, sbuf.substr(6,4));
      res->SetHetAtom(atom, hetatm);

      return(true);
    }
  } // end reading atom records
Exemple #22
0
  bool TinkerFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {
    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
        return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    int natoms;
    char buffer[BUFF_SIZE];
    vector<string> vs;

    ifs.getline(buffer, BUFF_SIZE);
    tokenize(vs,buffer);
    if (vs.size() < 2)
      return false;
    natoms = atoi(vs[0].c_str());

    // title is 2nd token (usually add tokens for the atom types)
    mol.SetTitle(vs[1]);

    mol.ReserveAtoms(natoms);
    mol.BeginModify();

    string str;
    double x,y,z;
    OBAtom *atom;

    for (int i = 1; i <= natoms; ++i)
    {
        if (!ifs.getline(buffer,BUFF_SIZE))
            return(false);
        tokenize(vs,buffer);
        // e.g. "2  C      2.476285    0.121331   -0.001070     2     1     3    14"
        if (vs.size() < 5)
            return(false);

        atom = mol.NewAtom();
        x = atof((char*)vs[2].c_str());
        y = atof((char*)vs[3].c_str());
        z = atof((char*)vs[4].c_str());
        atom->SetVector(x,y,z); //set coordinates

        //set atomic number
        atom->SetAtomicNum(OBElements::GetAtomicNum(vs[1].c_str()));

        // add bonding
        if (vs.size() > 6)
          for (unsigned int j = 6; j < vs.size(); ++j)
            mol.AddBond(mol.NumAtoms(), atoi((char *)vs[j].c_str()), 1); // we don't know the bond order

    }
    if (!pConv->IsOption("s",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();

    // clean out remaining blank lines
    std::streampos ipos;
    do
    {
      ipos = ifs.tellg();
      ifs.getline(buffer,BUFF_SIZE);
    }
    while(strlen(buffer) == 0 && !ifs.eof() );
    ifs.seekg(ipos);

    mol.EndModify();
    mol.SetTitle(title);
    return(true);
  }
bool ChemDrawXMLFormat::DoElement(const string& name)
{
    string buf;
    if(name=="fragment")
    {
        //This is the start of the molecule we are extracting and it will
        //be put into the OBMol* _pmol declared in the parent class.
        //initialise everything
        _tempAtom.Clear();
        atoms.clear();

        _pmol->SetDimension(2);
        _pmol->BeginModify();

        buf = _pxmlConv->GetAttribute("id");
        if (buf.length())
        {
            _pmol->SetTitle(buf);
        }
    }
    else if(name=="n")
    {
        EnsureEndElement();
        buf = _pxmlConv->GetAttribute("Type");
        if (buf.length())
        {
            if (buf != "Unspecified" && buf != "Element")
            {
                cerr << "CDXML Format: Node type \"" << buf <<
                     "\" is not currently supported." << endl;
                return false; // FIXME: use as many types as possible
            }
        }
        _tempAtom.SetAtomicNum(6); // default is carbon
        buf = _pxmlConv->GetAttribute("id");
        if (buf.length())
            _tempAtom.SetIdx(atoi(buf.c_str()));
        buf = _pxmlConv->GetAttribute("Element");
        if (buf.length())
            _tempAtom.SetAtomicNum(atoi(buf.c_str()));

        buf = _pxmlConv->GetAttribute("p"); // coords
        if (buf.length())
        {
            double x = 0., y = 0.;
            sscanf(buf.c_str(), "%lf %lf", &x, &y);
            _tempAtom.SetVector(x, y, 0.);
        }
        buf = _pxmlConv->GetAttribute("Charge");
        if (buf.length())
            _tempAtom.SetFormalCharge(atoi(buf.c_str()));
    }
    else if(name=="b")
    {
        EnsureEndElement();
        bool invert_ends = false;
        Begin = End = Flag = 0;
        buf = _pxmlConv->GetAttribute("Order");
        if (buf.length())
            Order = atoi(buf.c_str());
        else
            Order = 1; //default value
        buf = _pxmlConv->GetAttribute("Display");
        if (buf.length())
        {
            if (buf == "WedgeEnd")
            {
                invert_ends = true;
                Flag = OB_HASH_BOND;
            }
            else if (buf == "WedgeBegin")
            {
                Flag = OB_HASH_BOND;
            }
            else if (buf == "Hash" ||buf == "WedgedHashBegin")
            {
                Flag = OB_WEDGE_BOND;
            }
            else if (buf == "WedgedHashEnd")
            {
                invert_ends = true;
                Flag = OB_WEDGE_BOND;
            }
        }
        buf = _pxmlConv->GetAttribute("B");
        if (buf.length())
        {
            if (invert_ends)
                End = atoms[atoi(buf.c_str())];
            else
                Begin = atoms[atoi(buf.c_str())];
        }
        buf = _pxmlConv->GetAttribute("E");
        if (buf.length())
        {
            if (invert_ends)
                Begin = atoms[atoi(buf.c_str())];
            else
                End = atoms[atoi(buf.c_str())];
        }
    }
    /*
    // Forget that, the fragment, aka molecule, is in another XML hierachy tree than the data.
    // Parsing has already stopped before ever getting to this point
    else if(name=="tags")
    {
    	buf = _pxmlConv->GetAttribute("ID");
      if (buf.length())
      {
      }
    }
    else if(name=="tableCell")
    {
    	buf = _pxmlConv->GetAttribute("value");
      if (buf.length())
      {
      }
    }
    */
    return true;
}
Exemple #24
0
  bool ACRFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {
    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    istream& ifs = *pConv->GetInStream();

    pmol->BeginModify();

    /** Parse the input stream and use the OpenBabel API to populate the OBMol **/
    int id;
    char buf[BUFF_SIZE];
    int atoms, bonds, tmp;
    float scale, dtmp;
    bool atom_input = false, bond_input = false;
    string type;
    //int from, to;
    double X,Y,Z;
    vector<string> vs;

    // read in one at a time
    /* WARNING: Atom id starts from zero in Carine; not so in openbabel.
     * Solution: increment atom id's */

    while (true) {
      ifs.getline(buf, BUFF_SIZE);
      if (ifs.eof()) {
        break;
      }

      if (sscanf(buf, "General Scale=%f\n", &dtmp)) {
        scale = dtmp;
        continue;
      } else if (sscanf(buf, "Number of Atoms in Crystal=%d\n", &tmp)) {
        atoms = tmp;
        atom_input = true;

        // read table column names
        ifs.getline(buf, BUFF_SIZE);
        continue;
      } else if (sscanf(buf, "Number of Links in Crystal=%d\n", &tmp)) {
        atom_input = false;
        bond_input = true;
        bonds = tmp;

        // read table column names
        ifs.getline(buf, BUFF_SIZE);
        continue;
      } else if ( '#' == buf[0] || '\r' == buf[0] || '\n' == buf[0] ) {
        // between sections, in both windows and unix.
        continue;
      }
      tokenize(vs, buf, " \t\r\n");

      if (atom_input) {
	if (vs.size() < 9) return false; // timvdm 18/06/2008
        id = atoi((char*)vs[0].c_str()) + 1; // see warning above
        type = vs[1];
        X = atof((char*)vs[6].c_str())/scale;
        Y = atof((char*)vs[7].c_str())/scale;
        Z = atof((char*)vs[8].c_str())/scale;

        OBAtom* a = pmol->NewAtom();
        if (*(type.c_str()) != '*')
          a->SetAtomicNum(etab.GetAtomicNum(type.c_str()));
        a->SetVector(X,Y,Z);

      } else if (bond_input) {
	if (vs.size() < 2) return false; // timvdm 18/06/2008
        // add to pmol
        if (!pmol->AddBond(atoi((char*)vs[0].c_str()) + 1, atoi((char*)vs[1].c_str()) + 1,
                           1 /* bond order not specified in Carine, use PerceiveBondOrder later */))
          {
            obErrorLog.ThrowError(__FUNCTION__, "addition of bond between " + vs[0] + " and " + vs[1] + " failed", obError);
            return false;
          }
      }
    }

    /* got sanity? */
    if ( pmol->NumBonds() != bonds ) {
      // then we read a different number of bonds than those promised.
      obErrorLog.ThrowError(__FUNCTION__, "Number of bonds read does not match the number promised", obError);
      return false;
    } else if ( pmol->NumAtoms() != atoms ) {
      obErrorLog.ThrowError(__FUNCTION__, "Number of atoms read does not match the number promised", obError);
      return false;
    }

    pmol->PerceiveBondOrders();

    pmol->EndModify();

    return true;
  }
Exemple #25
0
bool ReadViewMol(istream &ifs,OBMol &mol,const char *title)
{
  char buffer[BUFF_SIZE];
  OBAtom *atom;
  double x,y,z, border;
  double factor = 1.0;
  int bgn, end, order;
  vector<string> vs;
  bool foundTitle = false;
  bool foundBonds = false;

  mol.BeginModify();

  while	(ifs.getline(buffer,BUFF_SIZE))
    {
      if (strstr(buffer,"$title") != NULL)
	{
	  if (!ifs.getline(buffer,BUFF_SIZE)) return (false);
	  mol.SetTitle(buffer);
	  foundTitle = true;
	}
      else if (strstr(buffer,"$coord") != NULL)
	{
	  tokenize(vs,buffer);
	  if (vs.size() == 2)
	    factor = atof((char*)vs[1].c_str()); // conversion to angstrom
	  while (ifs.getline(buffer,BUFF_SIZE))
	    {
	      if (buffer[0] == '$') break;
	      tokenize(vs,buffer);
	      if (vs.size() != 4) return(false);
	      atom = mol.NewAtom();
	      x = atof((char*)vs[0].c_str()) * factor;
	      y = atof((char*)vs[1].c_str()) * factor;
	      z = atof((char*)vs[2].c_str()) * factor;
	      atom->SetVector(x,y,z); //set coordinates
	      atom->SetAtomicNum(etab.GetAtomicNum(vs[3].c_str()));
	    }
	}
      else if (strstr(buffer,"$bonds") != NULL) 
	{
	  foundBonds = true;
	  while (ifs.getline(buffer,BUFF_SIZE))
	    {
	      if (buffer[0] == '$') break;
	      sscanf(buffer,"%d %d %lf",&bgn,&end, &border);
	      if (border > 1.0)
		order = int(border);
	      else
		order = 1;
	      mol.AddBond(bgn+1,end+1,order);
	    }
	}
      else if (strstr(buffer,"$end") != NULL)
	break;
    } // while
  
  mol.EndModify();

  if (!foundTitle)
    mol.SetTitle(title);
  if (!foundBonds)
    {
      mol.ConnectTheDots();
      mol.PerceiveBondOrders();
    }
  return(true);
}
bool ThermoFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
{
  OBMol* pmol = pOb->CastAndClear<OBMol>();
  if(!pmol)
    return false;
  bool stopOnEnd = pConv->IsOption("e",OBConversion::INOPTIONS)!=NULL;
  pmol->SetDimension(0);
  OBNasaThermoData* pND = new OBNasaThermoData; //to store rate constant data
  pND->SetOrigin(fileformatInput);
  pmol->SetData(pND);

  istream &ifs = *pConv->GetInStream();

  double DefaultMidT = 1500;
  char ln[BUFF_SIZE];
  unsigned int i;

  //find line with 1 in col 80
  do
  {
    if(!ifs.getline(ln,BUFF_SIZE) || stopOnEnd && !strncasecmp(ln,"END",3))
      return false;
  }while(ln[79]!='1');

  char phase, nam[25], dum[7], elname[3];
  elname[2]=0;
  int elnum;
  double Coeff[14];

  sscanf(ln,"%18s%6s",nam,dum);
  pmol->SetTitle(nam);
  char* p=ln+24;
  if(ln[80]=='&')
  {
    //Reaction Design extension
    p+=20;
    string line;
    if(!getline(ifs,line))return false;
    vector<string> toks;
    tokenize(toks,line," \t\n\r");
    for(i=0;i<toks.size();i+=2)
    {
      OBAtom atom;
      atom.SetAtomicNum(etab.GetAtomicNum(toks[i].c_str()));
      elnum = atoi(toks[i+1].c_str());
      atom.ForceNoH();
      for(;elnum>0;--elnum)
        pmol->AddAtom(atom);
    }
  }
  else
  {
    for(i=0;i<4;i++,p+=5)
    {
      char snum[4]={0,0,0,0};//Was problem with F   10   0 reading as ten
      sscanf(p,"%c%c%c%c%c",elname,elname+1,snum,snum+1,snum+2);
      elnum=atoi(snum);
      if(elname[0]!=' ' && elname[0]!='0')
      {
        if(elname[1]==' ')
          elname[1]=0;
        OBAtom atom;
        atom.SetAtomicNum(etab.GetAtomicNum(elname));
        atom.ForceNoH();
        for(;elnum>0;--elnum)
          pmol->AddAtom(atom);
      }
    }
  }
  double LoT, HiT, MidT=0;
  /* int nc = */sscanf(p,"%c%10lf%10lf10%lf",&phase, &LoT, &HiT, &MidT);
  pND->SetPhase(phase);
  pND->SetLoT(LoT);
  pND->SetHiT(HiT);
  if(MidT>HiT || MidT<LoT)
    MidT=DefaultMidT;
  pND->SetMidT(MidT);
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
  p=ln;
  for(i=0;i<5;i++,p+=15)
    sscanf(p,"%15lf",&Coeff[i]);
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
  p=ln;
  for(i=5;i<10;i++,p+=15)
    sscanf(p,"%15lf",&Coeff[i]);
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
  p=ln;
  for(i=10;i<14;i++,p+=15)
    sscanf(p,"%15lf",&Coeff[i]);

  for(i=0;i<14;++i)
    pND->SetCoeff(i, Coeff[i]);

  pmol->AssignSpinMultiplicity();
  return true;
}
Exemple #27
0
  bool MSIFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {
    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();
    char buffer[BUFF_SIZE];

    stringstream errorMsg;

    if (!ifs)
      return false; // we're attempting to read past the end of the file

    if (!ifs.getline(buffer,BUFF_SIZE))
      {
        obErrorLog.ThrowError(__FUNCTION__,
                              "Problems reading an MSI file: Cannot read the first line.", obWarning);
        return(false);
      }

    if (!EQn(buffer, "# MSI CERIUS2 DataModel File", 28))
      {
        obErrorLog.ThrowError(__FUNCTION__,
                              "Problems reading an MSI file: The first line must contain the MSI header.", obWarning);
        return(false);
      }

    // "records" start with
    // (1 Model
    // ....
    //   and end with
    // ....
    // )
    unsigned int openParens = 0; // the count of "open parentheses" tags
    unsigned int startBondAtom, endBondAtom, bondOrder;
    bool atomRecord = false;
    bool bondRecord = false;
    OBAtom *atom;
//    OBBond *bond;
    vector<string> vs;
    const SpaceGroup *sg;
    bool setSpaceGroup = false;
    double x,y,z;
    vector3 translationVectors[3];
    int numTranslationVectors = 0;

    mol.BeginModify();
    while (ifs.getline(buffer,BUFF_SIZE))
      {
        // model record
        if (strstr(buffer, "Model") != NULL) {
          openParens++;
          continue;
        }

        // atom record
        if (!bondRecord && strstr(buffer, "Atom") != NULL) {
          atomRecord = true;
          openParens++;
          continue;
        }

        if (strstr(buffer, "Bond") != NULL) {
          bondRecord = true;
          startBondAtom = endBondAtom = 0;
          bondOrder = 1;
          openParens++;
          continue;
        }

        /* (A I PeriodicType 100)
           (A D A3 (6.2380000000000004 0 0))
           (A D B3 (0 6.9909999999999997 0))
           (A D C3 (0 0 6.9960000000000004))
           (A C SpaceGroup "63 5")
        */
        if (strstr(buffer, "PeriodicType") != NULL) {
          ifs.getline(buffer,BUFF_SIZE); // next line should be translation vector
          tokenize(vs,buffer);
            while (vs.size() == 6) {
              x = atof((char*)vs[3].erase(0,1).c_str());
              y = atof((char*)vs[4].c_str());
              z = atof((char*)vs[5].c_str());

              translationVectors[numTranslationVectors++].Set(x, y, z);
              if (!ifs.getline(buffer,BUFF_SIZE))
                break;
              tokenize(vs,buffer);
            }
        }

        if (strstr(buffer, "SpaceGroup") != NULL) {
          tokenize(vs, buffer);
          if (vs.size() != 5)
            continue; // invalid space group
          setSpaceGroup = true;
          sg = SpaceGroup::GetSpaceGroup(vs[4]); // remove the initial " character
        }

        // atom information
        if (atomRecord) {
          if (strstr(buffer, "ACL") != NULL) {
            tokenize(vs, buffer);
            // size should be 5 -- need a test here
            if (vs.size() != 5) return false; // timvdm 18/06/2008
            vs[3].erase(0,1); // "6 => remove the first " character
            int atomicNum = atoi(vs[3].c_str());
            if (atomicNum == 0)
              atomicNum = 1; // hydrogen ?
            if (atomicNum <= 0 || atomicNum > etab.GetNumberOfElements())
              continue;

            // valid element, so create the atom
            atom = mol.NewAtom();
            atom->SetAtomicNum(atomicNum);
            continue;
          }
          else if (strstr(buffer, "XYZ") != NULL) {
            tokenize(vs, buffer);
            // size should be 6 -- need a test here
            if (vs.size() != 6) return false; // timvdm 18/06/2008
            vs[3].erase(0,1); // remove ( character
            vs[5].erase(vs[5].length()-2, 2); // remove trailing )) characters
            atom->SetVector(atof(vs[3].c_str()),
                            atof(vs[4].c_str()),
                            atof(vs[5].c_str()));
            continue;
          }
        } // end of atom records

        // bond information
        if (bondRecord) {
          if (strstr(buffer, "Atom1") != NULL) {
            tokenize(vs, buffer);
            if (vs.size() < 4) return false; // timvdm 18/06/2008
            vs[3].erase(vs[3].length()-1,1);
            startBondAtom = atoi(vs[3].c_str());
            continue;
          }
          else if (strstr(buffer, "Atom2") != NULL) {
            tokenize(vs, buffer);
            if (vs.size() < 4) return false; // timvdm 18/06/2008
            vs[3].erase(vs[3].length()-1,1);
            endBondAtom = atoi(vs[3].c_str());
            continue;
          }
          else if (strstr(buffer, "Type") != NULL) {
            tokenize(vs, buffer);
            if (vs.size() < 4) return false; // timvdm 18/06/2008
            vs[3].erase(vs[3].length()-1,1);
            bondOrder = atoi(vs[3].c_str());
            if (bondOrder == 4) // triple bond?
              bondOrder = 3;
            else if (bondOrder == 8) // aromatic?
              bondOrder = 5;
            else if (bondOrder != 2) // 1 OK, 2 OK, others unknown
              bondOrder = 1;
            continue;
          }
        }

        // ending a "tag" -- a lone ")" on a line
        if (strstr(buffer,")") != NULL && strstr(buffer, "(") == NULL) {
          openParens--;
          if (atomRecord) {
            atomRecord = false;
          }
          if (bondRecord) {
            // Bond records appear to be questionable
            mol.AddBond(startBondAtom - 1, endBondAtom - 1, bondOrder);
            bondRecord = false;
          }

          if (openParens == 0) {
            ifs.getline(buffer, BUFF_SIZE);
            break; // closed this molecule
          }
        }
      }

    mol.EndModify();

    // clean out any remaining blank lines
    while(ifs.peek() != EOF && ifs.good() &&
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
      ifs.getline(buffer,BUFF_SIZE);

    /*
    if (!pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.ConnectTheDots();
    if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
      mol.PerceiveBondOrders();
    */

    if (numTranslationVectors > 0) {
      OBUnitCell* uc = new OBUnitCell;
      uc->SetData(translationVectors[0], translationVectors[1], translationVectors[2]);
      uc->SetOrigin(fileformatInput);
      if (setSpaceGroup) {
        uc->SetSpaceGroup(sg);
      }
      mol.SetData(uc);
    }

    return(true);
  }
  bool AlchemyFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetInFilename().c_str();

    int i;
    int natoms = 0, nbonds = 0;
    char buffer[BUFF_SIZE];

    ifs.getline(buffer,BUFF_SIZE);
    sscanf(buffer," %d %*s %d", &natoms, &nbonds);
    if (!natoms)
      {
        ifs.getline(buffer,BUFF_SIZE);
        sscanf(buffer," %d %*s %d", &natoms, &nbonds);
        if (!natoms)
          return(false);
      }

    mol.ReserveAtoms(natoms);
    mol.BeginModify();
    ttab.SetFromType("ALC");

    string str;
    double x,y,z;
    OBAtom *atom;
    vector<string> vs;

    for (i = 1; i <= natoms; i ++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        tokenize(vs,buffer);
        if (vs.size() < 5)
          return(false);
        atom = mol.NewAtom();
        x = atof((char*)vs[2].c_str());
        y = atof((char*)vs[3].c_str());
        z = atof((char*)vs[4].c_str());
        atom->SetVector(x,y,z); //set coordinates

        //set atomic number
        ttab.SetToType("ATN");
        ttab.Translate(str,vs[1]);
        atom->SetAtomicNum(atoi(str.c_str()));
        //set type
        ttab.SetToType("INT");
        ttab.Translate(str,vs[1]);
        atom->SetType(str);
      }

    char bobuf[100];
    string bostr;
    int bgn,end,order;

    for (i = 0; i < nbonds; i++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        sscanf(buffer," %*d%d%d%99s",&bgn,&end,bobuf);
        bostr = bobuf;
        order = 1;
        if      (bostr == "DOUBLE")
          order = 2;
        else if (bostr == "TRIPLE")
          order = 3;
        else if (bostr == "AROMATIC")
          order = 5;
        mol.AddBond(bgn,end,order);
      }

    // clean out remaining blank lines
    while(ifs.peek() != EOF && ifs.good() &&
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
      ifs.getline(buffer,BUFF_SIZE);

    mol.EndModify();
    mol.SetTitle(title);
    return(true);
  }
  bool MOL2Format::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;

    //Old code follows...
    bool foundAtomLine = false;
    char buffer[BUFF_SIZE];
    char *comment = NULL;
    string str,str1;
    vector<string> vstr;
    int len;

    mol.BeginModify();

    for (;;)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        if (EQn(buffer,"@<TRIPOS>MOLECULE",17))
          break;
      }

    int lcount;
    int natoms,nbonds;
    for (lcount=0;;lcount++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        if (EQn(buffer,"@<TRIPOS>ATOM",13))
          {
            foundAtomLine = true;
            break;
          }

        if (lcount == 0)
          {
            tokenize(vstr,buffer);
            if (!vstr.empty())
              mol.SetTitle(buffer);
          }
        else if (lcount == 1)
          sscanf(buffer,"%d%d",&natoms,&nbonds);
        else if (lcount == 4) //energy
          {
            tokenize(vstr,buffer);
            if (!vstr.empty() && vstr.size() == 3)
              if (vstr[0] == "Energy")
                mol.SetEnergy(atof(vstr[2].c_str()));
          }
        else if (lcount == 5) //comment
          {
            if ( buffer[0] )
              {
                len = (int) strlen(buffer)+1;
                // TODO allow better multi-line comments
                // which don't allow ill-formed data to consume memory
                // Thanks to Andrew Dalke for the pointer
                if (comment != NULL)
                  delete [] comment;
                comment = new char [len];
                memcpy(comment,buffer,len);
              }
          }
      }

    if (!foundAtomLine)
      {
        mol.EndModify();
        mol.Clear();
        obErrorLog.ThrowError(__FUNCTION__, "Unable to read Mol2 format file. No atoms found.", obWarning);
        return(false);
      }

    mol.ReserveAtoms(natoms);

    int i;
    vector3 v;
    OBAtom atom;
    bool hasPartialCharges=false;
    double x,y,z,pcharge;
    char temp_type[BUFF_SIZE], resname[BUFF_SIZE], atmid[BUFF_SIZE];
    int elemno, resnum = -1;

    ttab.SetFromType("SYB");
    for (i = 0;i < natoms;i++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        sscanf(buffer," %*s %1024s %lf %lf %lf %1024s %d %1024s %lf",
               atmid, &x,&y,&z, temp_type, &resnum, resname, &pcharge);

        atom.SetVector(x, y, z);

        // Handle "CL" and "BR" and other mis-typed atoms
        str = temp_type;
        if (strncmp(temp_type, "CL", 2) == 0) {
          str = "Cl";
        } else  if (strncmp(temp_type,"BR",2) == 0) {
          str = "Br";
        } else if (strncmp(temp_type,"S.o2", 4) == 02) {
          str = "S.O2";
        } else if (strncmp(temp_type,"S.o", 3) == 0) {
          str = "S.O";
        } else if (strncmp(temp_type,"SI", 2) == 0) {
          str = "Si";
        // The following cases are entries which are not in openbabel/data/types.txt
        // and should probably be added there
        } else if (strncmp(temp_type,"S.1", 3) == 0) {
          str = "S.2"; // no idea what the best type might be here
        } else if (strncmp(temp_type,"P.", 2) == 0) {
          str = "P.3";
        } else if (strncasecmp(temp_type,"Ti.", 3) == 0) { // e.g. Ti.th
          str = "Ti";
        } else if (strncasecmp(temp_type,"Ru.", 3) == 0) { // e.g. Ru.oh
          str = "Ru";
        }

        ttab.SetToType("ATN");
        ttab.Translate(str1,str);
        elemno = atoi(str1.c_str());
        ttab.SetToType("IDX");

        // We might have missed some SI or FE type things above, so here's
        // another check
        if( !elemno && isupper(temp_type[1]) )
          {
            temp_type[1] = (char)tolower(temp_type[1]);
            str = temp_type;
            ttab.Translate(str1,str);
            elemno = atoi(str1.c_str());
          }
        // One last check if there isn't a period in the type,
        // it's a malformed atom type, but it may be the element symbol
        // GaussView does this (PR#1739905)
        if ( !elemno ) {
          obErrorLog.ThrowError(__FUNCTION__, "This Mol2 file is non-standard. Cannot interpret atom types correctly, instead attempting to interpret as elements instead.", obWarning);

          string::size_type dotPos = str.find('.');
          if (dotPos == string::npos) {
            elemno = etab.GetAtomicNum(str.c_str());
          }
        }

        atom.SetAtomicNum(elemno);
        ttab.SetToType("INT");
        ttab.Translate(str1,str);
        atom.SetType(str1);
        atom.SetPartialCharge(pcharge);
        if (!mol.AddAtom(atom))
          return(false);
        if (!IsNearZero(pcharge))
          hasPartialCharges = true;

        // Add residue information if it exists
        if (resnum != -1 && resnum != 0 &&
            strlen(resname) != 0 && strncmp(resname,"<1>", 3) != 0)
          {
            OBResidue *res  = (mol.NumResidues() > 0) ?
              mol.GetResidue(mol.NumResidues()-1) : NULL;
            if (res == NULL || res->GetName() != resname ||
                static_cast<int>(res->GetNum()) != resnum)
              {
                vector<OBResidue*>::iterator ri;
                for (res = mol.BeginResidue(ri) ; res ; res = mol.NextResidue(ri))
                  if (res->GetName() == resname &&
                      static_cast<int>(res->GetNum())
                      == resnum)
                    break;

                if (res == NULL)
                  {
                    res = mol.NewResidue();
                    res->SetName(resname);
                    res->SetNum(resnum);
                  }
              }
            OBAtom *atomPtr = mol.GetAtom(mol.NumAtoms());
            res->AddAtom(atomPtr);
            res->SetAtomID(atomPtr, atmid);
          } // end adding residue info
      }

    for (;;)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);
        str = buffer;
        if (!strncmp(buffer,"@<TRIPOS>BOND",13))
          break;
      }

    int start,end,order;
    for (i = 0; i < nbonds; i++)
      {
        if (!ifs.getline(buffer,BUFF_SIZE))
          return(false);

        sscanf(buffer,"%*d %d %d %1024s",&start,&end,temp_type);
        str = temp_type;
        order = 1;
        if (str == "ar" || str == "AR" || str == "Ar")
          order = 5;
        else if (str == "AM" || str == "am" || str == "Am")
          order = 1;
        else
          order = atoi(str.c_str());

        mol.AddBond(start,end,order);
      }

 
    // update neighbour bonds information for each atom.
    vector<OBAtom*>::iterator apos;
    vector<OBBond*>::iterator bpos;
    OBAtom* patom;
    OBBond* pbond;
    
    for (patom = mol.BeginAtom(apos); patom; patom = mol.NextAtom(apos))
      {
        patom->ClearBond();
        for (pbond = mol.BeginBond(bpos); pbond; pbond = mol.NextBond(bpos))
          {
            if (patom == pbond->GetBeginAtom() || patom == pbond->GetEndAtom())
              {
                patom->AddBond(pbond);
              }
          }
      }

    // Suggestion by Liu Zhiguo 2008-01-26
    // Mol2 files define atom types -- there is no need to re-perceive
    mol.SetAtomTypesPerceived();
    mol.EndModify();

    //must add generic data after end modify - otherwise it will be blown away
    if (comment)
      {
        OBCommentData *cd = new OBCommentData;
        cd->SetData(comment);
        cd->SetOrigin(fileformatInput);
        mol.SetData(cd);
        delete [] comment;
        comment = NULL;
      }
    if (hasPartialCharges)
      mol.SetPartialChargesPerceived();

    // continue untill EOF or untill next molecule record
    streampos pos;
    for(;;)
      {
        pos = ifs.tellg();
        if (!ifs.getline(buffer,BUFF_SIZE))
          break;
        if (EQn(buffer,"@<TRIPOS>MOLECULE",17))
          break;
      }

    ifs.seekg(pos); // go back to the end of the molecule
    return(true);
  }
Exemple #30
0
  bool HINFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
  {

    OBMol* pmol = pOb->CastAndClear<OBMol>();
    if(pmol==NULL)
      return false;

    //Define some references so we can use the old parameter names
    istream &ifs = *pConv->GetInStream();
    OBMol &mol = *pmol;
    const char* title = pConv->GetTitle();

    // Right now only read in the first molecule
    int i;
    int max, bo;
    char buffer[BUFF_SIZE];
    string str,str1;
    double x,y,z;
    OBAtom *atom;
    vector<string> vs;

    ifs.getline(buffer, BUFF_SIZE);
    while (ifs.good() && (strstr(buffer,"mol") == NULL || buffer[0]==';') ) //The "mol" in comment line should be ignored.
      {
        ifs.getline(buffer, BUFF_SIZE);
        if (ifs.peek() == EOF || !ifs.good())
          return false;
      }
    ifs.getline(buffer, BUFF_SIZE);
    if (!ifs.good())
      return false; // ended early

    mol.BeginModify();
    while (ifs.good() && strstr(buffer,"endmol") == NULL)
      {
	if(buffer[0]==';'){
		 ifs.getline(buffer, BUFF_SIZE);
		 continue; //The comment Line in HIN should be ignored.
	}

        tokenize(vs,buffer); // Don't really know how long it'll be
        if (vs.size() < 11)
          {
            ifs.getline(buffer, BUFF_SIZE);
            continue;
          }

        atom = mol.NewAtom();
        atom->SetAtomicNum(etab.GetAtomicNum(vs[3].c_str()));
        atom->SetPartialCharge(atof(vs[6].c_str()));
        x = atof((char*)vs[7].c_str());
        y = atof((char*)vs[8].c_str());
        z = atof((char*)vs[9].c_str());
        atom->SetVector(x,y,z);

        max = 11 + 2 * atoi((char *)vs[10].c_str());
        for (i = 11; i < max; i+=2)
          {
            switch(((char*)vs[i+1].c_str())[0]) // First char in next token
              {
              case 's':
                bo = 1;
                break;
              case 'd':
                bo = 2;
                break;
              case 't':
                bo = 3;
                break;
              case 'a':
                bo = 5;
                break;
              default :
                bo = 1;
                break;
              }
            mol.AddBond(mol.NumAtoms(), atoi((char *)vs[i].c_str()), bo);
          }
        ifs.getline(buffer, BUFF_SIZE);
      }

    // clean out remaining blank lines
    while(ifs.peek() != EOF && ifs.good() && 
          (ifs.peek() == '\n' || ifs.peek() == '\r'))
      ifs.getline(buffer,BUFF_SIZE);

    mol.EndModify();

    mol.SetTitle(title);
    mol.SetPartialChargesPerceived();
    
    return(true);
  }