#ifndef __FILE_EMMUSR_H_SEEN__
#define __FILE_EMMUSR_H_SEEN__

/*-----------------------------------------------------------------------------

Copyright (C) 2004, 2005, 2006, 2007.

A. Ronald Gallant
Post Office Box 659
Chapel Hill NC 27514-0659
USA   

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

-----------------------------------------------------------------------------*/

#include "libsnp.h"
#include "libsmm.h"
#include "emm_base.h"
#include "snp.h"

namespace emm {
    
  class sv_usrmod;

  typedef sv_usrmod usrmod_type;

  const INT_32BIT fixed_seed = 770116;

  class sv_usrmod : public libsmm::usrmod_base {
  private:
    scl::realmat data;
    scl::realmat rho;
    INTEGER slen;
    INTEGER spin;
    INTEGER lrho;
    INTEGER lstats;
    INT_32BIT variable_seed;
    bool sv_simulate (INT_32BIT& seed, INTEGER len, 
      scl::realmat& sim, scl::realmat& stats, scl::realmat& latent);
  public:
    sv_usrmod (const scl::realmat& dat, INTEGER len_mod_parm, 
      INTEGER len_mod_func, const std::vector<std::string>& mod_pfvec,
      const std::vector<std::string>& mod_alvec, std::ostream& detail);
    INTEGER len_rho() {return lrho;}
    INTEGER len_stats() {return lstats;}
    bool gen_sim(scl::realmat& sim, scl::realmat& stats)  
      //Same seed every call
      {
        scl::realmat latent;
        INT_32BIT seed = fixed_seed;
        return sv_simulate(seed,slen,sim,stats,latent);
      }
    bool gen_bootstrap(std::vector<scl::realmat>& bs); 
      //Different seed each call
    void get_rho(scl::realmat& parm) { parm = rho; }
    void set_rho(const scl::realmat& parm) { rho = parm; }
    bool support(const scl::realmat& rho);
    scl::den_val prior(const scl::realmat& rho, const scl::realmat& stats);
    void write_usrvar(const char* filename) 
      {
        scl::realmat sim, stats, latent;
        INT_32BIT seed = fixed_seed;
        if ( sv_simulate(seed,slen,sim,stats,latent) ) {
          #if defined GNU_GPP_COMPILER
            vecwrite(filename, rbind(sim,latent));
          #else
            scl::vecwrite(filename, scl::rbind(sim,latent));
          #endif
        }
      }
  };
  
}
#endif
