1 /* Copyright 2004
2    Stanford University
3 
4    This file is part of the DSR PDB Library.
5 
6    The DSR PDB Library is free software; you can redistribute it and/or modify
7    it under the terms of the GNU Lesser General Public License as published by
8    the Free Software Foundation; either version 2.1 of the License, or (at your
9    option) any later version.
10 
11    The DSR PDB Library is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14    License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public License
17    along with the DSR PDB Library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19    MA 02111-1307, USA. */
20 
21 #include "dsrpdb/PDB.h"
22 #include <cstdlib>
23 #include <iostream>
24 #include <fstream>
25 
26 #include <dsrpdb/Transform.h>
27 
28 #ifdef PDB_USE_BOOST_PROGRAM_OPTIONS
29 #include <boost/program_options.hpp>
30 #endif
31 
32 
main(int argc,char * argv[])33 int main(int argc, char *argv[]){
34   std::string input_file, output_file;
35   bool print_help=false;
36   bool verbose=false;
37   bool dali=false;
38   bool matrix=false;
39 
40 #ifdef PDB_USE_BOOST_PROGRAM_OPTIONS
41   boost::program_options::options_description o("Allowed options"), po, ao;
42   o.add_options()
43     ("help", boost::program_options::bool_switch(&print_help),
44      "produce help message")
45     ("dali,d", boost::program_options::bool_switch(&dali),
46      "Use a DALI transformation matrix as pasted from an email from DALI.")
47     ("matrix,m", boost::program_options::bool_switch(&matrix),
48      "Enter a single transformation matrix.")
49     ("verbose,v", boost::program_options::bool_switch(&verbose),
50      "print out any errors that occur during reading of the pdb file.");
51 
52   po.add_options()
53     ("input-pdb", boost::program_options::value< std::string>(&input_file),
54      "input file")
55     ("output-pdb", boost::program_options::value< std::string>(&output_file),
56      "output file");
57   ao.add(o).add(po);
58 
59   boost::program_options::positional_options_description p;
60   p.add("input-pdb", 1);
61   p.add("output-pdb", 1);
62 
63   boost::program_options::variables_map vm;
64   boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
65 				options(ao).positional(p).run(), vm);
66   boost::program_options::notify(vm);
67 
68   if (input_file.empty() || output_file.empty() || print_help || (dali && matrix)) {
69     std::cout << "This program transforms a pdb file by reading a transformation"
70 	      << " matrix. The matrix is either specified by pasting the transform lines"
71 	      << " from a DALI email, entering a 4x4 transformation matrix (with a"
72 	      << " coordinate ordering x,y,z,w) or as separate rotational and "
73 	      << " translational components.\n";
74     std::cout << "usage: " << argv[0] << " input-pdb output-pdb\n\n";
75     std::cout << o << "\n";
76     return EXIT_FAILURE;
77   }
78 
79 #else
80   if (argc != 3){
81     std::cerr << "The program is built without boost/program_options.hpp.\n";
82     std::cerr << "useage: " << argv[0] << " [-c] file.pdb output.pdb" << std::endl;
83     return EXIT_FAILURE;
84   }
85   input_file = argv[1];
86   output_file = argv[2];
87 #endif
88 
89 
90   // std::cout << input_file << " " << output_template << " " << split_domain << " " << split_chains << std::endl;
91 
92 
93 
94   //= new char[strlen(argv[2]+1000)];
95 
96   double rot[3][3];
97   double trans[3];
98   if (dali) {
99     // const char format[]="%d: %d %s %s %le %le %le %le"
100     const char format[]="%le %le %le %le";
101     for (unsigned int i=0; i< 3; ++i){
102       char buf[1000];
103       std::cin.getline(buf, 1000);
104       if (sscanf(buf+27, format, &rot[i][0], &rot[i][1], &rot[i][2], &trans[i]) != 4) {
105 	std::cerr << "Error parsing Dali matrix.\n";
106 	return EXIT_FAILURE;
107       };
108       //std::cin >> n >> jc >> js >> js >> x >> y >> z >> t;
109       //std::cout <<  x << " " << y << " " << z << " " << t << std::endl;
110     }
111   } else if (matrix) {
112     const char format[]="%le %le %le %le";
113     for (unsigned int i=0; i< 4; ++i){
114       char buf[1000];
115       std::cin.getline(buf, 1000);
116       if (sscanf(buf, format, &rot[i][0], &rot[i][1], &rot[i][2], &trans[i]) != 4) {
117 	std::cerr << "Error parsing matrix, expected 4 floats.\n";
118 	return EXIT_FAILURE;
119       };
120     }
121   } else {
122     std::cout << "Rotation:\n";
123     std::cout << "> " << std::flush;
124     for (unsigned int i=0; i< 3; ++i){
125       char buf[1000];
126       std::cin.getline(buf, 1000);
127       if (sscanf(buf, "%le %le %le", &rot[i][0], &rot[i][1], &rot[i][2]) != 3) {
128 	std::cerr << "Error parsing rotation matrix, expected 3 floats.\n";
129 	return EXIT_FAILURE;
130       };
131       if (i != 2) {
132 	std::cout << "> " << std::flush;
133       }
134     }
135     std::cout << "Translation:\n";
136     std::cout << "> "<< std::flush;
137     char buf[1000];
138     std::cin.getline(buf, 1000);
139     if (sscanf(buf, "%le %le %le", &trans[0], &trans[1], &trans[2]) != 3) {
140       std::cerr << "Error parsing translation, expected 3 floats.\n";
141       return EXIT_FAILURE;
142     };
143   }
144 
145   dsrpdb::Transform t(rot, trans);
146   std::cout  << t;
147   std::ifstream in(input_file.c_str());
148   if (!in) {
149     std::cerr << "Error opening input file " << input_file << std::endl;
150     return EXIT_FAILURE;
151   }
152 
153   dsrpdb::PDB pdb(in, verbose);
154 
155   if (verbose) std::cout << "Input PDB has " << pdb.number_of_models() << " models." << std::endl;
156 
157   for (unsigned int i=0;i< pdb.number_of_models(); ++i){
158     dsrpdb::Model &m= pdb.model(i);
159     std::cout << "Model " << i << " has " << m.number_of_chains() << " chains."<< std::endl;
160     for (unsigned int j=0; j< m.number_of_chains(); ++j){
161       dsrpdb::Protein &p= m.chain(j);
162       for (dsrpdb::Protein::Residues_iterator rit= p.residues_begin(); rit != p.residues_end(); ++rit){
163 	for (dsrpdb::Residue::Atoms_iterator ait= rit->atoms_begin(); ait != rit->atoms_end(); ++ait){
164 	  ait->second.set_cartesian_coords(t(ait->second.cartesian_coords()));
165 	}
166       }
167     }
168   }
169 
170   std::ofstream out(output_file.c_str());
171   if (!out) {
172     std::cerr << "Error opening output file " << output_file << std::endl;
173     return EXIT_FAILURE;
174   }
175 
176   pdb.write(out);
177 
178   //delete[] buf;
179   return EXIT_SUCCESS;
180 }
181