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_first_frame(const gmx_output_env_t *oenv, t_trxstatus **status, const char *fn, t_trxframe *fr, int flags) { t_fileio *fio = nullptr; gmx_bool bFirst, bOK; int ftp = fn2ftp(fn); clear_trxframe(fr, TRUE); bFirst = TRUE; snew((*status), 1); status_init( *status ); initcount(*status); (*status)->flags = flags; if (efTNG == ftp) { /* Special treatment for TNG files */ gmx_tng_open(fn, 'r', &(*status)->tng); } else { fio = (*status)->fio = gmx_fio_open(fn, "r"); } switch (ftp) { case efTRR: break; case efCPT: read_checkpoint_trxframe(fio, fr); bFirst = FALSE; break; case efG96: { /* Can not rewind a compressed file, so open it twice */ if (!(*status)->persistent_line) { /* allocate the persistent line */ snew((*status)->persistent_line, STRLEN+1); } t_symtab *symtab = nullptr; read_g96_conf(gmx_fio_getfp(fio), fn, nullptr, fr, symtab, (*status)->persistent_line); gmx_fio_close(fio); clear_trxframe(fr, FALSE); if (flags & (TRX_READ_X | TRX_NEED_X)) { snew(fr->x, fr->natoms); } if (flags & (TRX_READ_V | TRX_NEED_V)) { snew(fr->v, fr->natoms); } (*status)->fio = gmx_fio_open(fn, "r"); break; } case efXTC: if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x, &fr->prec, &bOK) == 0) { GMX_RELEASE_ASSERT(!bOK, "Inconsistent results - OK status from read_first_xtc, but 0 atom coords read"); fr->not_ok = DATA_NOT_OK; } if (fr->not_ok) { fr->natoms = 0; printincomp(*status, fr); } else { fr->bPrec = (fr->prec > 0); fr->bStep = TRUE; fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efTNG: fr->step = -1; if (!gmx_read_next_tng_frame((*status)->tng, fr, nullptr, 0)) { fr->not_ok = DATA_NOT_OK; fr->natoms = 0; printincomp(*status, fr); } else { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efPDB: pdb_first_x(*status, gmx_fio_getfp(fio), fr); if (fr->natoms) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efGRO: if (gro_first_x_or_v(gmx_fio_getfp(fio), fr)) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; default: #if GMX_USE_PLUGINS fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n" "Please make sure that the file is a trajectory!\n" "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n" "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn); gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/ if (!read_first_vmd_frame(fn, &(*status)->vmdplugin, fr)) { gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn); } #else gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n" "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n" "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn); #endif break; } (*status)->tf = fr->time; /* Return FALSE if we read a frame that's past the set ending time. */ if (!bFirst && (!(flags & TRX_DONT_SKIP) && check_times(fr->time) > 0)) { (*status)->t0 = fr->time; return FALSE; } if (bFirst || (!(flags & TRX_DONT_SKIP) && check_times(fr->time) < 0)) { /* Read a frame when no frame was read or the first was skipped */ if (!read_next_frame(oenv, *status, fr)) { return FALSE; } } (*status)->t0 = fr->time; /* We need the number of atoms for random-access XTC searching, even when * we don't have access to the actual frame data. */ (*status)->natoms = fr->natoms; return (fr->natoms > 0); }
int read_first_frame(const output_env_t oenv, t_trxstatus **status, const char *fn, t_trxframe *fr, int flags) { t_fileio *fio; gmx_bool bFirst, bOK; int dummy = 0; clear_trxframe(fr, TRUE); fr->flags = flags; bFirst = TRUE; snew((*status), 1); status_init( *status ); (*status)->nxframe = 1; initcount(*status); fio = (*status)->fio = gmx_fio_open(fn, "r"); switch (gmx_fio_getftp(fio)) { case efTRJ: case efTRR: break; case efCPT: read_checkpoint_trxframe(fio, fr); bFirst = FALSE; break; case efG96: /* Can not rewind a compressed file, so open it twice */ if (!(*status)->persistent_line) { /* allocate the persistent line */ snew((*status)->persistent_line, STRLEN+1); } read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line); gmx_fio_close(fio); clear_trxframe(fr, FALSE); if (flags & (TRX_READ_X | TRX_NEED_X)) { snew(fr->x, fr->natoms); } if (flags & (TRX_READ_V | TRX_NEED_V)) { snew(fr->v, fr->natoms); } fio = (*status)->fio = gmx_fio_open(fn, "r"); break; case efG87: fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time, &fr->x, fr->box); if (fr->natoms) { fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efXTC: if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x, &fr->prec, &bOK) == 0) { if (bOK) { gmx_fatal(FARGS, "No XTC!\n"); } else { fr->not_ok = DATA_NOT_OK; } } if (fr->not_ok) { fr->natoms = 0; printincomp(*status, fr); } else { fr->bPrec = (fr->prec > 0); fr->bStep = TRUE; fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efPDB: pdb_first_x(*status, gmx_fio_getfp(fio), fr); if (fr->natoms) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; case efGRO: if (gro_first_x_or_v(gmx_fio_getfp(fio), fr)) { printcount(*status, oenv, fr->time, FALSE); } bFirst = FALSE; break; default: #ifdef GMX_USE_PLUGINS fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n" "Please make sure that the file is a trajectory!\n" "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n" "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn); gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/ if (!read_first_vmd_frame(fn, fr)) { gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn); } #else gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n" "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n" "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn); #endif break; } /* Return FALSE if we read a frame that's past the set ending time. */ if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0)) { fr->t0 = fr->time; return FALSE; } if (bFirst || (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0)) { /* Read a frame when no frame was read or the first was skipped */ if (!read_next_frame(oenv, *status, fr)) { return FALSE; } } fr->t0 = fr->time; return (fr->natoms > 0); }
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; }
int read_first_frame(const output_env_t oenv,t_trxstatus **status, const char *fn,t_trxframe *fr,int flags) { t_fileio *fio; gmx_bool bFirst,bOK; int dummy=0; clear_trxframe(fr,TRUE); fr->flags = flags; bFirst = TRUE; snew((*status), 1); status_init( *status ); (*status)->nxframe=1; initcount(*status); fio = (*status)->fio =gmx_fio_open(fn,"r"); switch (gmx_fio_getftp(fio)) { case efTRJ: case efTRR: break; case efCPT: read_checkpoint_trxframe(fio,fr); bFirst = FALSE; break; case efG96: /* Can not rewind a compressed file, so open it twice */ read_g96_conf(gmx_fio_getfp(fio),fn,fr); gmx_fio_close(fio); clear_trxframe(fr,FALSE); if (flags & (TRX_READ_X | TRX_NEED_X)) snew(fr->x,fr->natoms); if (flags & (TRX_READ_V | TRX_NEED_V)) snew(fr->v,fr->natoms); fio = (*status)->fio =gmx_fio_open(fn,"r"); break; case efG87: fr->natoms=xyz_first_x(*status, gmx_fio_getfp(fio),oenv,&fr->time, &fr->x,fr->box); if (fr->natoms) { fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status,oenv,fr->time,FALSE); } bFirst = FALSE; break; case efXTC: if (read_first_xtc(fio,&fr->natoms,&fr->step,&fr->time,fr->box,&fr->x, &fr->prec,&bOK) == 0) { if (bOK) { gmx_fatal(FARGS,"No XTC!\n"); } else { fr->not_ok = DATA_NOT_OK; } } if (fr->not_ok) { fr->natoms = 0; printincomp(*status,fr); } else { fr->bPrec = (fr->prec > 0); fr->bStep = TRUE; fr->bTime = TRUE; fr->bX = TRUE; fr->bBox = TRUE; printcount(*status,oenv,fr->time,FALSE); } bFirst = FALSE; break; case efPDB: pdb_first_x(*status, gmx_fio_getfp(fio),fr); if (fr->natoms) printcount(*status,oenv,fr->time,FALSE); bFirst = FALSE; break; case efGRO: if (gro_first_x_or_v(gmx_fio_getfp(fio),fr)) printcount(*status,oenv,fr->time,FALSE); bFirst = FALSE; break; default: #ifdef GMX_DLOPEN gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/ if (!read_first_vmd_frame(&dummy,fn,fr,flags)) { gmx_fatal(FARGS,"Not supported in read_first_frame: %s",fn); } #else gmx_fatal(FARGS,"Not supported in read_first_frame: %s",fn); #endif break; } /* Return FALSE if we read a frame that's past the set ending time. */ if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0)) { fr->t0 = fr->time; return FALSE; } if (bFirst || (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0)) /* Read a frame when no frame was read or the first was skipped */ if (!read_next_frame(oenv,*status,fr)) return FALSE; fr->t0 = fr->time; return (fr->natoms > 0); }