00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef CGA_TOOLS_COMMAND_MERGEMAP2SAM_CONVERTER_HPP_
00016 #define CGA_TOOLS_COMMAND_MERGEMAP2SAM_CONVERTER_HPP_ 1
00017
00019
00020 #include "cgatools/core.hpp"
00021 #include "cgatools/util/RangeSet.hpp"
00022 #include "Map2SamConverter.hpp"
00023 #include "EvidenceCache.hpp"
00024
00025 #include <boost/array.hpp>
00026
00027 namespace cgatools { namespace mapping {
00028
00029 class DoubleMatrix;
00030
00031 class MergedMap2SamConfig : public Map2SamConfig
00032 {
00033 public:
00034 MergedMap2SamConfig()
00035 : Map2SamConfig()
00036 ,mapqAlpha_(1E-12)
00037 ,minMateGapFrequency_(1E-7)
00038 ,printAlleleInfo_(false)
00039 ,primaryMappingsOnly_(true)
00040 ,minMapQ_(0)
00041 ,debugOutStream_(&std::cerr)
00042 {
00043 skipNotMapped_ = true;
00044 }
00045
00046 double mapqAlpha_;
00047 double minMateGapFrequency_;
00048 bool printAlleleInfo_;
00049
00050 bool primaryMappingsOnly_;
00051
00052 uint8_t minMapQ_;
00053 std::string evidenceCacheRoot_;
00054 std::ostream* debugOutStream_;
00055 };
00056
00057 class MergedMap2SamConverter : public Map2SamConverter
00058 {
00059 public:
00060 typedef boost::array<std::vector<size_t>,2> MappingIndicesBySide;
00061 typedef boost::array<std::vector<double>,2> MappingWeightsBySide;
00062
00063 MergedMap2SamConverter(const MergedMap2SamConfig &config, std::ostream &outSamFile)
00064 : Map2SamConverter(config, outSamFile), config_(config)
00065 {}
00066
00067 virtual void init();
00068
00069 protected:
00071 virtual bool alternativeMappingProcessing(
00072 const mapping::ReadsRecord& readsRecord, SamRecordArray& records);
00073
00075 virtual void filterMappingRecords(SamRecordArray& records);
00076
00078 virtual void writeMappingRecord(const SamRecord &m);
00079
00081 void addEvidenceRecords(const mapping::ReadsRecord& readsRecord, SamRecordArray& records);
00083 void regroupMappings(const mapping::ReadsRecord& readsRecord, SamRecordArray& records);
00084
00086 void computeMappingWeights(const SamRecordArray& records,
00087 const MappingIndicesBySide& mappings, MappingWeightsBySide& weights) const;
00090 void filterDuplicatedMappings(SamRecordArray& records) const;
00091
00092 void computeWeightMatrix(
00093 const SamRecordArray& records, const MappingIndicesBySide& mappings,
00094 const MappingWeightsBySide& weights, DoubleMatrix& weightMatrix, DoubleMatrix& mateGaps) const;
00095
00096 double computeTotalWeight(const DoubleMatrix& weightMatrix, MappingWeightsBySide& sumAllPairs) const;
00097
00098 void findBestCandidates(const DoubleMatrix& m, const DoubleMatrix& mateGaps,
00099 const MappingIndicesBySide& mappingsBySide, const MappingWeightsBySide& sumAllPairs,
00100 MappingIndicesBySide& bestGroupMappings, MappingIndicesBySide& groups) const;
00101
00103 void reweightSingleMappings(SamRecordArray& records, const MappingIndicesBySide& mappingsBySide,
00104 const MappingWeightsBySide& dnbArmWeights,const MappingWeightsBySide& sumAllPairs) const;
00105
00107 void reweightDoubleArmMappings(SamRecordArray& records, const MappingIndicesBySide& mappingsBySide,
00108 const DoubleMatrix& weightMatrix, const DoubleMatrix& mateGapFrequency,
00109 const MappingWeightsBySide& sumAllPairs, const MappingIndicesBySide& bestFullMappedArms,
00110 const MappingIndicesBySide& groups, const double &totalWeight ) const;
00111
00113 void detectTheBestMappingAndMakeItPrimary(SamRecordArray& records) const;
00114
00116 void leaveOneBestMapping(SamRecordArray& records) const;
00117
00119 void dumpBestLocalMatches(const mapping::ReadsRecord& readsRecord, const SamRecordArray& records,
00120 const MappingIndicesBySide& mappingsBySide, const MappingWeightsBySide& dnbArmWeights,
00121 const DoubleMatrix& weights, const DoubleMatrix& mateGaps,
00122 const MappingWeightsBySide& sumAllPairs, const MappingIndicesBySide& bestFullMappedArms,
00123 const MappingIndicesBySide& groups,
00124 const double& totalWeight) const;
00125
00126 void printMatrix(const DoubleMatrix& m, std::ostream& ostr) const;
00127
00128 const MergedMap2SamConfig & config_;
00129
00130 mapping::BatchRecords evidenceBatchRecords_;
00131 };
00132
00133 } }
00134
00135 #endif // CGA_TOOLS_COMMAND_MERGEMAP2SAM_CONVERTER_HPP_