bool SDFfile::ReadHeader() { if (!IsOpen()) return true; // Read title title_ = GetLine(); RemoveTrailingWhitespace( title_ ); // Read past info and comment if (NextLine() == 0) return true; if (NextLine() == 0) return true; // Read Connection table const char* ptr = NextLine(); if (ptr == 0) return true; if (sscanf(ptr, "%i %i", &Natoms_, &Nbonds_) != 2) return true; return false; }
bool Traj_Gro::ID_TrajFormat(CpptrajFile& infile) { // Title line, atoms line, then resnum, resname, atomname, atomnum, X, Y, Z if (infile.OpenFile()) return false; int nread = 0; if (infile.NextLine() != 0) { // Title const char* ptr = infile.NextLine(); // Natom if (ptr != 0) { // Ensure only a single value on # atoms line std::string natom_str( ptr ); RemoveTrailingWhitespace( natom_str ); if (validInteger(natom_str)) { ptr = infile.NextLine(); // First atom if (ptr != 0) { char resnum[6], resname[6], atname[6], atnum[6]; float XYZ[3]; nread = sscanf(ptr, "%5c%5c%5c%5c%f %f %f", resnum, resname, atname, atnum, XYZ, XYZ+1, XYZ+2); } } } } infile.CloseFile(); return (nread == 7); }
std::string NoTrailingWhitespace(std::string const& line) { std::string duplicate(line); RemoveTrailingWhitespace(duplicate); return duplicate; }
int Traj_Gro::setupTrajin(FileName const& fnameIn, Topology* trajParm) { float fXYZ[9]; fname_ = fnameIn; // TODO SetupRead for BufferedLine // Open file for reading if (file_.OpenFileRead( fname_ )) return TRAJIN_ERR; // Read the title. May contain time value, 't= <time>' const char* ptr = file_.Line(); if (ptr == 0) { mprinterr("Error: Reading title.\n"); return TRAJIN_ERR; } std::string title( ptr ); RemoveTrailingWhitespace(title); mprintf("DBG: Title: %s\n", title.c_str()); bool hasTime = true; // TODO Is it OK to assume there will never be a negative time value? double timeVal = GetTimeValue( ptr ); if (timeVal < 0.0) hasTime = false; mprintf("DBG: Timeval= %g HasTime= %i\n", timeVal, (int)hasTime); // Read number of atoms ptr = file_.Line(); if (ptr == 0) return TRAJIN_ERR; natom_ = atoi(ptr); if (natom_ < 1) { mprinterr("Error: Reading number of atoms.\n"); return TRAJIN_ERR; } if (natom_ != trajParm->Natom()) { mprinterr("Error: Number of atoms %i does not match associated parm %s (%i)\n", natom_, trajParm->c_str(), trajParm->Natom()); return TRAJIN_ERR; } // Read first atom to see if there are velocities ptr = file_.Line(); int nread = sscanf(ptr, "%*5c%*5c%*5c%*5c%f %f %f %f %f %f", fXYZ, fXYZ+1, fXYZ+2, fXYZ+3, fXYZ+4, fXYZ+5); bool hasV = false; if (nread == 6) hasV = true; else if (nread != 3) { mprinterr("Error: Reading first atom, expected 3 or 6 coordinates, got %i\n", nread); return TRAJIN_ERR; } // Read past the rest of the atoms for (int i = 1; i != natom_; i++) if (file_.Line() == 0) { mprinterr("Error: Reading atom %i of first frame.\n", i+1); return TRAJIN_ERR; } // Attempt to read box line // v1(x) v2(y) v3(z) [v1(y) v1(z) v2(x) v2(z) v3(x) v3(y)] ptr = file_.Line(); Box groBox; if (ptr != 0) groBox = GetBox( ptr ); // Set trajectory information. No temperature info. SetCoordInfo( CoordinateInfo(groBox, hasV, false, hasTime) ); SetTitle( title ); // Check for multiple frames. If nothing was read above, 1 frame, no box. If // box info was read but nothing more can be read, 1 frame. Otherwise there // are more frames. bool hasMultipleFrames = false; if (ptr != 0) { if (groBox.Type() == Box::NOBOX) // Assume another title read. hasMultipleFrames = true; else { ptr = file_.Line(); // Read line after box info if (ptr != 0) hasMultipleFrames = true; } } // Set up some info for performing blank reads. linesToRead_ = natom_; if (groBox.Type() != Box::NOBOX) linesToRead_ += 1; int nframes = 1; if (hasMultipleFrames) { // Since there is no guarantee that each frame is the same size we cannot // just seek. Blank reads for as many times as possible. Should currently // be positioned at the title line of the next frame. while (ptr != 0 ) { ptr = file_.Line(); // Natoms int Nat = atoi(ptr); if (Nat != natom_) { mprinterr("Error: Frame %i # atoms (%i) does not match first frame (%i).\n" "Error: Only reading %i frames.\n", nframes+1, Nat, natom_, nframes); break; } for (int i = 0; i != linesToRead_; i++) ptr = file_.Line(); if (ptr == 0) break; nframes++; ptr = file_.Line(); // Next title or EOF } } file_.CloseFile(); return nframes; }