Esempio n. 1
0
int
ns_format_ttl(u_long src, char *dst, size_t dstlen) {
	char *odst = dst;
	int secs, mins, hours, days, weeks, x;
	char *p;

	secs = (int)(src % 60);   src /= 60;
	mins = (int)(src % 60);   src /= 60;
	hours = (int)(src % 24);  src /= 24;
	days = (int)(src % 7);    src /= 7;
	weeks = (int)src;       src = 0;

	x = 0;
	if (weeks) {
		T(fmt1(weeks, 'W', &dst, &dstlen));
		x++;
	}
	if (days) {
		T(fmt1(days, 'D', &dst, &dstlen));
		x++;
	}
	if (hours) {
		T(fmt1(hours, 'H', &dst, &dstlen));
		x++;
	}
	if (mins) {
		T(fmt1(mins, 'M', &dst, &dstlen));
		x++;
	}
	if (secs || !(weeks || days || hours || mins)) {
		T(fmt1(secs, 'S', &dst, &dstlen));
		x++;
	}

	if (x > 1) {
		int ch;

		for (p = odst; (ch = *p) != '\0'; p++)
			if (isascii(ch) && isupper(ch))
				*p = tolower(ch);
	}

	assert(INT_MIN <= (dst - odst) && (dst - odst) <= INT_MAX);
	return (int)(dst - odst);
}
Esempio n. 2
0
// print test parameters for current delay time
void
Responder::print_formatted_delay_line(const uint64_t simulate_duration, const uint64_t old_simulate_duration, const STATS& statsPrev, const double delay, const double simulate_frequency)
{
    if(_y_delay_pos < 0 || _x_delay_pos < 0){ // make sure it gets printed to the same position everytime
        getyx(_window, _y_delay_pos, _x_delay_pos);
    }
    double score = 0.0;
    if (statsPrev.detected > 0)
        score = 100.0 * (double)(statsPrev.detected - statsPrev.missed) / (double)statsPrev.detected;
    std::string form;
    boost::format fmt0("Delay now: %.6f (previous delay %.6f scored %.1f%% [%ld / %ld])");
    fmt0 % delay % statsPrev.delay % score % (statsPrev.detected - statsPrev.missed) % statsPrev.detected;
    form += fmt0.str();
    if (old_simulate_duration != simulate_duration)
    {
        boost::format fmt1(" [Simulation rate now: %.1f Hz (%ld samples)]");
        fmt1 % simulate_frequency % simulate_duration;
        form = form + fmt1.str();
    }
    move(_y_delay_pos, _x_delay_pos);
    print_msg(form);
}
Esempio n. 3
0
int main( int argc, char * argv[] ) {

  Libnucnet *p_my_nucnet;
  Libnucnet__NetView * p_net_view, * p_cluster_view;
  std::pair<double,double> flows;
  double d_min;

  //============================================================================
  // Check input.
  //============================================================================

   if ( argc < 4 || argc > 6 ) {
      fprintf(
        stderr,
        "\nUsage: %s xml_filename zone_xpath cluster_xpath min_flow reac_xpath\n\n",
        argv[0]
      );
      fprintf(
        stderr, "  xml_filename = input xml filename\n\n"
      );
      fprintf(
        stderr, "  zone_xpath = XPath to select zones for flows\n\n"
      );
      fprintf(
        stderr, "  cluster_xpath = XPath to select cluster\n\n"
      );
      fprintf(
        stderr,
        "  min_flow = minimum net flow for printout (optional--default=0)\n\n"
      );
      fprintf(
        stderr, "  reac_xpath = reaction xpath (optional)\n\n"
      );
      return EXIT_FAILURE;
   }

  //============================================================================
  // Read file and exit if not present.
  //============================================================================

  p_my_nucnet = Libnucnet__new_from_xml( argv[1], NULL, NULL, argv[2] );

  if( !p_my_nucnet ) {
    fprintf( stderr, "Input data not read!\n" );
    return EXIT_FAILURE;
  }

  //============================================================================
  // Register rate functions.
  //============================================================================

  user::register_my_rate_functions(
    Libnucnet__Net__getReac(
      Libnucnet__getNet( p_my_nucnet )
    )
  );

  //============================================================================
  // Get the minimum net flow for printout.
  //============================================================================

  if( argc > 4 )
    d_min = atof( argv[4] );
  else
    d_min = 0.;

  //============================================================================
  // Get printout formats.
  //============================================================================

  boost::format fmt1(
    "time(s) = %9.4e  t9 = %6.4f  rho(g/cc) = %9.4e  Ye = %6.4f"
  );

  boost::format fmt2( "time(s) = %9.4e  t9 = %6.4f  rho(g/cc) = %9.4e" );

  boost::format fmt3( "%-55s%12.3e%12.3e%12.3e" );

  //============================================================================
  // Get valid net view.
  //============================================================================

  if( argc != 6 )
    p_net_view =
      Libnucnet__NetView__new( Libnucnet__getNet( p_my_nucnet ), "", "" );
  else
    p_net_view =
      Libnucnet__NetView__new( Libnucnet__getNet( p_my_nucnet ), "", argv[5] );

  //============================================================================
  // Set compare function.
  //============================================================================

  Libnucnet__Reac__setReactionCompareFunction(
    Libnucnet__Net__getReac( Libnucnet__NetView__getNet( p_net_view ) ),
    (Libnucnet__Reaction__compare_function)
       nnt::compare_reactions_by_string
  );

  //============================================================================
  // Get the reactions.
  //============================================================================

  nnt::reaction_list_t reaction_list =
    nnt::make_reaction_list(
      Libnucnet__Net__getReac( Libnucnet__NetView__getNet( p_net_view ) )
    );

  //============================================================================
  // Get the cluster view.
  //============================================================================

  p_cluster_view =
    Libnucnet__NetView__new(
      Libnucnet__getNet( p_my_nucnet ),
      argv[3],
      ""
    );

  //============================================================================
  // Iterate the zones.
  //============================================================================

  Libnucnet__setZoneCompareFunction(
    p_my_nucnet,
    (Libnucnet__Zone__compare_function) nnt::zone_compare_by_first_label
  );

  BOOST_FOREACH( nnt::Zone zone, nnt::make_zone_list( p_my_nucnet ) )
  {

    Libnucnet__Zone__updateNetView(
      zone.getNucnetZone(),
      argv[3],
      "",
      NULL,
      Libnucnet__NetView__copy( p_cluster_view )
    );
      
    user::update_my_rate_functions_data( zone );

    //==========================================================================
    // Print conditions.
    //==========================================================================

    if( zone.hasProperty( nnt::s_TIME ) )
    {
      fmt1 %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_TIME ) ) %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_T9 ) ) %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_RHO ) ) %
        Libnucnet__Zone__computeZMoment( zone.getNucnetZone(), 1 );
      std::cout << fmt1.str() << std::endl;
    }
    else
    {
      fmt1 %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_TIME ) ) %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_T9 ) ) %
        boost::lexical_cast<double>( zone.getProperty( nnt::s_RHO ) );
      std::cout << fmt2.str() << std::endl;
    }

    //==========================================================================
    // Print flows.
    //==========================================================================
  
    double d_forward_in = 0.;
    double d_forward_out = 0.;
    double d_reverse_in = 0.;
    double d_reverse_out = 0.;

    std::vector< boost::tuple<std::string,double,double> > reactions;

    BOOST_FOREACH( nnt::Reaction reaction, reaction_list )
    {

      int i_nuc = 0;

      nnt::reaction_element_list_t reactant_list =
        nnt::make_reaction_nuclide_reactant_list(
          reaction.getNucnetReaction()
        );

      BOOST_FOREACH( nnt::ReactionElement reactant, reactant_list )
      {
        if(
          Libnucnet__Nuc__getSpeciesByName(
            Libnucnet__Net__getNuc(
              Libnucnet__NetView__getNet( p_cluster_view )
            ),
            Libnucnet__Reaction__Element__getName(
              reactant.getNucnetReactionElement()
            )
          )
        )
          i_nuc--;
      }

      nnt::reaction_element_list_t product_list =
        nnt::make_reaction_nuclide_product_list(
          reaction.getNucnetReaction()
        );

      BOOST_FOREACH( nnt::ReactionElement product, product_list )
      {
        if(
          Libnucnet__Nuc__getSpeciesByName(
            Libnucnet__Net__getNuc(
              Libnucnet__NetView__getNet( p_cluster_view )
            ),
            Libnucnet__Reaction__Element__getName(
              product.getNucnetReactionElement()
            )
          )
        )
          i_nuc++;
      }

      if( i_nuc != 0 )
      {

        flows =
          user::compute_flows_for_reaction(
            zone,
            reaction.getNucnetReaction()
          );

        reactions.push_back(
          boost::make_tuple(
            Libnucnet__Reaction__getString( reaction.getNucnetReaction() ),
            flows.first,
            flows.second
          )
        );

        if( i_nuc > 0 )
        {
          d_forward_in += flows.first;
          d_reverse_out += flows.second;
        }
        else
        {
          d_forward_out += flows.first;
          d_reverse_in += flows.second;
        }

      }

    }
Esempio n. 4
0
/**
 * Test new closure API
 */
void TestChoiceFormat::TestClosures(void) {
    // Construct open, half-open, half-open (the other way), and closed
    // intervals.  Do this both using arrays and using a pattern.

    // 'fmt1' is created using arrays
    UBool T = TRUE, F = FALSE;
    // 0:   ,1)
    // 1: [1,2]
    // 2: (2,3]
    // 3: (3,4)
    // 4: [4,5)
    // 5: [5,
    double limits[]  = { 0, 1, 2, 3, 4, 5 };
    UBool closures[] = { F, F, T, T, F, F };
    UnicodeString fmts[] = {
        ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
    };
    ChoiceFormat fmt1(limits, closures, fmts, 6);

    // 'fmt2' is created using a pattern; it should be equivalent
    UErrorCode status = U_ZERO_ERROR;
    const char* PAT = "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,";
    ChoiceFormat fmt2(PAT, status);
    if (U_FAILURE(status)) {
        errln("FAIL: ChoiceFormat constructor failed");
        return;
    }
    
    // Check the patterns
    UnicodeString str;
    fmt1.toPattern(str);
    if (str == PAT) {
        logln("Ok: " + str);
    } else {
        errln("FAIL: " + str + ", expected " + PAT);
    }
    str.truncate(0);

    // Check equality
    if (fmt1 != fmt2) {
        errln("FAIL: fmt1 != fmt2");
    }

#if 0  // ICU 4.8 deprecates and disables the ChoiceFormat getters.
    int32_t i;
    int32_t count2 = 0;
    const double *limits2 = fmt2.getLimits(count2);
    const UBool *closures2 = fmt2.getClosures(count2);

    if((count2 != 6) || !limits2 || !closures2) {
        errln("FAIL: couldn't get limits or closures");
    } else {
        for(i=0;i<count2;i++) {
          logln("#%d/%d: limit %g closed %s\n",
                i, count2,
                limits2[i],
                closures2[i] ?"T":"F");
          if(limits2[i] != limits[i]) {
            errln("FAIL: limit #%d = %g, should be %g\n", i, limits2[i], limits[i]);
          }
          if((closures2[i]!=0) != (closures[i]!=0)) {
            errln("FAIL: closure #%d = %s, should be %s\n", i, closures2[i]?"T":"F", closures[i]?"T":"F");
          }
        }
    }
#endif

    // Now test both format objects
    UnicodeString exp[] = {
        /*-0.5 => */ ",1)",
        /* 0.0 => */ ",1)",
        /* 0.5 => */ ",1)",
        /* 1.0 => */ "[1,2]",
        /* 1.5 => */ "[1,2]",
        /* 2.0 => */ "[1,2]",
        /* 2.5 => */ "(2,3]",
        /* 3.0 => */ "(2,3]",
        /* 3.5 => */ "(3,4)",
        /* 4.0 => */ "[4,5)",
        /* 4.5 => */ "[4,5)",
        /* 5.0 => */ "[5,",
        /* 5.5 => */ "[5,"
    };

    // Each format object should behave exactly the same
    ChoiceFormat* FMT[] = { &fmt1, &fmt2 };
    for (int32_t pass=0; pass<2; ++pass) {
        int32_t j=0;
        for (int32_t ix=-5; ix<=55; ix+=5) {
            double x = ix / 10.0; // -0.5 to 5.5 step +0.5
            FMT[pass]->format(x, str);
            if (str == exp[j]) {
                logln((UnicodeString)"Ok: " + x + " => " + str);
            } else {
                errln((UnicodeString)"FAIL: " + x + " => " + str +
                      ", expected " + exp[j]);
            }
            str.truncate(0);
            ++j;
        }
    }
}
Esempio n. 5
0
    void summarizeLibraryTypeCounts(boost::filesystem::path& opath){
        LibraryFormat fmt1(ReadType::SINGLE_END, ReadOrientation::NONE, ReadStrandedness::U);
        LibraryFormat fmt2(ReadType::SINGLE_END, ReadOrientation::NONE, ReadStrandedness::U);

        std::ofstream ofile(opath.string());

        fmt::MemoryWriter errstr;

        auto log = spdlog::get("jointLog");

        uint64_t numFmt1{0};
        uint64_t numFmt2{0};
        uint64_t numAgree{0};
        uint64_t numDisagree{0};

        for (auto& rl : readLibraries_) {
            auto fmt = rl.format();
            auto& counts = rl.libTypeCounts();

            // If the format is un-stranded, check that
            // we have a similar number of mappings in both
            // directions and then aggregate the forward and
            // reverse counts.
            if (fmt.strandedness == ReadStrandedness::U) {
                std::vector<ReadStrandedness> strands;
                switch (fmt.orientation) {
                    case ReadOrientation::SAME:
                    case ReadOrientation::NONE:
                        strands.push_back(ReadStrandedness::S);
                        strands.push_back(ReadStrandedness::A);
                        break;
                    case ReadOrientation::AWAY:
                    case ReadOrientation::TOWARD:
                        strands.push_back(ReadStrandedness::AS);
                        strands.push_back(ReadStrandedness::SA);
                        break;
                }

                fmt1.type = fmt.type; fmt1.orientation = fmt.orientation;
                fmt1.strandedness = strands[0];
                fmt2.type = fmt.type; fmt2.orientation = fmt.orientation;
                fmt2.strandedness = strands[1];

                numFmt1 = 0;
                numFmt2 = 0;
                numAgree = 0;
                numDisagree = 0;

                for (size_t i = 0; i < counts.size(); ++i) {
                    if (i == fmt1.formatID()) {
                        numFmt1 = counts[i];
                    } else if (i == fmt2.formatID()) {
                        numFmt2 = counts[i];
                    } else {
                        numDisagree += counts[i];
                    }
                }
                numAgree = numFmt1 + numFmt2;
                double ratio = static_cast<double>(numFmt1) / (numFmt1 + numFmt2);

                if ( std::abs(ratio - 0.5) > 0.01) {
                    errstr << "NOTE: Read Lib [" << rl.readFilesAsString() << "] :\n";
                    errstr << "\nDetected a strand bias > 1\% in an unstranded protocol "
                           << "check the file: " << opath.string() << " for details\n";

                    log->warn() << errstr.str();
                    errstr.clear();
                }

                ofile << "========\n"
                      << "Read library consisting of files: "
                      << rl.readFilesAsString()
                      << "\n\n"
                      << "Expected format: " << rl.format()
                      << "\n\n"
                      << "# of consistent alignments: " << numAgree << "\n"
                      << "# of inconsistent alignments: " << numDisagree << "\n"
                      << "strand bias = " << ratio << " (0.5 is unbiased)\n"
                      << "# alignments with format " << fmt1 << ": " << numFmt1 << "\n"
                      << "# alignments with format " << fmt2 << ": " << numFmt2 << "\n"
                      << "\n========\n";
            } else {
                numAgree = 0;
                numDisagree = 0;

                for (size_t i = 0; i < counts.size(); ++i) {
                    if (i == fmt.formatID()) {
                        numAgree = counts[i];
                    } else {
                        numDisagree += counts[i];
                    }
                } // end for

                ofile << "========\n"
                      << "Read library consisting of files: "
                      << rl.readFilesAsString()
                      << "\n\n"
                      << "Expected format: " << rl.format()
                      << "\n\n"
                      << "# of consistent alignments: " << numAgree << "\n"
                      << "# of inconsistent alignments: " << numDisagree << "\n"
                      << "\n========\n";

            } //end else

            double disagreeRatio = static_cast<double>(numDisagree) / (numAgree + numDisagree);
            if (disagreeRatio > 0.05) {
                errstr << "NOTE: Read Lib [" << rl.readFilesAsString() << "] :\n";
                errstr << "\nGreater than 5\% of the alignments (but not, necessarily reads) "
                       << "disagreed with the provided library type; "
                       << "check the file: " << opath.string() << " for details\n";

                log->warn() << errstr.str();
                errstr.clear();
            }

            ofile << "---- counts for each format type ---\n";
            for (size_t i = 0; i < counts.size(); ++i) {
                ofile << LibraryFormat::formatFromID(i) << " : " << counts[i] << "\n";
            }
            ofile << "------------------------------------\n\n";
        }
        ofile.close();
    }
Esempio n. 6
0
// ============================================================================
void DataOnDemandSvc::dump
( const MSG::Level level ,
  const bool       mode  )  const
{
  if ( m_algs.empty()  &&  m_nodes.empty() ) { return ; }

  typedef std::pair<std::string,std::string> Pair ;
  typedef std::map<std::string,Pair>         PMap ;

  PMap _m ;
  for ( AlgMap::const_iterator alg = m_algs.begin() ;
        m_algs.end() != alg ; ++alg )
  {
    PMap::const_iterator check = _m.find(alg->first) ;
    if ( _m.end() != check )
    {
      stream()
        << MSG::WARNING
        << " The data item is activated for '"
        << check->first << "' as '" << check->second.first << "'" << endmsg ;
    }
    const Leaf& l = alg->second ;
    std::string nam = ( l.name == l.type ? l.type  : (l.type+"/"+l.name) ) ;
    //
    if ( !mode && 0 == l.num ) { continue ; }
    //
    std::string val ;
    if ( mode ) { val = ( 0 == l.algorithm ) ? "F" : "T" ; }
    else { val = boost::lexical_cast<std::string>( l.num ) ; }
    //
    _m[ no_prefix ( alg->first , m_prefix ) ] = std::make_pair ( nam , val ) ;
  }
  // nodes:
  for ( NodeMap::const_iterator node = m_nodes.begin() ;
        m_nodes.end() != node ; ++node )
  {
    PMap::const_iterator check = _m.find(node->first) ;
    if ( _m.end() != check )
    {
      stream()
        << MSG::WARNING
        << " The data item is already activated for '"
        << check->first << "' as '" << check->second.first << "'" << endmsg ;
    }
    const Node& n = node->second ;
    std::string nam = "'" + n.name + "'"  ;
    //
    std::string val ;

    if ( !mode && 0 == n.num ) { continue ; }

    if ( mode ) { val = ( 0 == n.clazz ) ? "F" : "T" ; }
    else { val = boost::lexical_cast<std::string>( n.num ) ; }
    //
    _m[ no_prefix ( node->first , m_prefix ) ] = std::make_pair ( nam , val ) ;
  }
  //
  if ( _m.empty() ) { return ; }

  // find the correct formats
  size_t n1 = 0 ;
  size_t n2 = 0 ;
  size_t n3 = 0 ;
  for  ( PMap::const_iterator it = _m.begin() ; _m.end() != it ; ++it )
  {
    n1 = std::max ( n1 , it->first.size()         ) ;
    n2 = std::max ( n2 , it->second.first.size()  ) ;
    n3 = std::max ( n3 , it->second.second.size() ) ;
  }
  if ( 10 > n1 ) { n1 = 10 ; }
  if ( 10 > n2 ) { n2 = 10 ; }
  if ( 60 < n1 ) { n1 = 60 ; }
  if ( 60 < n2 ) { n2 = 60 ; }
  //

  const std::string _f = " | %%1$-%1%.%1%s | %%2$-%2%.%2%s | %%3$%3%.%3%s |" ;
  boost::format _ff ( _f ) ;
  _ff % n1 % n2 % n3 ;

  const std::string _format  = _ff.str() ;

  MsgStream& msg = stream() << level ;

  if ( mode ) { msg << "Data-On-Demand Actions enabled for:"       ; }
  else        { msg << "Data-On-Demand Actions has been used for:" ; }

  boost::format fmt1( _format)  ;
  fmt1 % "Address" % "Creator" % ( mode ? "S" : "#" ) ;
  //
  const std::string header = fmt1.str() ;
  std::string line = std::string( header.size() , '-' ) ;
  line[0] = ' ' ;

  msg << std::endl << line
      << std::endl << header
      << std::endl << line ;

  // make the actual printout:
  for ( PMap::const_iterator item = _m.begin() ;
        _m.end() != item ; ++item )
  {
    boost::format fmt( _format)  ;
    msg << std::endl <<
      ( fmt % item->first % item->second.first % item->second.second ) ;
  }

  msg << std::endl << line << endmsg ;

}