// Action_NativeContacts::WriteContacts()
void Action_NativeContacts::WriteContacts(contactListType& ContactsIn, bool isNative) {
  if (ContactsIn.empty()) return;
  // Map of residue pairs to total contact values.
  typedef std::map<Cpair, resContact> resContactMap;
  resContactMap ResContacts;
  std::pair<resContactMap::iterator, bool> ret;
  // Normalize native contacts. Place them into an array where they will
  // be sorted. Sum up total contact over residue pairs.
  std::vector<contactType> sortedList;
  for (contactListType::iterator it = ContactsIn.begin();
                                 it != ContactsIn.end(); ++it)
  {
    it->second.Finalize();
    sortedList.push_back( it->second );
    ret = ResContacts.insert( Rpair(Cpair(it->second.Res1(),it->second.Res2()),
                                    resContact(it->second.Nframes(), it->second.DataPtr())) );
    if (!ret.second) // residue pair exists, update it.
      ret.first->second.Increment( it->second.Nframes(), it->second.DataPtr() );
  }
  std::sort( sortedList.begin(), sortedList.end() );
  // Place residue pairs into an array to be sorted.
  std::vector<Rpair> ResList;
  for (resContactMap::const_iterator it = ResContacts.begin(); it != ResContacts.end(); ++it)
  {
    ResList.push_back( *it );
    if (Rseries_ != NO_RESSERIES) {
      const char* resDsAspect;
      std::string lprefix = "";
      if (isNative)
        resDsAspect = "NCRES";
      else {
        resDsAspect = "NNRES";
        lprefix = "nn_";
      }
      // Ensure r1 < r2 so we can calculate a unique index for residue pairs
      // that can match between native/non-native contacts
      int r1, r2;
      if (it->first.second < it->first.first) {
        r1 = it->first.second;
        r2 = it->first.first;
      } else {
        r1 = it->first.first;
        r2 = it->first.second;
      }
      int ridx = (r2 * CurrentParm_->Nres()) + r1;
      std::string legend(lprefix +
                         CurrentParm_->TruncResNameNum(r1) + "_" +
                         CurrentParm_->TruncResNameNum(r2)); 
      MetaData md(numnative_->Meta().Name(), resDsAspect, ridx);
      md.SetLegend( legend );
      DataSet_integer* ds = (DataSet_integer*)masterDSL_->AddSet(DataSet::INTEGER, md);
      if (ds != 0) {
        ds->Allocate(DataSet::SizeArray(1, nframes_));
        if (seriesRout_ != 0) seriesRout_->AddDataSet( ds );
        // All series will be the same size thanks to UpdateSeries()
        for (unsigned int f = 0; f != nframes_; f++) {
          int total_present = 0;
          for (DSarray::const_iterator set = it->second.Sets().begin();
                                       set != it->second.Sets().end(); ++set)
            total_present += (*(*set))[f];
          if (Rseries_ == RES_PRESENT && total_present > 0) total_present = 1;
          ds->AddElement( total_present );
        }
      }
    }
  }
  std::sort( ResList.begin(), ResList.end(), res_cmp() );
  // Print out total fraction frames for residue pairs.
  const char* ctitle;
  if (isNative)
    ctitle = "Contacts";
  else
    ctitle = "nnContacts";
  rfile_->Printf("%-8s %8s %10s %10s\n", "#Res1", "#Res2", "TotalFrac", ctitle);
  //for (resContactMap::const_iterator it = ResContacts.begin(); it != ResContacts.end(); ++it)
  for (std::vector<Rpair>::const_iterator it = ResList.begin();
                                          it != ResList.end(); ++it)
    rfile_->Printf("%-8i %8i %10g %10i\n", it->first.first+1, it->first.second+1,
                  (double)it->second.Nframes()/(double)nframes_,
                  it->second.Ncontacts());
  // Print out sorted atom contacts.
  cfile_->Printf("%-8s %20s %8s %8s %8s %8s\n", "#", "Contact", "Nframes", "Frac.", "Avg", "Stdev");
  unsigned int num = 1;
  for (std::vector<contactType>::const_iterator NC = sortedList.begin();
                                                NC != sortedList.end(); ++NC, ++num)
  { 
    double fracPresent = (double)NC->Nframes() / (double)nframes_;
    cfile_->Printf("%8u %20s %8i %8.3g %8.3g %8.3g\n", num, NC->id(),
                   NC->Nframes(), fracPresent, NC->Avg(), NC->Stdev());
  }
}
示例#2
0
// Analysis_RemLog::Setup()
Analysis::RetType Analysis_RemLog::Setup(ArgList& analyzeArgs, DataSetList* datasetlist, DataFileList* DFLin, int debugIn)
{
  debug_ = debugIn;
  // Get remlog dataset
  std::string remlogName = analyzeArgs.GetStringNext();
  if (remlogName.empty()) {
    mprinterr("Error: no remlog data set or file name specified.\n");
    return Analysis::ERR;
  }
  // Check if data set exists
  remlog_ = (DataSet_RemLog*)datasetlist->FindSetOfType( remlogName, DataSet::REMLOG );
  if (remlog_ == 0) {
    mprinterr("Error: remlog data with name %s not found.\n", remlogName.c_str());
    return Analysis::ERR;
  }
  if (remlog_->Size() < 1 || remlog_->NumExchange() < 1) {
    mprinterr("Error: remlog data set appears to be empty.\n");
    return Analysis::ERR;
  }
  acceptout_ = DFLin->AddCpptrajFile( analyzeArgs.GetStringKey("acceptout"), "replica acceptance",
                                      DataFileList::TEXT, true );
  if (acceptout_ == 0) return Analysis::ERR;
  lifetimes_ = DFLin->AddCpptrajFile( analyzeArgs.GetStringKey("lifetime"), "remlog lifetimes" );
  calculateLifetimes_ = (lifetimes_ != 0);
  calculateStats_ = analyzeArgs.hasKey("stats");
  if (calculateStats_) {
    statsout_ = DFLin->AddCpptrajFile( analyzeArgs.GetStringKey("statsout"), "remlog stats",
                                       DataFileList::TEXT, true );
    reptime_ = DFLin->AddCpptrajFile( analyzeArgs.GetStringKey("reptime"), "replica times",
                                      DataFileList::TEXT, true );
    if (statsout_ == 0 || reptime_ == 0) return Analysis::ERR;
  }
  calcRepFracSlope_ = analyzeArgs.getKeyInt("reptimeslope", 0);
  std::string rfs_name = analyzeArgs.GetStringKey("reptimeslopeout");
  if (!calculateStats_) {
    calcRepFracSlope_ = 0;
    rfs_name.clear();
  }
  if ( (calcRepFracSlope_ > 0) != (!rfs_name.empty()) ) {
    mprinterr("Error: Both reptimeslope and reptimeslopeout must be specified.\n");
    return Analysis::ERR;
  }
  repFracSlope_ = DFLin->AddCpptrajFile( rfs_name, "replica fraction slope" );
  printIndividualTrips_ = analyzeArgs.hasKey("printtrips");
  // Get mode
  if (analyzeArgs.hasKey("crdidx"))
    mode_ = CRDIDX;
  else if (analyzeArgs.hasKey("repidx"))
    mode_ = REPIDX;
  else
    mode_ = NONE;
  const char* def_name = 0;
  const char* yaxis = 0;
  if (mode_ == CRDIDX) {
    def_name = "repidx";
    yaxis = "ylabel CrdIdx";
  } else if (mode_ == REPIDX) {
    def_name = "crdidx";
    yaxis = "ylabel RepIdx";
  }
  // Set up an output set for each replica
  DataFile* dfout = 0;
  if (mode_ != NONE) {
    // Get output filename
    std::string outname = analyzeArgs.GetStringKey("out");
    if (!outname.empty()) {
      dfout = DFLin->AddDataFile( outname, analyzeArgs );
      if (dfout == 0 ) return Analysis::ERR;
      if (yaxis != 0 ) dfout->ProcessArgs(yaxis);
    }
    std::string dsname = analyzeArgs.GetStringKey("name");
    if (dsname.empty())
      dsname = datasetlist->GenerateDefaultName(def_name);
    MetaData md(dsname);
    for (int i = 0; i < (int)remlog_->Size(); i++) {
      md.SetIdx(i+1);
      DataSet_integer* ds = (DataSet_integer*)datasetlist->AddSet(DataSet::INTEGER, md);
      if (ds == 0) return Analysis::ERR;
      outputDsets_.push_back( (DataSet*)ds );
      if (dfout != 0) dfout->AddDataSet( (DataSet*)ds );
      ds->Resize( remlog_->NumExchange() ); 
    }
  }
  mprintf("   REMLOG: %s, %i replicas, %i exchanges\n", remlog_->legend(),
          remlog_->Size(), remlog_->NumExchange());
  if (mode_ == CRDIDX)
    mprintf("\tGetting coordinate index vs exchange.\n");
  else if (mode_ == REPIDX)
    mprintf("\tGetting replica index vs exchange.\n");
  if (mode_ != NONE && dfout != 0)
    mprintf("\tOutput is to %s\n", dfout->DataFilename().base());
  if (calculateStats_) {
    mprintf("\tGetting replica exchange stats, output to %s\n", statsout_->Filename().full());
    if (printIndividualTrips_)
      mprintf("\tIndividual round trips will be printed.\n");
    mprintf("\tWriting time spent at each replica to %s\n", reptime_->Filename().full());
  }
  if (calculateLifetimes_)
    mprintf("\tThe lifetime of each crd at each replica will be calculated.\n");
  if (acceptout_ != 0)
    mprintf("\tOverall exchange acceptance % will be written to %s\n",
            acceptout_->Filename().full());

  return Analysis::OK;
}
// Action_ClusterDihedral::Print()
void Action_ClusterDihedral::Print() {
  // Setup output file
  mprintf("\tPrinting Dihedral Clustering Results.\n");
  // Print bin information
  outfile_->Printf("DIHEDRAL CLUSTER RESULTS");
  if (mask_.MaskStringSet())
    outfile_->Printf(" for %s", mask_.MaskString());
  outfile_->Printf("\n");
  long int num = 0;
  for (std::vector<DCmask>::const_iterator dih = DCmasks_.begin();
                                           dih != DCmasks_.end(); ++dih)
  {
    outfile_->Printf("    %6li ", num++);
    outfile_->Printf("%-s(%i)", (*dcparm_)[ dih->A1() ].c_str(), dih->A1() + 1);
    outfile_->Printf("%-s(%i)", (*dcparm_)[ dih->A2() ].c_str(), dih->A2() + 1);
    outfile_->Printf("%-s(%i)", (*dcparm_)[ dih->A3() ].c_str(), dih->A3() + 1);
    outfile_->Printf("%-s(%i)", (*dcparm_)[ dih->A4() ].c_str(), dih->A4() + 1);
    outfile_->Printf(" [Bins=%i]\n", dih->Bins());
  }
  outfile_->Printf("%zu clusters.\n", dcarray_.size());

  // Sort array by count
  std::sort( dcarray_.begin(), dcarray_.end() );

  // Allocate space for storing cluster #s for each frame
  std::vector<long int> framecluster( lastframe_ + 1 );

  // Print sorted cluster array
  if (CUT_ > 0)
    outfile_->Printf("Only printing clusters with pop > %i\n",CUT_);
  num = 0;
  for (std::vector<DCnode>::const_iterator DC = dcarray_.begin();
                                           DC != dcarray_.end(); ++DC)
  {
    if ( DC->Count() > CUT_ ) {
      //mprintf("DEBUG: Cluster %li has %i frames.\n", num, DC->NumFrames());
      outfile_->Printf("Cluster %10li %10li [ ", num+1, DC->Count());
      for (DCnode::bin_it binid = DC->binbegin(); binid != DC->binend(); ++binid)
        outfile_->Printf("%3i ", *binid);
      outfile_->Printf(" ]\n");
      for (DCnode::frame_it frame = DC->framebegin(); frame != DC->frameend(); ++frame)
      {
        outfile_->Printf("%i ", *frame + 1);
        // store which cluster each frame belongs to. Not neccesary if user
        // didn't specify this option, but avoids a second loop if they did.
        framecluster[ *frame ] = num;
      }
      outfile_->Printf("\n");
    }
    ++num;
  }

  // Place reordered cluster nums in CVT
  if (CVT_ != 0) {
    DataSet_integer* iCVT = (DataSet_integer*)CVT_;
    iCVT->Resize( framecluster.size() );
    num = 0;
    for (std::vector<long int>::const_iterator cnum = framecluster.begin();
                                               cnum != framecluster.end(); ++cnum)
      (*iCVT)[ num++ ] = (int)*cnum + 1;
  }

  // Print cluster for each frame
  if (framefile_ != 0) {
    mprintf("\tPrinting cluster number for each frame.\n");
    num = 1;
    for (std::vector<long int>::const_iterator cnum = framecluster.begin(); 
                                               cnum != framecluster.end(); ++cnum)
    {
      // Frame, cluster num, cluster count
      framefile_->Printf("%10li %10i %10li ", num++, *cnum + 1, dcarray_[*cnum].Count());
      // Print binID
      for (DCnode::bin_it binid = dcarray_[*cnum].binbegin();
                          binid != dcarray_[*cnum].binend(); ++binid)
        framefile_->Printf("%03i", *binid);
      framefile_->Printf("\n");
    }
  }

  // Print cluster information file
  if (infofile_!=0) {
    mprintf("\tPrinting cluster information.\n");
    infofile_->Printf("%zu\n", DCmasks_.size());
    for (std::vector<DCmask>::const_iterator dih = DCmasks_.begin();
                                             dih != DCmasks_.end(); ++dih)
    {
      infofile_->Printf("%10i %10i %10i %10i %10i %8.3f\n", dih->A1()+1, dih->A2()+1,
                    dih->A3()+1, dih->A4()+1, dih->Bins(), dih->Min());
    }
    infofile_->Printf("%zu\n", dcarray_.size());
    num = 1;
    for (std::vector<DCnode>::const_iterator DC = dcarray_.begin();
                                             DC != dcarray_.end(); ++DC)
    {
      infofile_->Printf("%10li %10li ", num++, DC->Count());
      for (DCnode::bin_it binid = DC->binbegin(); binid != DC->binend(); ++binid)
        infofile_->Printf(" %3i", *binid);
      infofile_->Printf("\n");
    }
  }
}