00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef CGATOOLS_VARIANTS_VARIANTFILEITERATOR_HPP_
00016 #define CGATOOLS_VARIANTS_VARIANTFILEITERATOR_HPP_ 1
00017
00019
00020 #include "cgatools/core.hpp"
00021 #include "cgatools/util/DelimitedFile.hpp"
00022 #include "cgatools/variants/Locus.hpp"
00023
00024 namespace cgatools { namespace variants {
00025
00026 class VariantFileIterator : boost::noncopyable
00027 {
00028 typedef reference::CrrFile CrrFile;
00029
00030 public:
00031 VariantFileIterator(const CrrFile& crr);
00032 void open(const std::string& fn);
00033 void close();
00034
00035 bool eof() const
00036 {
00037 return eof_;
00038 }
00039
00040 const Locus& operator*() const
00041 {
00042 return locus_;
00043 }
00044
00045 const Locus* operator->() const
00046 {
00047 return &locus_;
00048 }
00049
00050 VariantFileIterator& operator++()
00051 {
00052 readLocus();
00053 return *this;
00054 }
00055
00056 const std::string& getFileName()
00057 {
00058 return name_;
00059 }
00060
00061 void error(const std::string& msg) const;
00062 void locusCallError(const std::string& msg, const Locus& locus, const Call& call) const;
00063 void locusError(const std::string& msg, const Locus& locus) const;
00064
00065 void setReferenceCoverValidation(bool validate)
00066 {
00067 referenceCoverValidation_ = validate;
00068 }
00069
00070 const util::DelimitedFile::Metadata& getMetadata() const
00071 {
00072 return df_->getMetadata();
00073 }
00074
00075 const std::vector<std::string>& getColumnHeaders() const
00076 {
00077 return df_->getColumnHeaders();
00078 }
00079
00080 const std::vector<std::string>& getAnnotationColumnHeaders() const
00081 {
00082 return annotationColumnHeaders_;
00083 }
00084
00085 bool xRefIsAlleleSpecific() const
00086 {
00087 return locus_.xRefIsAlleleSpecific();
00088 }
00089
00090 const std::string& getFilterString() const
00091 {
00092 return filterStr_;
00093 }
00094
00100 void getReadCounts(int32_t* allele1ReadCount,
00101 int32_t* allele2ReadCount,
00102 int32_t* referenceAlleleReadCount,
00103 int32_t* totalReadCount,
00104 cgdata::EvidenceReader& evidence,
00105 const Locus& locus) const;
00106
00107 bool hasAnnotation(const std::string& name) const;
00108
00111 size_t getAnnotationIndex(const std::string& name) const;
00112
00113 void fillOlplFileMetadata(util::DelimitedFile::Metadata& meta) const;
00114 void writeOlplFileHeader(std::ostream& out) const;
00115
00116 bool isOlpl() const
00117 {
00118 return oneLinePerLocus_;
00119 }
00120
00121 class CallFilter
00122 {
00123 public:
00124 virtual ~CallFilter() { }
00125 virtual bool passesFilter(const Locus& locus, const Call& call) const = 0;
00126 };
00127
00128 private:
00130 struct LocusData
00131 {
00132 LocusData()
00133 : locus_(0),
00134 ploidy_(0),
00135 range_(0,0,0)
00136 {
00137 alleleVarScoreVAF_.assign(Call::EMPTY_SCORE);
00138 alleleVarScoreEAF_.assign(Call::EMPTY_SCORE);
00139 }
00140
00141 uint32_t locus_;
00142 uint16_t ploidy_;
00143 reference::Range range_;
00144 std::string zygosity_;
00145 std::string varType_;
00146 std::string reference_;
00147 boost::array<std::string, 2> alleleSeq_;
00148 boost::array<int32_t, 2> alleleVarScoreVAF_;
00149 boost::array<int32_t, 2> alleleVarScoreEAF_;
00150 boost::array<std::vector<std::string>, 2> alleleVarFilter_;
00151 boost::array<std::string, 2> hapLink_;
00152 boost::array<std::string, 2> xRef_;
00153 boost::array<std::string, 2> alleleFreq_;
00154 boost::array<std::vector<Call::AlternativeCall>, 2> alleleAlternativeCalls_;
00155 std::vector<std::string> extras_;
00156 };
00157
00158 void applyFilters();
00159 void applyNoCall(Call& call);
00160 void parseFilters(const std::string& fnWithFilters);
00161 boost::shared_ptr<VariantFileIterator::CallFilter>
00162 parseFilter(const std::string& andPart) const;
00163 void readPending();
00164 void readLocus();
00165 void readMultilineLocus();
00166 void readOlplLocus();
00167 void fillOlplLocus();
00168 void detectFileType();
00169 void addOlplParsers();
00170 void createNoCallLocus(const reference::Range& r);
00171 bool isPureNoCall(const LocusData& d);
00172
00173 const CrrFile* crr_;
00174 boost::shared_ptr<std::istream> istream_;
00175 boost::shared_ptr<util::DelimitedFile> df_;
00176 std::string name_;
00177 std::string filterStr_;
00178 Locus locus_;
00179 Call pendingCall_;
00180 LocusData pendingLocusLine_;
00181 bool emptyFile_;
00182 bool eof_;
00183 bool hasPending_;
00184 bool referenceCoverValidation_;
00185 boost::uint64_t headerEnd_;
00186 boost::uint64_t fileEnd_;
00187
00188 std::vector< std::vector< boost::shared_ptr<CallFilter> > > filters_;
00189
00190 bool oneLinePerLocus_;
00191 std::vector<std::string> annotationColumnHeaders_;
00192 size_t allele1ReadCountOffset_;
00193 size_t allele2ReadCountOffset_;
00194 size_t referenceAlleleReadCountOffset_;
00195 size_t totalReadCountOffset_;
00196 };
00197
00198 } }
00199
00200 #endif // CGATOOLS_VARIANTS_VARIANTFILEITERATOR_HPP_