/** For each point p, calculate function Kdist(p) which is the distance of * the Kth nearest point to p. */ void Cluster_DBSCAN::ComputeKdist( int Kval, std::vector<int> const& FramesToCluster ) const { std::vector<double> dists; std::vector<double> Kdist; dists.reserve( FramesToCluster.size() ); Kdist.reserve( FramesToCluster.size() ); std::string outfilename = k_prefix_ + "Kdist." + integerToString(Kval) + ".dat"; mprintf("\tDBSCAN: Calculating Kdist(%i), output to %s\n", Kval, outfilename.c_str()); for (std::vector<int>::const_iterator point = FramesToCluster.begin(); point != FramesToCluster.end(); ++point) { // Store distances from this point dists.clear(); for (std::vector<int>::const_iterator otherpoint = FramesToCluster.begin(); otherpoint != FramesToCluster.end(); ++otherpoint) dists.push_back( FrameDistances_.GetFdist(*point, *otherpoint) ); // Sort distances - first dist should always be 0 std::sort(dists.begin(), dists.end()); Kdist.push_back( dists[Kval] ); } std::sort( Kdist.begin(), Kdist.end() ); CpptrajFile Outfile; Outfile.OpenWrite(outfilename); Outfile.Printf("%-8s %1i%-11s\n", "#Point", Kval,"-dist"); // Write out largest to smallest unsigned int ik = 0; for (std::vector<double>::reverse_iterator k = Kdist.rbegin(); k != Kdist.rend(); ++k, ++ik) Outfile.Printf("%8u %12.4f\n", ik, *k); Outfile.CloseFile(); }
// DataIO_Std::WriteData() int DataIO_Std::WriteData(FileName const& fname, DataSetList const& SetList) { int err = 0; if (!SetList.empty()) { // Open output file. CpptrajFile file; if (file.OpenWrite( fname )) return 1; // Base write type off first data set dimension FIXME if (SetList[0]->Group() == DataSet::CLUSTERMATRIX) { // Special case of 2D - may have sieved frames. err = WriteCmatrix(file, SetList); } else if (SetList[0]->Ndim() == 1) { if (group_ == NO_TYPE) { if (isInverted_) err = WriteDataInverted(file, SetList); else err = WriteDataNormal(file, SetList); } else err = WriteByGroup(file, SetList, group_); } else if (SetList[0]->Ndim() == 2) err = WriteData2D(file, SetList); else if (SetList[0]->Ndim() == 3) err = WriteData3D(file, SetList); file.CloseFile(); } return err; }
/** \return true if TRR/TRJ file. */ bool Traj_GmxTrX::ID_TrajFormat(CpptrajFile& infile) { // File must already be set up for read if (infile.OpenFile()) return false; bool istrx = IsTRX(infile); infile.CloseFile(); return istrx; }
// Action_ClusterDihedral::ReadDihedrals() int Action_ClusterDihedral::ReadDihedrals(std::string const& fname) { CpptrajFile infile; char buffer[256]; int a1, a2, a3, a4, bins; double min; if ( infile.OpenRead( fname ) ) return 1; mprintf("\tReading dihedral information from %s\n", fname.c_str()); while (infile.Gets(buffer, 256)==0) { // Expected line format: At#1 At#2 At#3 At#4 Bins Min // ATOM NUMBERS SHOULD START FROM 1! int nvals = sscanf(buffer, "%i %i %i %i %i %lf", &a1, &a2, &a3, &a4, &bins, &min); if (nvals < 5) { mprinterr("Error: Dihedral file %s: Expected at least 5 values, got %i\n", fname.c_str(), nvals); mprinterr("Error: Problem line: [%s]\n",buffer); mprinterr("Error: Expected format: At#1 At#2 At#3 At#4 Bins [Min]\n"); return 1; // This should automatically close infile through destructor. } if (nvals < 6) min = minimum_; DCmasks_.push_back( DCmask(a1-1, a2-1, a3-1, a4-1, bins, min ) ); mprintf("\t\t(%i)-(%i)-(%i)-(%i) Bins=%i Min=%.3f\n",a1,a2,a3,a4,bins,min); } mprintf("\tRead %zu dihedrals.\n", DCmasks_.size()); infile.CloseFile(); return 0; }
// WriteNameToBuffer() void DataIO_Std::WriteNameToBuffer(CpptrajFile& fileIn, std::string const& label, int width, bool isLeftCol) { std::string temp_name = label; // If left aligning, add '#' to name; if (isLeftCol) { if (temp_name[0]!='#') { temp_name.insert(0,"#"); // Ensure that name will not be larger than column width. if ((int)temp_name.size() > width) temp_name.resize( width ); } } // Replace any spaces with underscores for (std::string::iterator tc = temp_name.begin(); tc != temp_name.end(); ++tc) if ( *tc == ' ' ) *tc = '_'; if (width >= (int)CpptrajFile::BUF_SIZE) // Protect against CpptrajFile buffer overflow fileIn.Write(temp_name.c_str(), temp_name.size()); else { // Set up header format string TextFormat::AlignType align; if (isLeftCol) align = TextFormat::LEFT; else align = TextFormat::RIGHT; TextFormat header_format(TextFormat::STRING, width, align); fileIn.Printf(header_format.fmt(), temp_name.c_str()); } }
int DataIO_OpenDx::WriteGrid(DataSet const& setIn, CpptrajFile& outfile) const { DataSet_3D const& set = static_cast<DataSet_3D const&>( setIn ); Vec3 oxyz = set.Bin().GridOrigin(); if (gridWriteMode_ == BIN_CENTER) // Origin needs to be shifted to center of bin located at 0,0,0 oxyz = set.Bin().Center(0,0,0); // Print the OpenDX header WriteDxHeader(outfile, set.NX(), set.NY(), set.NZ(), set.NX(), set.NY(), set.NZ(), set.Bin().Ucell(), oxyz); // Now print out the data. size_t gridsize = set.Size(); if (gridsize == 1) outfile.Printf("%g\n", set[0]); else if (gridsize == 2) outfile.Printf("%g %g\n", set[0], set[1]); else if (gridsize > 2) { // Data is already in row-major form (z-axis changes // fastest), so no need to do any kind of data adjustment for (size_t i = 0UL; i < gridsize - 2UL; i += 3UL) outfile.Printf("%g %g %g\n", set[i], set[i+1], set[i+2]); // Print out any points we may have missed switch (gridsize % 3) { case 2: outfile.Printf("%g %g\n", set[gridsize-2], set[gridsize-1]); break; case 1: outfile.Printf("%g\n", set[gridsize-1]); break; } } return 0; }
bool Parm_CharmmPsf::ID_ParmFormat(CpptrajFile& fileIn) { // Assumes already set up if (fileIn.OpenFile()) return false; std::string nextLine = fileIn.GetLine(); if (nextLine.empty()) return false; bool isPSF = ( nextLine.compare(0, 3, "PSF") == 0 ); fileIn.CloseFile(); return isPSF; }
void DataSet_GridDbl::WriteBuffer(CpptrajFile& outfile, SizeArray const& pIn) const { size_t x = pIn[0]; size_t y = pIn[1]; size_t z = pIn[2]; if ( x >= grid_.NX() || y >= grid_.NY() || z >= grid_.NZ() ) outfile.Printf(format_.fmt(), 0.0); else outfile.Printf(format_.fmt(), grid_.element(x,y,z)); }
bool DataIO_OpenDx::ID_DataFormat( CpptrajFile& infile ) { bool isDX = false; if (!infile.OpenFile()) { std::string firstLine = infile.GetLine(); if (!firstLine.empty()) isDX = (firstLine.compare(0, 28, "object 1 class gridpositions") == 0); infile.CloseFile(); } return isDX; }
// PDBfile::ID_PDB() bool PDBfile::ID_PDB(CpptrajFile& fileIn) { // NOTE: ASSUME FILE SET UP FOR READ if (fileIn.OpenFile()) return false; std::string line1 = fileIn.GetLine(); std::string line2 = fileIn.GetLine(); fileIn.CloseFile(); if (!IsPDBkeyword( line1 )) return false; if (!IsPDBkeyword( line2 )) return false; return true; }
// DataSet_Vector::WriteBuffer() void DataSet_Vector::WriteBuffer(CpptrajFile &cbuffer, SizeArray const& pIn) const { if (pIn[0] >= vectors_.size()) { cbuffer.Printf(format_.fmt(), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // VXYZ OXYZ } else { Vec3 const& Vxyz = vectors_[pIn[0]]; Vec3 const& Oxyz = OXYZ(pIn[0]); cbuffer.Printf(format_.fmt(), Vxyz[0], Vxyz[1], Vxyz[2], Oxyz[0], Oxyz[1], Oxyz[2]); } }
void TriangleMatrix::Write2D( CpptrajFile& outfile, int xIn, int yIn ) { size_t x = (size_t)xIn; size_t y = (size_t)yIn; if ( xIn==yIn || xIn < 0 || yIn < 0 || x >= nrows_ || y >= nrows_ ) outfile.Printf(data_format_, 0.0); else { size_t index = calcIndex(x, y); outfile.Printf(data_format_, elements_[index]); } }
// TODO: Accept const ArgList so arguments are not reset? CpptrajFile* DataFileList::AddCpptrajFile(FileName const& nameIn, std::string const& descrip, CFtype typeIn, bool allowStdout) { // If no filename and stdout not allowed, no output desired. if (nameIn.empty() && !allowStdout) return 0; FileName name; CpptrajFile* Current = 0; int currentIdx = -1; if (!nameIn.empty()) { name = nameIn; // Append ensemble number if set. if (ensembleNum_ != -1) name.Append( "." + integerToString(ensembleNum_) ); // Check if filename in use by DataFile. DataFile* df = GetDataFile(name); if (df != 0) { mprinterr("Error: Text output file name '%s' already in use by data file '%s'.\n", nameIn.full(), df->DataFilename().full()); return 0; } // Check if this filename already in use currentIdx = GetCpptrajFileIdx( name ); if (currentIdx != -1) Current = cfList_[currentIdx]; } // If no CpptrajFile associated with name, create new CpptrajFile if (Current==0) { switch (typeIn) { case TEXT: Current = new CpptrajFile(); break; case PDB: Current = (CpptrajFile*)(new PDBfile()); break; } Current->SetDebug(debug_); // Set up file for writing. //if (Current->SetupWrite( name, debug_ )) if (Current->OpenWrite( name )) { mprinterr("Error: Setting up text output file %s\n", name.full()); delete Current; return 0; } cfList_.push_back( Current ); cfData_.push_back( CFstruct(descrip, typeIn) ); } else { // If Current type does not match typeIn do not allow. if (typeIn != cfData_[currentIdx].Type()) { mprinterr("Error: Cannot change type of text output for '%s'.\n", Current->Filename().full()); return 0; } Current->SetDebug(debug_); // Update description if (!descrip.empty()) cfData_[currentIdx].UpdateDescrip( descrip ); } return Current; }
// DataIO_CCP4::ID_DataFormat() bool DataIO_CCP4::ID_DataFormat( CpptrajFile& infile ) { bool isCCP4 = false; if (!infile.OpenFile()) { unsigned char MAP[4]; if (infile.Seek(52 * wSize) == 0) { infile.Read( MAP, wSize ); isCCP4 = MapCharsValid( MAP ); } infile.CloseFile(); } return isCCP4; }
// DataIO_CCP4::WriteData() int DataIO_CCP4::WriteData(FileName const& fname, DataSetList const& setList) { // Open output file CpptrajFile outfile; if (outfile.OpenWrite(fname)) { mprinterr("Error: Could not open CCP4 output file '%s'.\n", fname.full()); return 1; } // Warn about writing multiple sets if (setList.size() > 1) mprintf("Warning: %s: Writing multiple 3D sets in CCP4 format not supported.\n" "Warning: Only writing first set.\n", fname.full()); return WriteSet3D( setList.begin(), outfile ); }
// ParmFile::DetectFormat() ParmIO* ParmFile::DetectFormat(FileName const& fname, ParmFormatType& ptype) { CpptrajFile file; if (file.SetupRead(fname, 0) == 0) { for (int i = 0; i < (int)UNKNOWN_PARM; i++) { ptype = (ParmFormatType)i; ParmIO* IO = (ParmIO*)FileTypes::AllocIO(PF_AllocArray, ptype, true ); if (IO != 0 && IO->ID_ParmFormat( file )) return IO; delete IO; } } ptype = UNKNOWN_PARM; return 0; }
// TrajectoryFile::DetectFormat() TrajectoryIO* TrajectoryFile::DetectFormat(FileName const& fname, TrajFormatType& ttype) { CpptrajFile file; if (file.SetupRead(fname, 0) == 0) { for (int i = 0; i < (int)UNKNOWN_TRAJ; i++) { ttype = (TrajFormatType)i; TrajectoryIO* IO = (TrajectoryIO*)FileTypes::AllocIO(TF_AllocArray, ttype, true ); if (IO != 0 && IO->ID_TrajFormat( file )) return IO; delete IO; } } ttype = UNKNOWN_TRAJ; return 0; }
/** Create new DataFile, or return existing DataFile. */ DataFile* DataFileList::AddDataFile(FileName const& nameIn, ArgList& argIn, DataFile::DataFormatType typeIn) { // If no filename, no output desired if (nameIn.empty()) return 0; FileName fname( nameIn ); // Append ensemble number if set. //rprintf("DEBUG: Setting up data file '%s' with ensembleNum %i\n", nameIn.base(), ensembleNum_); if (ensembleNum_ != -1) fname.Append( "." + integerToString(ensembleNum_) ); // Check if filename in use by CpptrajFile. CpptrajFile* cf = GetCpptrajFile(fname); if (cf != 0) { mprinterr("Error: Data file name '%s' already in use by text output file '%s'.\n", fname.full(), cf->Filename().full()); return 0; } // Check if this filename already in use DataFile* Current = GetDataFile(fname); // If no DataFile associated with name, create new DataFile if (Current==0) { Current = new DataFile(); if (Current->SetupDatafile(fname, argIn, typeIn, debug_)) { mprinterr("Error: Setting up data file %s\n", fname.full()); delete Current; return 0; } fileList_.push_back(Current); } else { // Set debug level Current->SetDebug(debug_); // If a type was specified, make sure it matches. if (typeIn != DataFile::UNKNOWN_DATA && typeIn != Current->Type()) { mprinterr("Error: '%s' is type %s but has been requested as type %s.\n", Current->DataFilename().full(), Current->FormatString(), DataFile::FormatString( typeIn )); return 0; } // Check for keywords that do not match file type DataFile::DataFormatType kType = DataFile::GetFormatFromArg( argIn ); if (kType != DataFile::UNKNOWN_DATA && kType != Current->Type()) mprintf("Warning: %s is type %s but type %s keyword specified; ignoring keyword.\n", Current->DataFilename().full(), Current->FormatString(), DataFile::FormatString( kType )); // Process Arguments if (!argIn.empty()) Current->ProcessArgs( argIn ); } return Current; }
/** Determine if fileIn is a CIF file. Look for entries beginning with * an underscore (indicating data block), and a 'loop_' keyword or * '_entry.id' block. */ bool CIFfile::ID_CIF(CpptrajFile& fileIn) { // NOTE: ASSUME FILE SET UP FOR READ if (fileIn.OpenFile()) return false; int ndata = 0; // Number of '_XXX' entries seen bool foundLoop = false; bool foundEntryID = false; for (int i = 0; i < 10; i++) { std::string lineIn = fileIn.GetLine(); if (lineIn[0] == '_') ndata++; if (lineIn.compare(0,5,"loop_")==0) foundLoop = true; if (lineIn.compare(0,9,"_entry.id")==0) foundEntryID = true; } fileIn.CloseFile(); return ( ndata > 2 && (foundLoop || foundEntryID) ); }
// DataIO_Mdout::ID_DataFormat() bool DataIO_Mdout::ID_DataFormat(CpptrajFile& infile) { if (infile.OpenFile()) return false; bool isMdout = false; std::string line = infile.GetLine(); if (line[0] == '\n') { line = infile.GetLine(); if (line.compare(0, 15, " -----") == 0) { line = infile.GetLine(); if (line.compare(0, 15, " Amber") == 0) isMdout = true; } } infile.CloseFile(); return isMdout; }
bool SDFfile::ID_SDF(CpptrajFile& fileIn) { // NOTE: ASSUMES FILE IS ALREADY SETUP! if (fileIn.OpenFile()) return false; // Search for V2000 somewhere in line 4 const char* ptr = 0; for (int i = 0; i < 4; i++) if ( (ptr = fileIn.NextLine()) == 0 ) { fileIn.CloseFile(); return false; } fileIn.CloseFile(); std::string line( ptr ); // Line 4, Connection table if ( line.find( "V2000" ) != std::string::npos ) return true; return false; }
void Cluster_DPeaks::ClusterResults(CpptrajFile& outfile) const { outfile.Printf("#Algorithm: DPeaks epsilon %g\n", epsilon_); if (calc_noise_) { outfile.Printf("#NOISE_FRAMES:"); std::vector<int> noiseFrames; for (Carray::const_iterator point = Points_.begin(); point != Points_.end(); ++point) if (point->Cnum() == -1) noiseFrames.push_back( point->Fnum()+1 ); std::sort( noiseFrames.begin(), noiseFrames.end() ); for (std::vector<int>::const_iterator f = noiseFrames.begin(); f != noiseFrames.end(); ++f) outfile.Printf(" %i", *f); outfile.Printf("\n"); outfile.Printf("#Number_of_noise_frames: %zu\n", noiseFrames.size()); } }
// DataIO_OpenDx::WriteData() int DataIO_OpenDx::WriteData(FileName const& fname, DataSetList const& setList) { // Open output file CpptrajFile outfile; if (outfile.OpenWrite(fname)) { mprinterr("Error: Could not open OpenDX output file.\n"); return 1; } // Warn about writing multiple sets if (setList.size() > 1) mprintf("Warning: %s: Writing multiple 3D sets in OpenDX format may result in unexpected behavior\n", fname.full()); int err = 0; for (DataSetList::const_iterator set = setList.begin(); set != setList.end(); ++set) err += WriteSet3D( *(*set), outfile ); return err; }
// DataIO_Std::WriteByGroup() int DataIO_Std::WriteByGroup(CpptrajFile& file, DataSetList const& SetList, GroupType gtype) { int err = 0; bool firstWrite = true; DataSetList tmpdsl; std::vector<bool> setIsWritten(SetList.size(), false); unsigned int startIdx = 0; unsigned int nWritten = 0; while (nWritten < SetList.size()) { std::string currentName; Dimension currentDim; int currentNum = -1; switch (gtype) { case BY_NAME : currentName = SetList[startIdx]->Meta().Name(); break; case BY_ASPECT : currentName = SetList[startIdx]->Meta().Aspect(); break; case BY_IDX : currentNum = SetList[startIdx]->Meta().Idx(); break; case BY_ENS : currentNum = SetList[startIdx]->Meta().EnsembleNum(); break; case BY_DIM : currentDim = SetList[startIdx]->Dim(0); break; case NO_TYPE : return 1; } int firstNonMatch = -1; for (unsigned int idx = startIdx; idx != SetList.size(); idx++) { if (!setIsWritten[idx]) { bool match = false; switch (gtype) { case BY_NAME : match = (currentName == SetList[idx]->Meta().Name()); break; case BY_ASPECT : match = (currentName == SetList[idx]->Meta().Aspect()); break; case BY_IDX : match = (currentNum == SetList[idx]->Meta().Idx()); break; case BY_ENS : match = (currentNum == SetList[idx]->Meta().EnsembleNum()); break; case BY_DIM : match = (currentDim == SetList[idx]->Dim(0)); break; case NO_TYPE : return 1; } if (match) { tmpdsl.AddCopyOfSet( SetList[idx] ); setIsWritten[idx] = true; nWritten++; } else if (firstNonMatch == -1) firstNonMatch = (int)idx; } } if (firstNonMatch > -1) startIdx = (unsigned int)firstNonMatch; if (!firstWrite) file.Printf("\n"); else firstWrite = false; if (isInverted_) err += WriteDataInverted(file, tmpdsl); else err += WriteDataNormal(file, tmpdsl); tmpdsl.ClearAll(); } return err; }
int Parm_CharmmPsf::FindTag(char* tag, const char* target, int tgtsize, CpptrajFile& infile) { int nval = 0; while (strncmp(tag,target,tgtsize)!=0) { const char* buffer = infile.NextLine(); if ( buffer == 0 ) return 0; sscanf(buffer,"%i %10s",&nval,tag); } return nval; }
// DataIO_Std::WriteData3D() int DataIO_Std::WriteData3D( CpptrajFile& file, DataSetList const& setList) { int err = 0; for (DataSetList::const_iterator set = setList.begin(); set != setList.end(); ++set) { if (set != setList.begin()) file.Printf("\n"); err += WriteSet3D( *(*set), file ); } return err; }
// Cluster_DBSCAN::ClusterResults() void Cluster_DBSCAN::ClusterResults(CpptrajFile& outfile) const { outfile.Printf("#Algorithm: DBSCAN minpoints %i epsilon %g sieveToCentroid %i\n", minPoints_, epsilon_, (int)sieveToCentroid_); // List the number of noise points. outfile.Printf("#NOISE_FRAMES:"); unsigned int frame = 1; unsigned int numNoise = 0; for (std::vector<char>::const_iterator stat = Status_.begin(); stat != Status_.end(); ++stat, ++frame) { if ( *stat == NOISE ) { outfile.Printf(" %i", frame); ++numNoise; } } outfile.Printf("\n"); outfile.Printf("#Number_of_noise_frames: %u\n", numNoise); }
/** \return true if TRR/TRJ file. Determine endianness. */ bool Traj_GmxTrX::IsTRX(CpptrajFile& infile) { int magic; if ( infile.Read( &magic, 4 ) != 4 ) return 1; if (magic != Magic_) { // See if this is big endian endian_swap( &magic, 1 ); if (magic != Magic_) return false; else isBigEndian_ = true; } else isBigEndian_ = false; // TODO: At this point file is trX, but not sure how best to differentiate // between TRR and TRJ. For now do it based on extension. Default TRR. if (infile.Filename().Ext() == ".trr") format_ = TRR; else if (infile.Filename().Ext() == ".trj") format_ = TRJ; else format_ = TRR; return true; }
//------------------------------------------------------------------------ bool Traj_Conflib::ID_TrajFormat(CpptrajFile& fileIn) { // If the file name is conflib.dat, assume this is a conflib.dat file // from LMOD. Cant think of a better way to detect this since there is no // magic number but the file is binary. if ( fileIn.Filename().Base() == "conflib.dat" ) { mprintf(" LMOD CONFLIB file\n"); return true; } return false; }
bool Traj_CharmmCor::ID_TrajFormat(CpptrajFile& fileIn) { // File must already be set up for read. if (fileIn.OpenFile()) return false; bool isCor = false; const char* ptr = fileIn.NextLine(); // Must be at least 1 title line denoted with '*' if (ptr != 0 && *ptr == '*') { // Scan past all title lines while (ptr != 0 && *ptr == '*') ptr = fileIn.NextLine(); if (ptr != 0) { // Next line must be # atoms ONLY int ibuf[2]; if (sscanf(ptr, "%i %i", ibuf, ibuf+1) == 1) // make sure it was a valid integer isCor = (ibuf[0] > 0); } } fileIn.CloseFile(); return isCor; }