bpp-seq-omics  2.4.1
PlinkOutputMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: PlinkOutputMafIterator.cpp
3 // Authors: Julien Dutheil
4 // Created: Tue Dec 10 2015
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (2015)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "PlinkOutputMafIterator.h"
41 
42 //From bpp-seq:
45 #include <Bpp/Seq/SiteTools.h>
46 #include <Bpp/Seq/SequenceWalker.h>
47 
48 using namespace bpp;
49 
50 //From the STL:
51 #include <string>
52 #include <numeric>
53 #include <ctime>
54 
55 using namespace std;
56 
58  for (size_t i = 0; i < species_.size(); ++i) {
59  ped_[i] = "FAM001\t" + TextTools::toString(i + 1) + "\t0\t0\t0\t0";
60  }
61 }
62 
63 
64 void PlinkOutputMafIterator::parseBlock_(std::ostream& out, const MafBlock& block)
65 {
66  //Preliminary stuff...
68  for (size_t i = 0; i < species_.size(); ++i) {
69  if (block.hasSequenceForSpecies(species_[i])) {
70  sites.addSequence(block.getSequenceForSpecies(species_[i]));
71  //Note: in case of duplicates, this takes the first sequence.
72  } else {
73  //Block with missing species are ignored.
74  return;
75  }
76  }
77  //Get the reference species for coordinates:
78  if (! block.hasSequenceForSpecies(refSpecies_))
79  return;
80  const MafSequence& refSeq = block.getSequenceForSpecies(refSpecies_);
81  string chr = refSeq.getChromosome();
82  if (chr != currentChr_) {
83  currentChr_ = chr;
84  lastPosition_ = 0;
85  } else {
86  //Check that block are ordered according to reference sequence:
87  if (refSeq.start() < lastPosition_)
88  throw Exception("PlinkOutputMafIterator: blocks are not projected according to reference sequence: " + refSeq.getDescription() + "<!>" + TextTools::toString(lastPosition_) + ".");
89  lastPosition_ = refSeq.stop();
90  }
91 
92  string chrStr = chr;
93  if (recodeChr_) {
94  auto i = chrCodes_.find(chr);
95  if (i != chrCodes_.end()) {
96  chrStr = TextTools::toString(i->second);
97  } else {
98  unsigned int code = currentCode_++;
99  chrCodes_[chr] = code;
100  chrStr = TextTools::toString(code);
101  }
102  }
103 
104  SequenceWalker walker(refSeq);
105  size_t offset = refSeq.start();
106  int gap = refSeq.getAlphabet()->getGapCharacterCode();
107 
108  //Now we shall scan all sites for SNPs:
109  for (size_t i = 0; i < sites.getNumberOfSites(); i++) {
110  if (refSeq[i] == gap)
111  continue;
112 
113  //We call SNPs only at position without gap or unresolved characters, and for biallelic sites:
115  string pos = "NA";
116  if (refSeq[i] != gap) {
117  pos = TextTools::toString(offset + walker.getSequencePosition(i) + 1);
118  }
119  string alleles = sites.getSite(i).toString();
120  for (size_t j = 0; j < alleles.size(); ++j) {
121  ped_[j] += "\t" + TextTools::toString(alleles[j]) + " " + alleles[j];
122  }
123  // SNP identifier are built as <chr>.<pos>
124  out << chrStr << "\t" << chr << "." << pos << "\t";
125  if (!map3_)
126  out << "0\t"; //Add null genetic distance
127  out << pos << endl;
128  }
129  }
130 }
131 
133  for (size_t i = 0; i < ped_.size(); ++i)
134  out << ped_[i] << endl;
135 }
136 
static const DNA DNA_ALPHABET
virtual int getGapCharacterCode() const=0
virtual std::string toString() const=0
virtual const Alphabet * getAlphabet() const=0
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
const MafSequence & getSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:139
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
std::string getDescription() const
Definition: MafSequence.h:188
const std::string & getChromosome() const
Definition: MafSequence.h:164
size_t start() const
Definition: MafSequence.h:116
size_t stop() const
Definition: MafSequence.h:121
void parseBlock_(std::ostream &out, const MafBlock &block)
void writePedToFile_(std::ostream &out)
size_t getSequencePosition(size_t alnPos)
static bool isComplete(const IntCoreSymbolList &site)
static size_t getNumberOfDistinctCharacters(const IntCoreSymbolList &site)
void addSequence(const Sequence &sequence, bool checkName=true)
const Site & getSite(size_t siteIndex) const
size_t getNumberOfSites() const
std::string toString(T t)