void list_xtc(const char *fn) { t_fileio *xd; int indent; char buf[256]; rvec *x; matrix box; int nframe, natoms, step; real prec, time; gmx_bool bOK; xd = open_xtc(fn, "r"); read_first_xtc(xd, &natoms, &step, &time, box, &x, &prec, &bOK); nframe = 0; do { sprintf(buf, "%s frame %d", fn, nframe); indent = 0; indent = pr_title(stdout, indent, buf); pr_indent(stdout, indent); fprintf(stdout, "natoms=%10d step=%10d time=%12.7e prec=%10g\n", natoms, step, time, prec); pr_rvecs(stdout, indent, "box", box, DIM); pr_rvecs(stdout, indent, "x", x, natoms); nframe++; } while (read_next_xtc(xd, natoms, &step, &time, box, x, &prec, &bOK)); if (!bOK) { fprintf(stderr, "\nWARNING: Incomplete frame at time %g\n", time); } sfree(x); close_xtc(xd); }
void ReadGromacs::readFileXtc() { int xd, indent; char buf[256]; rvec *x; matrix box; int nframe, natoms, step; real prec, time; bool bOK; float *d; float *e; float *f; coDoPoints **outanimationpoints = NULL; coDoSet *setpoints = NULL; const char *filename = xtcFileParam->getValue(); xd = open_xtc((char *)"traj.xtc", (char *)"r"); read_first_xtc(xd, &natoms, &step, &time, box, &x, &prec, &bOK); outanimationpoints = new coDoPoints *[100]; for (int h = 0; h < 100; h++) { outanimationpoints[h] = new coDoPoints(pointsAnimationOutput->getObjName(), natoms); } nframe = 0; do { indent = 0; outanimationpoints[nframe]->getAddresses(&d, &e, &f); for (int i = 0; i < natoms; i++) { d[i] = x[i][0]; e[i] = x[i][1]; f[i] = x[i][2]; } nframe++; } while (read_next_xtc(xd, natoms, &step, &time, box, x, &prec, &bOK)); setpoints = new coDoSet(pointsAnimationOutput->getObjName(), (coDistributedObject **)outanimationpoints); pointsAnimationOutput->setCurrentObject(setpoints); }
void list_xtc(char *fn, bool bXVG) { int xd,indent; char buf[256]; rvec *x; matrix box; int nframe,natoms,step; real prec,time; bool bOK; xd = open_xtc(fn,"r"); read_first_xtc(xd,&natoms,&step,&time,box,&x,&prec,&bOK); nframe=0; do { if (bXVG) { int i,d; fprintf(stdout,"%g",time); for(i=0; i<natoms; i++) for(d=0; d<DIM; d++) fprintf(stdout," %g",x[i][d]); fprintf(stdout,"\n"); } else { sprintf(buf,"%s frame %d",fn,nframe); indent=0; indent=pr_title(stdout,indent,buf); pr_indent(stdout,indent); fprintf(stdout,"natoms=%10d step=%10d time=%12.7e prec=%10g\n", natoms,step,time,prec); pr_rvecs(stdout,indent,"box",box,DIM); pr_rvecs(stdout,indent,"x",x,natoms); } nframe++; } while (read_next_xtc(xd,natoms,&step,&time,box,x,&prec,&bOK)); if (!bOK) fprintf(stderr,"\nWARNING: Incomplete frame at time %g\n",time); close_xtc(xd); }
bool read_next_frame(const gmx_output_env_t *oenv, t_trxstatus *status, t_trxframe *fr) { real pt; int ct; gmx_bool bOK, bMissingData = FALSE, bSkip = FALSE; bool bRet = false; int ftp; pt = status->tf; do { clear_trxframe(fr, FALSE); if (status->tng) { /* Special treatment for TNG files */ ftp = efTNG; } else { ftp = gmx_fio_getftp(status->fio); } switch (ftp) { case efTRR: bRet = gmx_next_frame(status, fr); break; case efCPT: /* Checkpoint files can not contain mulitple frames */ break; case efG96: { t_symtab *symtab = nullptr; read_g96_conf(gmx_fio_getfp(status->fio), nullptr, nullptr, fr, symtab, status->persistent_line); bRet = (fr->natoms > 0); break; } case efXTC: if (bTimeSet(TBEGIN) && (status->tf < rTimeValue(TBEGIN))) { if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE)) { gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.", rTimeValue(TBEGIN)); } initcount(status); } bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box, fr->x, &fr->prec, &bOK); fr->bPrec = (bRet && fr->prec > 0); fr->bStep = bRet; fr->bTime = bRet; fr->bX = bRet; fr->bBox = bRet; if (!bOK) { /* Actually the header could also be not ok, but from bOK from read_next_xtc this can't be distinguished */ fr->not_ok = DATA_NOT_OK; } break; case efTNG: bRet = gmx_read_next_tng_frame(status->tng, fr, nullptr, 0); break; case efPDB: bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr); break; case efGRO: bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr); break; default: #if GMX_USE_PLUGINS bRet = read_next_vmd_frame(status->vmdplugin, fr); #else gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s", ftp2ext(gmx_fio_getftp(status->fio)), gmx_fio_getname(status->fio)); #endif } status->tf = fr->time; if (bRet) { bMissingData = (((status->flags & TRX_NEED_X) && !fr->bX) || ((status->flags & TRX_NEED_V) && !fr->bV) || ((status->flags & TRX_NEED_F) && !fr->bF)); bSkip = FALSE; if (!bMissingData) { ct = check_times2(fr->time, status->t0, fr->bDouble); if (ct == 0 || ((status->flags & TRX_DONT_SKIP) && ct < 0)) { printcount(status, oenv, fr->time, FALSE); } else if (ct > 0) { bRet = false; } else { printcount(status, oenv, fr->time, TRUE); bSkip = TRUE; } } } } while (bRet && (bMissingData || bSkip)); if (!bRet) { printlast(status, oenv, pt); if (fr->not_ok) { printincomp(status, fr); } } return bRet; }
gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr) { real pt; int ct; gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE; int dummy = 0; int ftp; bRet = FALSE; pt = fr->tf; do { clear_trxframe(fr, FALSE); fr->tppf = fr->tpf; fr->tpf = fr->tf; if (status->tng) { /* Special treatment for TNG files */ ftp = efTNG; } else { ftp = gmx_fio_getftp(status->fio); } switch (ftp) { case efTRR: bRet = gmx_next_frame(status, fr); break; case efCPT: /* Checkpoint files can not contain mulitple frames */ break; case efG96: read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr, status->persistent_line); bRet = (fr->natoms > 0); break; case efXTC: /* B. Hess 2005-4-20 * Sometimes is off by one frame * and sometimes reports frame not present/file not seekable */ /* DvdS 2005-05-31: this has been fixed along with the increased * accuracy of the control over -b and -e options. */ if (bTimeSet(TBEGIN) && (fr->tf < rTimeValue(TBEGIN))) { if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE)) { gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.", rTimeValue(TBEGIN)); } initcount(status); } bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box, fr->x, &fr->prec, &bOK); fr->bPrec = (bRet && fr->prec > 0); fr->bStep = bRet; fr->bTime = bRet; fr->bX = bRet; fr->bBox = bRet; if (!bOK) { /* Actually the header could also be not ok, but from bOK from read_next_xtc this can't be distinguished */ fr->not_ok = DATA_NOT_OK; } break; case efTNG: bRet = gmx_read_next_tng_frame(status->tng, fr, NULL, 0); break; case efPDB: bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr); break; case efGRO: bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr); break; default: #ifdef GMX_USE_PLUGINS bRet = read_next_vmd_frame(fr); #else gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s", ftp2ext(gmx_fio_getftp(status->fio)), gmx_fio_getname(status->fio)); #endif } fr->tf = fr->time; if (bRet) { bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) || ((fr->flags & TRX_NEED_V) && !fr->bV) || ((fr->flags & TRX_NEED_F) && !fr->bF)); bSkip = FALSE; if (!bMissingData) { ct = check_times2(fr->time, fr->t0, fr->bDouble); if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0)) { printcount(status, oenv, fr->time, FALSE); } else if (ct > 0) { bRet = FALSE; } else { printcount(status, oenv, fr->time, TRUE); bSkip = TRUE; } } } } while (bRet && (bMissingData || bSkip)); if (!bRet) { printlast(status, oenv, pt); if (fr->not_ok) { printincomp(status, fr); } } return bRet; }
int main(int argc, char* argv[]) { const char* program_name = "contact_profile"; bool optsOK = true; gmx::initForCommandLine(&argc,&argv); copyright(program_name); cout << " Computes the standard atomic contacts for structures in" << endl; cout << " the given xtc file. A topology PDB file and atom index file" << endl; cout << " should be provided for determining the atoms to compare." << endl; cout << " The resulting sparse contact distance profiles are" << endl; cout << " in sparse vector format (index-file and data-file)." << endl; cout << endl; cout << " Use -h or --help to see the complete list of options." << endl; cout << endl; // Option vars... int nthreads = 0; double sigma; double eps; string top_filename; string xtc_filename; string ndx_filename; const char* ndx_filename_ptr = NULL; string index_filename; string data_filename; // Declare the supported options. po::options_description cmdline_options; po::options_description program_options("Program options"); program_options.add_options() ("help,h", "show this help message and exit") ("threads,t", po::value<int>(&nthreads)->default_value(omp_get_max_threads()>omp_get_num_procs()?omp_get_num_procs():omp_get_max_threads()), "Input: Number of threads to start (int)") ("epsilon,e", po::value<double>(&eps)->default_value(9.0), "Input: Contact cutoff (real)") // ("sigma,q", po::value<double>(&sigma)->default_value(1), "Input: Standard deviation of gaussian kernel (real)") ("topology-file,p", po::value<string>(&top_filename)->default_value("topology.pdb"), "Input: Topology file [.pdb,.gro,.tpr] (string:filename)") ("xtc-file,x", po::value<string>(&xtc_filename)->default_value("traj.xtc"), "Input: Trajectory file (string:filename)") ("ndx-file,n", po::value<string>(&ndx_filename), "Input: K-nn distances file (string:filename)") ("index-file,i", po::value<string>(&index_filename)->default_value("reference.svi"), "Output: Sparse vector indices file (string:filename)") ("data-file,d", po::value<string>(&data_filename)->default_value("reference.svd"), "Output: Sparse vector data file (string:filename)") ; cmdline_options.add(program_options); po::variables_map vm; po::store(po::parse_command_line(argc, argv, cmdline_options), vm); po::notify(vm); if (vm.count("help")) { cout << "usage: " << program_name << " [options]" << endl; cout << cmdline_options << endl; return 1; } if (vm.count("ndx-file")) { ndx_filename_ptr = ndx_filename.c_str(); } if (!optsOK) { return -1; } cout << "Running with the following options:" << endl; cout << "threads = " << nthreads << endl; cout << "topology-file = " << top_filename << endl; cout << "xtc-file = " << xtc_filename << endl; cout << "ndx-file = " << ndx_filename << endl; cout << "index-file = " << index_filename << endl; cout << "data-file = " << data_filename << endl; cout << endl; // Local vars int step = 1; float time = 0.0; matrix box; float prec = 0.001; char buf[256]; t_topology top; int ePBC; int natoms = 0; int nframes= 0; int update_interval = 1; t_fileio *ref_file; rvec *mycoords = NULL; gmx_bool bOK = 1; double *contact = NULL; vector<coord_array> *ref_coords = NULL; ::real *weights = NULL; int gnx1,gnx2; atom_id *index1,*index2; char *grpname1,*grpname2; ofstream index; ofstream data; // Remove C stdout (silly GROMACS warnings going every which stream!) int myout = dup(1); dup2(2,1); // Setup threads omp_set_num_threads(nthreads); // Get number of atoms and check xtc cout << "Reading topology information from " << top_filename << " ... "; read_tps_conf(top_filename.c_str(), buf, &top, &ePBC, &mycoords, NULL, box, TRUE); cout << "done." << endl; delete [] mycoords; ref_file = open_xtc(xtc_filename.c_str(),"r"); read_first_xtc(ref_file,&natoms, &step, &time, box, &mycoords, &prec, &bOK); close_xtc(ref_file); if (natoms != top.atoms.nr) { cout << "*** ERROR ***" << endl; cout << "Number of atoms in topology file (" << top.atoms.nr << ") " << "does not match the number of atoms " << "in the XTC file (" << xtc_filename << " : " << natoms << ")." << endl; exit(4); } // Get atom selections cout << "Please select two (non-overlapping) groups for contact profiling..." << endl; get_index(&top.atoms,ndx_filename_ptr,1,&gnx1,&index1,&grpname1); cout << endl; get_index(&top.atoms,ndx_filename_ptr,1,&gnx2,&index2,&grpname2); cout << endl; cout << "Total grid size is " << gnx1 << " x " << gnx2 << " = " << (gnx1*gnx2) << endl; // Read coordinates and weight-center all structures cout << "Reading reference coordinates from file: " << xtc_filename << " ... "; ref_coords = new vector<coord_array>; ref_file = open_xtc(xtc_filename.c_str(),"r"); mycoords = new rvec[natoms]; while (read_next_xtc(ref_file, natoms, &step, &time, box, mycoords, &prec, &bOK)) { ref_coords->push_back(mycoords); mycoords = new rvec[natoms]; } close_xtc(ref_file); delete [] mycoords; mycoords = NULL; nframes = ref_coords->size(); cout << "done." << endl; // Allocate vectors for storing the distances for a structure contact = new double[gnx1*gnx2]; weights = new ::real[gnx1*gnx2]; for (int x = 0; x < natoms; x++) weights[x] = top.atoms.atom[x].m; #pragma omp parallel for for (int i = 0; i < gnx1; i++) for (int j = 0; j < gnx2; j++) { weights[(i*gnx2)+j] = top.atoms.atom[index1[i]].m * top.atoms.atom[index2[j]].m; } // Restore C stdout. dup2(myout,1); index.open(index_filename.c_str()); data.open(data_filename.c_str()); // Timer for ETA time_t start = std::time(0); time_t last = start; // Compute fits for (int frame = 0; frame < nframes; frame++) { // Update user of progress if (std::time(0) - last > update_interval) { last = std::time(0); time_t eta = start + ((last-start) * nframes / frame); cout << "\rFrame: " << frame << ", will finish " << string(std::ctime(&eta)).substr(0,20); cout.flush(); } // Do Work #pragma omp parallel for for (int i = 0; i < gnx1*gnx2; i++) contact[i] = 0.0; #pragma omp parallel for for (int i = 0; i < gnx1; i++) { int ii = index1[i]; for (int j = 0; j < gnx2; j++) { int jj = index2[j]; double d = 0.0; for (int k = 0; k < 3; k++) d += (((*ref_coords)[frame][ii][k] - (*ref_coords)[frame][jj][k]) * ((*ref_coords)[frame][ii][k] - (*ref_coords)[frame][jj][k])); d = sqrt(d) * 10.0; // d = exp(-(d*d) / (2.0 * weights[(i*gnx2)+j])); // if (d > eps) // contact[(i*gnx2)+j] = d; if (d < eps) contact[(i*gnx2)+j] = 1.0; } // j } // i double sum = 0.0; #pragma omp parallel for reduction(+:sum) for (int i = 0; i < gnx1*gnx2; i++) sum += contact[i]; sum = 1.0; // No normalization... int total = 0; #pragma omp parallel for reduction(+:total) for (int i = 0; i < gnx1*gnx2; i++) if (contact[i] > 0) { contact[i] /= sum; total++; } index.write((char*) &total, sizeof(int) / sizeof(char)); for (int i = 0; i < gnx1*gnx2; i++) if (contact[i] > 0.0) { index.write((char*) &i, sizeof(int) / sizeof(char)); data.write((char*) &contact[i], sizeof(double) / sizeof(char)); } // cout << frame << " " << total << endl; } // frame cout << endl << endl; index.close(); data.close(); // Clean coordinates for (vector<coord_array>::iterator itr = ref_coords->begin(); itr != ref_coords->end(); itr++) delete [] (*itr); delete ref_coords; delete [] contact; delete [] weights; return 0; }