Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FormatStringOutputter.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file protocols/outputter/FormatStringOutputter.cc
11 /// @brief An abstract base class that implements format string parsing
12 /// @author Ken Jung
13 
14 // Unit Headers
16 #include <core/pose/Pose.hh>
17 #include <core/pose/util.hh>
18 #include <utility/file/FileName.hh>
19 
20 // tracer
21 #include <basic/Tracer.hh>
22 
23 namespace protocols {
24 namespace outputter {
25 
26 static basic::Tracer TR("protocols.outputter.FormatStringOutputter");
27 
28 #ifdef USELUA
29 void lregister_FormatStringOutputter( lua_State * lstate ) {
30  lregister_Outputter(lstate);
31 
32  luabind::module(lstate, "protocols")
33  [
34  luabind::namespace_("outputter")
35  [
36  luabind::class_<FormatStringOutputter, Outputter>("FormatStringOutputter")
37  .def("format_string", (void (FormatStringOutputter::*) (std::string) ) &FormatStringOutputter::format_string)
38  ]
39  ];
40 }
41 #endif
42 
45 
47  // never should get here
48  return OutputterSP();
49 }
50 
52  TR << "-------Outputting PipeMap to File--------" << std::endl;
53  for( PipeMap::iterator itr = p.begin(); itr != p.end(); itr++ ) {
54  PipeSP current_pipe = itr->second;
55  filenameparts_["pipe_name"] = itr->first;
56  if( current_pipe ) {
57  write( *current_pipe );
58  }
59  }
60 }
61 
63  for( core::Size idx = 0; idx < p.size(); idx++ ) {
64  PoseSP current_pose = p[idx];
65  filenameparts_["pipe_idx"] = idx;
66  if( current_pose ) {
68  core::pose::get_comment(*current_pose, "inputfile", filename );
69  utility::file::FileName f(filename);
70  filenameparts_["filepath"] = f.path();
71  filenameparts_["filename"] = f.base() + f.extension();
72  filenameparts_["filebasename"] = f.base();
73  core::pose::get_comment(*current_pose, "filemultiplier", filenameparts_["filemultiplier"] );
74 
75  write( *current_pose );
76  }
77  }
78 }
79 
80 // walk through format string char by char, look for key matches to filenameparts and replace as needed
81 void FormatStringOutputter::parse_format_string( boost::unordered_map< std::string, std::string> & filenameparts, std::string const & format_string, std::string & filename ) {
82  std::string potential_key = "";
83  bool in_potential_key = false;
84  for( core::Size i = 0; i < format_string.size() ; i++ ) {
85  if( format_string[i] != '%' && ! in_potential_key ) {
86  filename.push_back( format_string[i] );
87  } else if ( format_string[i] == '%' && in_potential_key ) {
88  // the key we though wasn't, so push it onto the filename
89  filename.push_back('%');
90  filename += potential_key;
91  potential_key = "";
92  } else if ( format_string[i] == '%' ) {
93  in_potential_key = true;
94  } else if ( in_potential_key ) {
95  potential_key.push_back(format_string[i]);
96  boost::unordered_map< std::string, std::string>::iterator itr = filenameparts.find( potential_key );
97  if( itr != filenameparts.end() ) {
98  // potential key is real key
99  filename += itr->second;
100  in_potential_key=false;
101  potential_key="";
102  }
103  }
104  }
105  if( in_potential_key )
106  // the rest of the input name was a potential key that turned out to be false, so just append it
107  filename += '%' + potential_key;
108 }
109 
110 
111 #ifdef USELUA
112 void FormatStringOutputter::parse_def( utility::lua::LuaObject const & def,
113  utility::lua::LuaObject const & tasks ) {
114  format_string_ = def["format_string"] ? def["format_string"].to<std::string>() : "%filebasename_%filemultiplier.pdb";
115 }
116 
117 void FormatStringOutputter::lregister( lua_State * lstate ) {
118  lregister_FormatStringOutputter(lstate);
119 }
120 #endif
121 
122 } // outputter
123 } // protocols