/** Determine what atoms each mask pertains to for the current parm file. */ Action::RetType Action_ReplicateCell::Setup(ActionSetup& setup) { if (setup.Top().SetupIntegerMask( Mask1_ )) return Action::ERR; mprintf("\t%s (%i atoms)\n",Mask1_.MaskString(), Mask1_.Nselected()); if (Mask1_.None()) { mprintf("Warning: One or both masks have no atoms.\n"); return Action::SKIP; } // Set up imaging info for this parm image_.SetupImaging( setup.CoordInfo().TrajBox().Type() ); if (!image_.ImagingEnabled()) { mprintf("Warning: Imaging cannot be performed for topology %s\n", setup.Top().c_str()); return Action::SKIP; } // Create combined topology. if (combinedTop_.Natom() > 0) { // Topology already set up. Check that # atoms matches. if (Mask1_.Nselected() * ncopies_ != combinedTop_.Natom()) { mprintf("Warning: Unit cell can currently only be replicated for" " topologies with same # atoms.\n"); return Action::SKIP; } // Otherwise assume top does not change. } else { // Set up topology and frame. Topology* stripParm = setup.Top().modifyStateByMask( Mask1_ ); if (stripParm == 0) return Action::ERR; for (int cell = 0; cell != ncopies_; cell++) combinedTop_.AppendTop( *stripParm ); combinedTop_.Brief("Combined parm:"); delete stripParm; if (!parmfilename_.empty()) { ParmFile pfile; if (pfile.WriteTopology(combinedTop_, parmfilename_, ParmFile::UNKNOWN_PARM, 0)) { mprinterr("Error: Topology file %s not written.\n", parmfilename_.c_str()); return Action::ERR; } } // Only coordinates for now. FIXME combinedFrame_.SetupFrameM(combinedTop_.Atoms()); // Set up COORDS / output traj if necessary. if (coords_ != 0) coords_->CoordsSetup( combinedTop_, CoordinateInfo() ); if (!trajfilename_.empty()) { if ( outtraj_.PrepareEnsembleTrajWrite(trajfilename_, trajArgs_, &combinedTop_, CoordinateInfo(), setup.Nframes(), TrajectoryFile::UNKNOWN_TRAJ, ensembleNum_) ) return Action::ERR; } } return Action::OK; }
/** Process a mask from the command line. */ int Cpptraj::ProcessMask( Sarray const& topFiles, Sarray const& refFiles, std::string const& maskexpr, bool verbose, bool residue ) const { SetWorldSilent(true); if (topFiles.empty()) { mprinterr("Error: No topology file specified.\n"); return 1; } ParmFile pfile; Topology parm; if (pfile.ReadTopology(parm, topFiles[0], State_.Debug())) return 1; if (!refFiles.empty()) { DataSet_Coords_REF refCoords; if (refCoords.LoadRefFromFile( refFiles[0], parm, State_.Debug())) return 1; parm.SetDistMaskRef( refCoords.RefFrame() ); } if (!verbose) { AtomMask tempMask( maskexpr ); if (parm.SetupIntegerMask( tempMask )) return 1; loudPrintf("Selected="); if (residue) { int res = -1; for (AtomMask::const_iterator atom = tempMask.begin(); atom != tempMask.end(); ++atom) { if (parm[*atom].ResNum() > res) { loudPrintf(" %i", parm[*atom].ResNum()+1); res = parm[*atom].ResNum(); } } } else for (AtomMask::const_iterator atom = tempMask.begin(); atom != tempMask.end(); ++atom) loudPrintf(" %i", *atom + 1); loudPrintf("\n"); } else { if (residue) parm.PrintResidueInfo( maskexpr ); else parm.PrintAtomInfo( maskexpr ); } return 0; }
// Analysis_Hist::Analyze() Analysis::RetType Analysis_Hist::Analyze() { // Set up dimensions // Size of histdata and dimensionArgs should be the same size_t total_bins = 0UL; for (unsigned int hd = 0; hd < N_dimensions_; hd++) { if ( setupDimension(dimensionArgs_[hd], *(histdata_[hd]), total_bins) ) return Analysis::ERR; } // dimensionArgs no longer needed dimensionArgs_.clear(); // Check that the number of data points in each dimension are equal std::vector<DataSet_1D*>::iterator ds = histdata_.begin(); size_t Ndata = (*ds)->Size(); ++ds; for (; ds != histdata_.end(); ++ds) { //mprintf("DEBUG: DS %s size %i\n",histdata[hd]->Name(),histdata[hd]->Xmax()+1); if (Ndata != (*ds)->Size()) { mprinterr("Error: Hist: Dataset %s has inconsistent # data points (%zu), expected %zu.\n", (*ds)->legend(), (*ds)->Size(), Ndata); return Analysis::ERR; } } mprintf("\tHist: %zu data points in each dimension.\n", Ndata); if (calcAMD_ && Ndata != amddata_->Size()) { mprinterr("Error: Hist: AMD data set size (%zu) does not match # expected data points (%zu).\n", amddata_->Size(), Ndata); return Analysis::ERR; } // Allocate bins mprintf("\tHist: Allocating histogram, total bins = %zu\n", total_bins); Bins_.resize( total_bins, 0.0 ); // Bin data for (size_t n = 0; n < Ndata; n++) { long int index = 0; HdimType::const_iterator dim = dimensions_.begin(); OffType::const_iterator bOff = binOffsets_.begin(); for (std::vector<DataSet_1D*>::iterator ds = histdata_.begin(); ds != histdata_.end(); ++ds, ++dim, ++bOff) { double dval = (*ds)->Dval( n ); // Check if data is out of bounds for this dimension. if (dval > dim->Max() || dval < dim->Min()) { index = -1L; break; } // Calculate index for this particular dimension (idx) long int idx = (long int)((dval - dim->Min()) / dim->Step()); if (debug_>1) mprintf(" [%s:%f (%li)],", dim->label(), dval, idx); // Calculate overall index in Bins, offset has already been calcd. index += (idx * (*bOff)); } // If index was successfully calculated, populate bin if (index > -1L && index < (long int)Bins_.size()) { if (debug_ > 1) mprintf(" |index=%li",index); if (calcAMD_) Bins_[index] += exp( amddata_->Dval(n) ); else Bins_[index]++; } else { mprintf("\tWarning: Frame %zu Coordinates out of bounds (%li)\n", n+1, index); } if (debug_>1) mprintf("}\n"); } // Calc free energy if requested if (calcFreeE_) CalcFreeE(); // Normalize if requested if (normalize_ != NO_NORM) Normalize(); if (nativeOut_) { // Use Histogram built-in output PrintBins(); } else { // Using DataFileList framework, set-up labels etc. if (N_dimensions_ == 1) { DataSet_double& dds = static_cast<DataSet_double&>( *hist_ ); // Since Allocate1D only reserves data, use assignment op. dds = Bins_; hist_->SetDim(Dimension::X, dimensions_[0]); } else if (N_dimensions_ == 2) { DataSet_MatrixDbl& mds = static_cast<DataSet_MatrixDbl&>( *hist_ ); mds.Allocate2D( dimensions_[0].Bins(), dimensions_[1].Bins() ); std::copy( Bins_.begin(), Bins_.end(), mds.begin() ); hist_->SetDim(Dimension::X, dimensions_[0]); hist_->SetDim(Dimension::Y, dimensions_[1]); outfile_->ProcessArgs("noxcol usemap nolabels"); } else if (N_dimensions_ == 3) { DataSet_GridFlt& gds = static_cast<DataSet_GridFlt&>( *hist_ ); //gds.Allocate3D( dimensions_[0].Bins(), dimensions_[1].Bins(), dimensions_[2].Bins() ); gds.Allocate_N_O_D( dimensions_[0].Bins(), dimensions_[1].Bins(), dimensions_[2].Bins(), Vec3(dimensions_[0].Min(), dimensions_[1].Min(), dimensions_[2].Min()), Vec3(dimensions_[0].Step(), dimensions_[1].Step(), dimensions_[2].Step()) ); //std::copy( Bins_.begin(), Bins_.end(), gds.begin() ); // FIXME: Copy will not work since in grids data is ordered with Z // changing fastest. Should the ordering in grid be changed? size_t idx = 0; for (size_t z = 0; z < gds.NZ(); z++) for (size_t y = 0; y < gds.NY(); y++) for (size_t x = 0; x < gds.NX(); x++) gds.SetElement( x, y, z, (float)Bins_[idx++] ); hist_->SetDim(Dimension::X, dimensions_[0]); hist_->SetDim(Dimension::Y, dimensions_[1]); hist_->SetDim(Dimension::Z, dimensions_[2]); outfile_->ProcessArgs("noxcol usemap nolabels"); // Create pseudo-topology/trajectory if (!traj3dName_.empty()) { Topology pseudo; pseudo.AddTopAtom(Atom("H3D", 0), Residue("H3D", 1, ' ', ' ')); pseudo.CommonSetup(); if (!parmoutName_.empty()) { ParmFile pfile; if (pfile.WriteTopology( pseudo, parmoutName_, ParmFile::UNKNOWN_PARM, 0 )) mprinterr("Error: Could not write pseudo topology to '%s'\n", parmoutName_.c_str()); } Trajout_Single out; if (out.PrepareTrajWrite(traj3dName_, ArgList(), &pseudo, CoordinateInfo(), Ndata, traj3dFmt_) == 0) { Frame outFrame(1); for (size_t i = 0; i < Ndata; ++i) { outFrame.ClearAtoms(); outFrame.AddVec3( Vec3(histdata_[0]->Dval(i), histdata_[1]->Dval(i), histdata_[2]->Dval(i)) ); out.WriteSingle(i, outFrame); } out.EndTraj(); } else mprinterr("Error: Could not set up '%s' for write.\n", traj3dName_.c_str()); } } } return Analysis::OK; }
// ----- M A I N --------------------------------------------------------------- int main(int argc, char** argv) { SetWorldSilent(true); // No STDOUT output from cpptraj routines. std::string topname, crdname, title, bres, pqr, sybyltype, writeconect; std::string aatm(" pdbatom"), ter_opt(" terbyres"), box(" sg \"P 1\""); TrajectoryFile::TrajFormatType fmt = TrajectoryFile::PDBFILE; bool ctr_origin = false; bool useExtendedInfo = false; int res_offset = 0; int debug = 0; int numSoloArgs = 0; for (int i = 1; i < argc; ++i) { std::string arg( argv[i] ); if (arg == "-p" && i+1 != argc && topname.empty()) // Topology topname = std::string( argv[++i] ); else if (arg == "-c" && i+1 != argc && crdname.empty()) // Coords crdname = std::string( argv[++i] ); else if (arg == "-tit" && i+1 != argc && title.empty()) // Title title = " title " + std::string( argv[++i] ); else if (arg == "-offset" && i+1 != argc) // Residue # offset res_offset = convertToInteger( argv[++i] ); else if ((arg == "-d" || arg == "--debug") && i+1 != argc) // Debug level debug = convertToInteger( argv[++i] ); else if (arg == "-h" || arg == "--help") { // Help Help(argv[0], true); return 0; } else if (arg == "-v" || arg == "--version") { // Version info WriteVersion(); return 0; } else if (arg == "-aatm") // Amber atom names, include extra pts aatm.assign(" include_ep"); else if (arg == "-sybyl") // Amber atom types to SYBYL sybyltype.assign(" sybyltype"); else if (arg == "-conect") // Write CONECT records from bond info writeconect.assign(" conect"); else if (arg == "-ep") // PDB atom names, include extra pts aatm.append(" include_ep"); else if (arg == "-bres") // PDB residue names bres.assign(" pdbres"); else if (arg == "-ext") // Use extended PDB info from Topology useExtendedInfo = true; else if (arg == "-ctr") // Center on origin ctr_origin = true; else if (arg == "-noter") // No TER cards ter_opt.assign(" noter"); else if (arg == "-nobox") // No CRYST1 record box.assign(" nobox"); else if (arg == "-pqr") { // Charge/Radii in occ/bfactor cols pqr.assign(" dumpq"); ++numSoloArgs; } else if (arg == "-mol2") { // output as mol2 fmt = TrajectoryFile::MOL2FILE; ++numSoloArgs; } else if (Unsupported(arg)) { mprinterr("Error: Option '%s' is not yet supported.\n\n", arg.c_str()); return 1; } else { mprinterr("Error: Unrecognized option '%s'\n", arg.c_str()); Help(argv[0], false); return 1; } } if (debug > 0) WriteVersion(); // Check command line for errors. if (topname.empty()) topname.assign("prmtop"); if (debug > 0 && crdname.empty()) mprinterr("| Reading Amber restart from STDIN\n"); if (numSoloArgs > 1) { mprinterr("Error: Only one alternate output format option may be specified (found %i)\n", numSoloArgs); Help(argv[0], true); return 1; } if (!sybyltype.empty() && fmt != TrajectoryFile::MOL2FILE) { mprinterr("Warning: -sybyl is only valid for MOL2 file output.\n"); sybyltype.clear(); } if (debug > 0) { mprinterr("Warning: debug is %i; debug info will be written to STDOUT.\n", debug); SetWorldSilent(false); } // Topology ParmFile pfile; Topology parm; if (pfile.ReadTopology(parm, topname, debug)) { if (topname == "prmtop") Help(argv[0], false); return 1; } if (!useExtendedInfo) parm.ResetPDBinfo(); if (res_offset != 0) for (int r = 0; r < parm.Nres(); r++) parm.SetRes(r).SetOriginalNum( parm.Res(r).OriginalResNum() + res_offset ); ArgList trajArgs; // Input coords Frame TrajFrame; CoordinateInfo cInfo; if (!crdname.empty()) { Trajin_Single trajin; if (trajin.SetupTrajRead(crdname, trajArgs, &parm)) return 1; cInfo = trajin.TrajCoordInfo(); TrajFrame.SetupFrameV(parm.Atoms(), cInfo); trajin.BeginTraj(); if (trajin.ReadTrajFrame(0, TrajFrame)) return 1; trajin.EndTraj(); } else { // Assume Amber restart from STDIN // Check that input is from a redirect. if ( isatty(fileno(stdin)) ) { mprinterr("Error: No coordinates specified with '-c' and no STDIN '<'.\n"); return 1; } Traj_AmberRestart restartIn; restartIn.SetDebug( debug ); //restartIn.processReadArgs( trajArgs ); int total_frames = restartIn.setupTrajin("", &parm); if (total_frames < 1) return 1; cInfo = restartIn.CoordInfo(); TrajFrame.SetupFrameV(parm.Atoms(), cInfo); if (restartIn.openTrajin()) return 1; if (restartIn.readFrame(0, TrajFrame)) return 1; restartIn.closeTraj(); } if (ctr_origin) TrajFrame.CenterOnOrigin(false); // Output coords Trajout_Single trajout; trajArgs.SetList( aatm + bres + pqr + title + ter_opt + box + sybyltype + writeconect, " " ); if ( trajout.PrepareStdoutTrajWrite(trajArgs, &parm, cInfo, 1, fmt) ) return 1; trajout.WriteSingle(0, TrajFrame); trajout.EndTraj(); return 0; }