Coverage Report

Created: 2026-08-31 06:40

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
12
    {
33
12
      OBConversion::RegisterFormat("cssr",this);
34
12
    }
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
2.01k
    {
50
2.01k
      return NOTREADABLE;
51
2.01k
    }
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
2.00k
  {
66
2.00k
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
67
2.00k
    if (pmol == nullptr)
68
0
      return false;
69
70
    //Define some references so we can use the old parameter names
71
2.00k
    ostream &ofs = *pConv->GetOutStream();
72
2.00k
    OBMol &mol = *pmol;
73
74
2.00k
    char buffer[BUFF_SIZE];
75
76
2.00k
    if (!mol.HasData(OBGenericDataType::UnitCell))
77
2.00k
      {
78
2.00k
        snprintf(buffer, BUFF_SIZE,
79
2.00k
                 " REFERENCE STRUCTURE = 00000   A,B,C =%8.3f%8.3f%8.3f",
80
2.00k
                 1.0,1.0,1.0);
81
2.00k
        ofs << buffer << endl;
82
2.00k
        snprintf(buffer, BUFF_SIZE,
83
2.00k
                 "   ALPHA,BETA,GAMMA =%8.3f%8.3f%8.3f    SPGR =    P1"
84
2.00k
                 , 90.0f, 90.0f, 90.0f);
85
2.00k
        ofs << buffer << endl;
86
2.00k
      }
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
2.00k
    snprintf(buffer, BUFF_SIZE, "%4d   1 %s\n",mol.NumAtoms(), mol.GetTitle());
101
2.00k
    ofs << buffer << endl << endl;
102
103
2.00k
    OBAtom *atom,*nbr;
104
2.00k
    vector<OBAtom*>::iterator i;
105
2.00k
    vector<OBBond*>::iterator j;
106
2.00k
    vector<int> vtmp(119,0);
107
2.00k
    int bonds;
108
109
2.00k
    for(atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
110
0
      {
111
        //assign_pdb_number(pdb_types,atom->GetIdx());
112
0
        unsigned int atomicNum = atom->GetAtomicNum();
113
0
        if (atomicNum >= vtmp.size())
114
0
          atomicNum = 0;
115
0
        vtmp[atomicNum]++;
116
0
        snprintf(buffer, BUFF_SIZE, "%4d%2s%-3d  %9.5f %9.5f %9.5f ",
117
0
                 atom->GetIdx(),
118
0
                 OBElements::GetSymbol(atomicNum),
119
0
                 vtmp[atomicNum],
120
0
                 atom->x(),
121
0
                 atom->y(),
122
0
                 atom->z());
123
0
        ofs << buffer;
124
0
        bonds = 0;
125
0
        for (nbr = atom->BeginNbrAtom(j); nbr; nbr = atom->NextNbrAtom(j))
126
0
          {
127
0
            if (bonds > 8) break;
128
0
            snprintf(buffer, BUFF_SIZE, "%4d",nbr->GetIdx());
129
0
            ofs << buffer;
130
0
            bonds++;
131
0
          }
132
0
        for (; bonds < 8; bonds ++)
133
0
          {
134
0
            snprintf(buffer, BUFF_SIZE, "%4d",0);
135
0
            ofs << buffer;
136
0
          }
137
0
        snprintf(buffer, BUFF_SIZE, " %7.3f%4d", atom->GetPartialCharge(), 1);
138
0
        ofs << buffer << endl;
139
0
      }
140
141
2.00k
    return(true);
142
2.00k
  }
143
144
} //namespace OpenBabel