libStatGen Software  1
PedigreeAlleles.h
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __PEDALLELES_H__
00019 #define __PEDALLELES_H__
00020 
00021 #include "LongInt.h"
00022 
00023 class Alleles
00024 {
00025 public:
00026     char   one;
00027     char   two;
00028 
00029     Alleles()
00030     {
00031         one = two = 0;
00032     }
00033 
00034     char & operator [](int i)
00035     {
00036         return (i == 1) ? one : two;
00037     }
00038 
00039     // is the genotype fully defined?
00040     bool isKnown()
00041     {
00042         return (one * two) != 0;
00043     }
00044     bool isHeterozygous()
00045     {
00046         return isKnown() && (one != two);
00047     }
00048     bool isHomozygous()
00049     {
00050         return isKnown() && (one == two);
00051     }
00052     bool hasAllele(int a)
00053     {
00054         return (one == a) || (two == a);
00055     }
00056 
00057     // in a bi-allelic system (a, NOT a)
00058     bool isHeterozygousFor(int a)
00059     {
00060         return isHeterozygous() && hasAllele(a);
00061     }
00062     bool isHomozygousFor(int a)
00063     {
00064         return !(isHeterozygousFor(a));
00065     }
00066 
00067     // how may alleles a in this genotype?
00068     int countAlleles(int a)
00069     {
00070         return ((one == a) ? 1 : 0) + ((two == a) ? 1 : 0);
00071     }
00072 
00073     // what is the other allele, assuming genotype is (a, X)
00074     int otherAllele(int a)
00075     {
00076         return ((one == a) ? two : one);
00077     }
00078 
00079     // are two unordered genotypes identical?
00080     int identicalTo(Alleles & al)
00081     {
00082         return ((al.one == one) && (al.two == two)) ||
00083                ((al.two == one) && (al.one == two));
00084     }
00085 
00086     // how many alleles are identical by state
00087     int countIBS(Alleles & al)
00088     {
00089         return (one == al.one) ?
00090                ((two == al.two) ? 2 : 1) :
00091                        ((one == al.two) ?
00092                         ((two == al.one) ? 2 : 1) :
00093                         (((two == al.one) || (two == al.two)) ? 1 : 0));
00094     }
00095 
00096     int operator == (Alleles & rhs)
00097 {
00098         return identicalTo(rhs);
00099     }
00100     int operator != (Alleles & rhs)
00101     {
00102         return !identicalTo(rhs);
00103     }
00104 
00105     char Hi()
00106     {
00107         return one > two ? one : two;
00108     }
00109     char Lo()
00110     {
00111         return one > two ? two : one;
00112     }
00113 
00114     int SequenceCoded()
00115     {
00116         return isKnown() ? Hi() *(Hi() - 1) / 2 + Lo() : 0;
00117     }
00118 
00119     longint BinaryCoded()
00120     {
00121         if (isKnown())
00122         {
00123             longint allele1(1);
00124             longint allele2(1);
00125 
00126             allele1 <<= one - 1;
00127             allele2 <<= two - 1;
00128 
00129             return allele1 | allele2;
00130         }
00131         else
00132             return NOTZERO;
00133     }
00134 
00135     void Intersect(Alleles & geno)
00136     {
00137         char a1 = Lo(), a2 = Hi();
00138         char b1 = geno.Lo(), b2 = geno.Hi();
00139 
00140         if (a1 == b1 && a2 == b2)
00141             return;
00142         if (a1 == b1 || a1 == b2)
00143             one = two = a1;
00144         else if (a2 == b1 || a2 == b2)
00145             one = two = a2;
00146         else
00147             one = two = 0;
00148     }
00149 
00150     void Intersect(char allele)
00151     {
00152         if (one != allele && two != allele)
00153             one = two = 0;
00154         else
00155             one = two = allele;
00156     }
00157 
00158     bool AddAllele(char allele)
00159     {
00160         if (one == allele || two == allele)
00161             return true;
00162 
00163         if (one != 0 && two != 0)
00164             return false;
00165 
00166         if (one == 0) one = allele;
00167         else two = allele;
00168         return true;
00169     }
00170 
00171     void Wipe()
00172     {
00173         one = two = 0;
00174     }
00175 };
00176 
00177 #endif
00178 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends