faif
Loci.hpp
1 #ifndef FAIF_HAPL_LOCI_HPP
2 #define FAIF_HAPL_LOCI_HPP
3 
4 #include <vector>
5 #include <algorithm>
6 #include <boost/lexical_cast.hpp>
7 
8 #include "../Value.hpp"
9 
10 namespace faif {
11 
12  /** \brief Population genetics (haplotype and marker analyzis) primitives and algorithms */
13  namespace hapl {
14 
15  /** \brief the helping structure, implements the Allele members */
16  struct AlleleImpl : public std::pair<std::string, bool> {
17 
18  /** \brief c-tor */
19  AlleleImpl(const std::string& name = std::string(""), bool is_silent = false)
20  : std::pair<std::string,bool>(name, is_silent) {}
21 
22  /** \brief equality comparison */
23  bool operator==(const AlleleImpl& a) const {
24  return first == a.first;
25  }
26 
27  /** \brief non-equality comparison */
28  bool operator!=(const AlleleImpl& a) const {
29  return first != a.first;
30  }
31  };
32 
33  /** \brief ostream operator */
34  inline std::ostream& operator<<(std::ostream& os, const AlleleImpl& a) {
35  os << a.first;
36  if(a.second)
37  os << "(silent)";
38  return os;
39  }
40 
41  /** \brief Allele, variant of a gene */
43 
44  /** \brief accessor */
45  inline std::string getName(const Allele& a) {
46  return a.get().first;
47  }
48 
49  /** \brief accessor */
50  inline bool isSilent(const Allele& a) {
51  return a.get().second;
52  }
53 
54  /** \brief Locus, place on DNA contatining the Allele */
56 
57  namespace {
58 
59  /** \brief generates the allele name */
60  std::string generateAlleleName(const std::string& locus_name, int allele_number) {
61  return locus_name + boost::lexical_cast<std::string>(allele_number);
62  }
63 
64  }
65 
66  /** \brief helping method - create the entire locus, with number variants.
67 
68  Variants name are the locus name and the variant number,
69  i.e. silent variant has name 'name0', first non silent variant has name 'name1' etc.
70  */
71  Locus createLocus(const std::string& name, int num_variants, bool has_silent) {
72  Locus l(name);
73  int num = 0;
74  //the silent allele
75  if(has_silent)
76  l.insert( AlleleImpl(generateAlleleName(name, num), true) );
77 
78  //not sillent alleles
79  for(num = 1; num < num_variants; num++) {
80  l.insert( AlleleImpl( generateAlleleName(name, num), false) );
81  }
82 
83  //if the locus does not have silent allele, another allele (because always create num alleles)
84  if(!has_silent)
85  l.insert( AlleleImpl( generateAlleleName(name, num), false) );
86 
87  return l;
88  }
89 
90  /** \brief Loci, the collection(sequence) of Locus, e.g. genotype */
91  typedef std::vector<Locus> Loci;
92 
93  /** ostream operator for loci */
94  inline std::ostream& operator<<(std::ostream& os, const Loci& loci) {
95  std::copy(loci.begin(), loci.end(), std::ostream_iterator<Locus>(os,";") );
96  return os;
97  }
98 
99  } //namespace hapl
100 } //namespace faif
101 
102 #endif //FAIF_HAPL_LOCI_HPP
DomainEnumerate< Allele > Locus
Locus, place on DNA contatining the Allele.
Definition: Loci.hpp:55
Definition: Chain.h:17
bool operator==(const AlleleImpl &a) const
equality comparison
Definition: Loci.hpp:23
Locus createLocus(const std::string &name, int num_variants, bool has_silent)
helping method - create the entire locus, with number variants.
Definition: Loci.hpp:71
STL namespace.
std::ostream & operator<<(std::ostream &os, const AlleleImpl &a)
ostream operator
Definition: Loci.hpp:34
Value & get()
accessor
Definition: Value.hpp:99
ValueNominal< AlleleImpl > Allele
Allele, variant of a gene.
Definition: Loci.hpp:42
std::string getName(const Allele &a)
accessor
Definition: Loci.hpp:45
forward declaration
Definition: Value.hpp:60
the helping structure, implements the Allele members
Definition: Loci.hpp:16
bool isSilent(const Allele &a)
accessor
Definition: Loci.hpp:50
AlleleImpl(const std::string &name=std::string(""), bool is_silent=false)
c-tor
Definition: Loci.hpp:19
ValueId insert(const typename Val::Value &value)
search for value equal to given, if found return the value identifier, otherwise insert new into coll...
Definition: Value.hpp:310
std::vector< Locus > Loci
Loci, the collection(sequence) of Locus, e.g. genotype.
Definition: Loci.hpp:91
nominal attribute template (equality comparable)
Definition: Value.hpp:65
bool operator!=(const AlleleImpl &a) const
non-equality comparison
Definition: Loci.hpp:28