| 1 | /********************************************************************** | 
| 2 | Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. | 
| 3 | Some portions Copyright (C) 2001-2005 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 | /* contributed by Walter Scott (wscott@igc.phys.chem.ethz.ch) | 
| 17 |  | 
| 18 | (Actually the routine was copied from write_xyz and and write_pdb and | 
| 19 | then modified...) | 
| 20 |  | 
| 21 | This is a small routine to write a GROMOS96 formatted | 
| 22 | "position coordinate block" (POSITION) or a | 
| 23 | "reduced position coordinate block" (POSITIONRED) | 
| 24 | The former has name information (atom and residue names) while | 
| 25 | the latter only has coordinates. | 
| 26 | This version does not support the writing of binary | 
| 27 | GROMOS files. | 
| 28 |  | 
| 29 | NOTE 1: the actual formats used in writing out the coordinates | 
| 30 | do not matter, as GROMOS96 uses free formatted reads. | 
| 31 | Each line may not be longer than 80 characters. | 
| 32 |  | 
| 33 | (Note, however, in the POSITION block, the first 24 (twenty four) | 
| 34 | character on each line are ignored when the line is read in by GROMOS) | 
| 35 | Comments lines, beginning with hash (#) may occur within a block and are | 
| 36 | used as delimiters for easier reading. | 
| 37 |  | 
| 38 | NOTE 2: Many programs specify the units of the coordinates (e.g. Angstrom). | 
| 39 | GROMOS96 does NOT, as all physical constants, from K_B to EPS are | 
| 40 | NOT hardwired into the code, but specified by the user. | 
| 41 | This allows some (mostly Americans) to use GROMOS96 in KCal and | 
| 42 | Angstrom and the rest of us to use kJoule and nm. | 
| 43 | It also makes it easy to use reduced units. | 
| 44 |  | 
| 45 | We get around this by supplying a routine, wr_sco_gr96, which | 
| 46 | will scale the coordinates by a factor before writing. | 
| 47 | This routine is then called with the factor set to 1.0 in | 
| 48 | write_gr96A, or to 0.1 in write_gr96N depending on the users choice. | 
| 49 | Thus, we always assume that we have read coordinates in Angstrom. | 
| 50 | *** But now handled by a command line option in new framework. | 
| 51 | */ | 
| 52 |  | 
| 53 | #include "gromos96format.hpp" | 
| 54 |  | 
| 55 |  | 
| 56 | using namespace std; | 
| 57 | namespace OpenBabel | 
| 58 | { | 
| 59 | bool GROMOS96Format::WriteMolecule(OBBase* pOb, OBConversion* pConv) | 
| 60 | { | 
| 61 | OBMol* pmol = dynamic_cast<OBMol*>(pOb); | 
| 62 | if(pmol==NULL) | 
| 63 | return false; | 
| 64 |  | 
| 65 | //Define some references so we can use the old parameter names | 
| 66 | ostream &ofs = *pConv->GetOutStream(); | 
| 67 | OBMol &mol = *pmol; | 
| 68 | double fac = pConv->IsOption("n") ? 0.1 : 1.0; //new framework | 
| 69 |  | 
| 70 | char type_name[10]; | 
| 71 | char res_name[10],padded_name[10]; | 
| 72 | char buffer[BUFF_SIZE]; | 
| 73 | int res_num; | 
| 74 |  | 
| 75 | sprintf(buffer,"#GENERATED BY OPEN BABEL %s",BABEL_VERSION); | 
| 76 | ofs << buffer << endl; | 
| 77 |  | 
| 78 | /* GROMOS wants a TITLE block, so let's write one*/ | 
| 79 | sprintf(buffer,"TITLE\n%s\nEND",mol.GetTitle()); | 
| 80 | ofs << buffer << endl; | 
| 81 | ofs << "POSITION" << endl; | 
| 82 |  | 
| 83 | OBAtom *atom; | 
| 84 | OBResidue *res; | 
| 85 | vector<OBNodeBase*>::iterator i; | 
| 86 |  | 
| 87 | for(atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i)) | 
| 88 | { | 
| 89 | if ( (res = atom->GetResidue()) ) | 
| 90 | { | 
| 91 | strcpy(res_name,(char*)res->GetName().c_str()); | 
| 92 | strcpy(type_name,(char*)res->GetAtomID(atom).c_str()); | 
| 93 | res_num = res->GetNum(); | 
| 94 | } | 
| 95 | else | 
| 96 | { | 
| 97 | strcpy(type_name,etab.GetSymbol(atom->GetAtomicNum())); | 
| 98 | strcpy(res_name,"UNK"); | 
| 99 | sprintf(padded_name,"%2s",type_name); | 
| 100 | strcpy(type_name,padded_name); | 
| 101 | res_num = 1; | 
| 102 | } | 
| 103 |  | 
| 104 | sprintf(buffer,"%5d %5s %5s %6d %15.5f %15.5f %15.5f", | 
| 105 | res_num,res_name,type_name,atom->GetIdx(), | 
| 106 | atom->x()*fac,atom->y()*fac,atom->z()*fac); | 
| 107 | ofs << buffer << endl; | 
| 108 |  | 
| 109 | if (!(atom->GetIdx()%10)) | 
| 110 | { | 
| 111 | sprintf(buffer,"# %d",atom->GetIdx()); | 
| 112 | ofs << buffer << endl; | 
| 113 | } | 
| 114 | } | 
| 115 |  | 
| 116 | ofs << "END" << endl; | 
| 117 |  | 
| 118 | return(true); | 
| 119 | } | 
| 120 |  | 
| 121 | } //namespace OpenBabel |