Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Examples

pdb_transform.cc

00001 /* Copyright 2004
00002    Stanford University
00003 
00004    This file is part of the DSR PDB Library.
00005 
00006    The DSR PDB Library is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU Lesser General Public License as published by
00008    the Free Software Foundation; either version 2.1 of the License, or (at your
00009    option) any later version.
00010 
00011    The DSR PDB Library is distributed in the hope that it will be useful, but
00012    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014    License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public License
00017    along with the DSR PDB Library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019    MA 02111-1307, USA. */
00020 
00021 #include "dsrpdb/PDB.h"
00022 #include <cstdlib>
00023 #include <iostream>
00024 #include <fstream>
00025 
00026 #include <dsrpdb/Transform.h>
00027 
00028 #ifdef PDB_USE_BOOST_PROGRAM_OPTIONS
00029 #include <boost/program_options.hpp>
00030 #endif
00031 
00032 
00033 int main(int argc, char *argv[]){
00034   std::string input_file, output_file;
00035   bool print_help=false;
00036   bool verbose=false;
00037   bool dali=false;
00038   bool matrix=false;
00039 
00040 #ifdef PDB_USE_BOOST_PROGRAM_OPTIONS
00041   boost::program_options::options_description o("Allowed options"), po, ao;
00042   o.add_options()
00043     ("help", boost::program_options::bool_switch(&print_help),
00044      "produce help message")
00045     ("dali,d", boost::program_options::bool_switch(&dali), 
00046      "Use a DALI transformation matrix as pasted from an email from DALI.")
00047     ("matrix,m", boost::program_options::bool_switch(&matrix),
00048      "Enter a single transformation matrix.")
00049     ("verbose,v", boost::program_options::bool_switch(&verbose),
00050      "print out any errors that occur during reading of the pdb file.");
00051    
00052   po.add_options()
00053     ("input-pdb", boost::program_options::value< std::string>(&input_file),
00054      "input file")
00055     ("output-pdb", boost::program_options::value< std::string>(&output_file),
00056      "output file");
00057   ao.add(o).add(po);
00058 
00059   boost::program_options::positional_options_description p;
00060   p.add("input-pdb", 1);
00061   p.add("output-pdb", 1);
00062 
00063   boost::program_options::variables_map vm;
00064   boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
00065                                 options(ao).positional(p).run(), vm);
00066   boost::program_options::notify(vm); 
00067 
00068   if (input_file.empty() || output_file.empty() || print_help || (dali && matrix)) {
00069     std::cout << "This program transforms a pdb file by reading a transformation"
00070               << " matrix. The matrix is either specified by pasting the transform lines"
00071               << " from a DALI email, entering a 4x4 transformation matrix (with a"
00072               << " coordinate ordering x,y,z,w) or as separate rotational and "
00073               << " translational components.\n";
00074     std::cout << "usage: " << argv[0] << " input-pdb output-pdb\n\n";
00075     std::cout << o << "\n";
00076     return EXIT_FAILURE;
00077   }
00078 
00079 #else
00080   if (argc != 3){
00081     std::cerr << "The program is built without boost/program_options.hpp.\n";
00082     std::cerr << "useage: " << argv[0] << " [-c] file.pdb output.pdb" << std::endl;
00083     return EXIT_FAILURE;
00084   }
00085   input_file = argv[1];
00086   output_file = argv[2];
00087 #endif
00088 
00089   
00090   // std::cout << input_file << " " << output_template << " " << split_domain << " " << split_chains << std::endl;
00091 
00092   
00093 
00094   //= new char[strlen(argv[2]+1000)];
00095  
00096   double rot[3][3];
00097   double trans[3];
00098   if (dali) {
00099     // const char format[]="%d: %d %s %s %le %le %le %le"
00100     const char format[]="%le %le %le %le";
00101     for (unsigned int i=0; i< 3; ++i){
00102       char buf[1000];
00103       std::cin.getline(buf, 1000);
00104       if (sscanf(buf+27, format, &rot[i][0], &rot[i][1], &rot[i][2], &trans[i]) != 4) {
00105         std::cerr << "Error parsing Dali matrix.\n";
00106         return EXIT_FAILURE;
00107       };
00108       //std::cin >> n >> jc >> js >> js >> x >> y >> z >> t;
00109       //std::cout <<  x << " " << y << " " << z << " " << t << std::endl;
00110     }
00111   } else if (matrix) {
00112     const char format[]="%le %le %le %le";
00113     for (unsigned int i=0; i< 4; ++i){
00114       char buf[1000];
00115       std::cin.getline(buf, 1000);
00116       if (sscanf(buf, format, &rot[i][0], &rot[i][1], &rot[i][2], &trans[i]) != 4) {
00117         std::cerr << "Error parsing matrix, expected 4 floats.\n";
00118         return EXIT_FAILURE;
00119       };
00120     }
00121   } else {
00122     std::cout << "Rotation:\n";
00123     std::cout << "> " << std::flush;
00124     for (unsigned int i=0; i< 3; ++i){
00125       char buf[1000];
00126       std::cin.getline(buf, 1000);
00127       if (sscanf(buf, "%le %le %le", &rot[i][0], &rot[i][1], &rot[i][2]) != 3) {
00128         std::cerr << "Error parsing rotation matrix, expected 3 floats.\n";
00129         return EXIT_FAILURE;
00130       };
00131       if (i != 2) {
00132         std::cout << "> " << std::flush;
00133       }
00134     }
00135     std::cout << "Translation:\n";
00136     std::cout << "> "<< std::flush;
00137     char buf[1000];
00138     std::cin.getline(buf, 1000);
00139     if (sscanf(buf, "%le %le %le", &trans[0], &trans[1], &trans[2]) != 3) {
00140       std::cerr << "Error parsing translation, expected 3 floats.\n";
00141       return EXIT_FAILURE;
00142     };
00143   }
00144   
00145   dsrpdb::Transform t(rot, trans);
00146   std::cout  << t;
00147   std::ifstream in(input_file.c_str());
00148   if (!in) {
00149     std::cerr << "Error opening input file " << input_file << std::endl;
00150     return EXIT_FAILURE;
00151   }
00152 
00153   dsrpdb::PDB pdb(in, verbose);
00154 
00155   if (verbose) std::cout << "Input PDB has " << pdb.number_of_models() << " models." << std::endl;
00156  
00157   for (unsigned int i=0;i< pdb.number_of_models(); ++i){
00158     dsrpdb::Model &m= pdb.model(i);
00159     std::cout << "Model " << i << " has " << m.number_of_chains() << " chains."<< std::endl;
00160     for (unsigned int j=0; j< m.number_of_chains(); ++j){
00161       dsrpdb::Protein &p= m.chain(j);
00162       for (dsrpdb::Protein::Residues_iterator rit= p.residues_begin(); rit != p.residues_end(); ++rit){
00163         for (dsrpdb::Residue::Atoms_iterator ait= rit->atoms_begin(); ait != rit->atoms_end(); ++ait){
00164           ait->second.set_cartesian_coords(t(ait->second.cartesian_coords()));
00165         }
00166       }
00167     }
00168   }
00169 
00170   std::ofstream out(output_file.c_str());
00171   if (!out) {
00172     std::cerr << "Error opening output file " << output_file << std::endl;
00173     return EXIT_FAILURE;
00174   }
00175 
00176   pdb.write(out);
00177 
00178   //delete[] buf;
00179   return EXIT_SUCCESS;
00180 }