Coverage Report

Created: 2026-01-17 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/src/formats/cssrformat.cpp
Line
Count
Source
1
/**********************************************************************
2
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
3
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
4
Some portions Copyright (C) 2004 by Chris Morley
5
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation version 2 of the License.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
***********************************************************************/
15
16
#include <openbabel/babelconfig.h>
17
#include <openbabel/obmolecformat.h>
18
#include <openbabel/mol.h>
19
#include <openbabel/atom.h>
20
#include <openbabel/elements.h>
21
#include <openbabel/generic.h>
22
23
using namespace std;
24
namespace OpenBabel
25
{
26
27
  class CSSRFormat : public OBMoleculeFormat
28
  {
29
  public:
30
    //Register this format type ID
31
    CSSRFormat()
32
6
    {
33
6
      OBConversion::RegisterFormat("cssr",this);
34
6
    }
35
36
    const char* Description() override  // required
37
0
    {
38
0
      return
39
0
        "CSD CSSR format\n"
40
0
        "No comments yet\n";
41
0
    }
42
43
    const char* SpecificationURL() override
44
0
    { return ""; }  // optional
45
46
    //Flags() can return be any the following combined by | or be omitted if none apply
47
    // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
48
    unsigned int Flags() override
49
16
    {
50
16
      return NOTREADABLE;
51
16
    }
52
53
    ////////////////////////////////////////////////////
54
    /// The "API" interface functions
55
    bool WriteMolecule(OBBase* pOb, OBConversion* pConv) override;
56
57
  };
58
59
  //Make an instance of the format class
60
  CSSRFormat theCSSRFormat;
61
62
  ////////////////////////////////////////////////////////////////
63
64
  bool CSSRFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
65
0
  {
66
0
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
67
0
    if (pmol == nullptr)
68
0
      return false;
69
70
    //Define some references so we can use the old parameter names
71
0
    ostream &ofs = *pConv->GetOutStream();
72
0
    OBMol &mol = *pmol;
73
74
0
    char buffer[BUFF_SIZE];
75
76
0
    if (!mol.HasData(OBGenericDataType::UnitCell))
77
0
      {
78
0
        snprintf(buffer, BUFF_SIZE,
79
0
                 " REFERENCE STRUCTURE = 00000   A,B,C =%8.3f%8.3f%8.3f",
80
0
                 1.0,1.0,1.0);
81
0
        ofs << buffer << endl;
82
0
        snprintf(buffer, BUFF_SIZE,
83
0
                 "   ALPHA,BETA,GAMMA =%8.3f%8.3f%8.3f    SPGR =    P1"
84
0
                 , 90.0f, 90.0f, 90.0f);
85
0
        ofs << buffer << endl;
86
0
      }
87
0
    else
88
0
      {
89
0
        OBUnitCell *uc = (OBUnitCell*)mol.GetData(OBGenericDataType::UnitCell);
90
0
        snprintf(buffer, BUFF_SIZE,
91
0
                 " REFERENCE STRUCTURE = 00000   A,B,C =%8.3f%8.3f%8.3f",
92
0
                 uc->GetA(), uc->GetB(), uc->GetC());
93
0
        ofs << buffer << endl;
94
0
        snprintf(buffer, BUFF_SIZE,
95
0
                 "   ALPHA,BETA,GAMMA =%8.3f%8.3f%8.3f    SPGR =    P1",
96
0
                 uc->GetAlpha() , uc->GetBeta(), uc->GetGamma());
97
0
        ofs << buffer << endl;
98
0
      }
99
100
0
    snprintf(buffer, BUFF_SIZE, "%4d   1 %s\n",mol.NumAtoms(), mol.GetTitle());
101
0
    ofs << buffer << endl << endl;
102
103
0
    OBAtom *atom,*nbr;
104
0
    vector<OBAtom*>::iterator i;
105
0
    vector<OBBond*>::iterator j;
106
0
    vector<int> vtmp(106,0);
107
0
    int bonds;
108
109
0
    for(atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
110
0
      {
111
        //assign_pdb_number(pdb_types,atom->GetIdx());
112
0
        vtmp[atom->GetAtomicNum()]++;
113
0
        snprintf(buffer, BUFF_SIZE, "%4d%2s%-3d  %9.5f %9.5f %9.5f ",
114
0
                 atom->GetIdx(),
115
0
                 OBElements::GetSymbol(atom->GetAtomicNum()),
116
0
                 vtmp[atom->GetAtomicNum()],
117
0
                 atom->x(),
118
0
                 atom->y(),
119
0
                 atom->z());
120
0
        ofs << buffer;
121
0
        bonds = 0;
122
0
        for (nbr = atom->BeginNbrAtom(j); nbr; nbr = atom->NextNbrAtom(j))
123
0
          {
124
0
            if (bonds > 8) break;
125
0
            snprintf(buffer, BUFF_SIZE, "%4d",nbr->GetIdx());
126
0
            ofs << buffer;
127
0
            bonds++;
128
0
          }
129
0
        for (; bonds < 8; bonds ++)
130
0
          {
131
0
            snprintf(buffer, BUFF_SIZE, "%4d",0);
132
0
            ofs << buffer;
133
0
          }
134
0
        snprintf(buffer, BUFF_SIZE, " %7.3f%4d", atom->GetPartialCharge(), 1);
135
0
        ofs << buffer << endl;
136
0
      }
137
138
0
    return(true);
139
0
  }
140
141
} //namespace OpenBabel