#############################################################################
##  DU_TLIB.pm - miscellaneous Daylight User toolkit (DayPerl) subroutines
##
##  See also DU_LIB.pm, non-toolkit subroutines.
#############################################################################
## Subroutines:
##   DU_PRINTERRORS
##   DU_STREAM2SEQ
##   DU_EXPLICIT_HYDRO
#############################################################################
##  Author: Jeremy Yang
##  Rev:  26 May 1999
#############################################################################

package DU_TLIB;

use DayPerl;

#############################################################################
###  DU_PRINTERRORS()
#############################################################################
sub main'DU_PRINTERRORS
{
  local($level) = @_;
  $level = DX_ERR_ERROR if (!$level);

  $errs = dt_errors($level);
  while (NULL_OB != ($err = dt_next($errs))) {
    $errstring = dt_stringvalue($err);
    print "$errstring\n";
  }
  dt_errorclear();
}

#############################################################################
###  DU_STREAM2SEQ($stream)
#############################################################################
sub main'DU_STREAM2SEQ
{
  my $stream = shift;
  my $seq = dt_alloc_seq();

  dt_reset($stream);
  while (NULL_OB != ($ob = dt_next($stream))) { dt_append($seq, $ob); }
  return $seq;
}

#############################################################################
###  DU_EXPLICIT_HYDRO - make H's explicit
###  (This has the problem of losing chirality.)
#############################################################################
sub main'DU_EXPLICIT_HYDRO
{
  my $mol = shift;
  my ($atoms,$atomseq,$atom,$hyd,$imp_hcount,$ok);

  dt_mod_on($mol);
  $atoms = dt_stream($mol, TYP_ATOM);
  $atomseq = &main'DU_STREAM2SEQ($atoms);
  dt_dealloc($atoms);
  while (NULL_OB != ($atom = dt_next($atomseq))) {
    $imp_hcount = dt_imp_hcount($atom);
    while ($imp_hcount-- > 0) {
      $hyd = dt_addatom($mol, DX_ATN_H, 0);
      dt_addbond($atom, $hyd, 1);
    }
    dt_setimp_hcount($atom, 0);
  }
  $ok = dt_mod_off($mol);

  return ($ok);
}


#############################################################################
1;  ### package return value
