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     public:
00030         VariantFileIterator(const CrrFile& crr);
00031         void open(const std::string& fn);
00032         void close();
00033 
00034         bool eof() const
00035         {
00036             return eof_;
00037         }
00038 
00039         const Locus& operator*() const
00040         {
00041             return locus_;
00042         }
00043 
00044         const Locus* operator->() const
00045         {
00046             return &locus_;
00047         }
00048 
00049         VariantFileIterator& operator++()
00050         {
00051             readLocus();
00052             return *this;
00053         }
00054 
00055         const std::string& getFileName()
00056         {
00057             return name_;
00058         }
00059 
00060         void error(const std::string& msg) const;
00061         void locusCallError(const std::string& msg, const Locus& locus, const Call& call) const;
00062         void locusError(const std::string& msg, const Locus& locus) const;
00063 
00064         void setReferenceCoverValidation(bool validate)
00065         {
00066             referenceCoverValidation_ = validate;
00067         }
00068 
00069         const util::DelimitedFile::Metadata& getMetadata() const
00070         {
00071             return df_->getMetadata();
00072         }
00073 
00074         const std::vector<std::string>& getColumnHeaders() const
00075         {
00076             return df_->getColumnHeaders();
00077         }
00078 
00079         const std::vector<std::string>& getAnnotationColumnHeaders() const
00080         {
00081             return annotationColumnHeaders_;
00082         }
00083 
00086         size_t getAnnotationIndex(const std::string& name) const;
00087 
00088         void fillOlplFileMetadata(util::DelimitedFile::Metadata& meta) const;
00089         void writeOlplFileHeader(std::ostream& out) const;
00090     private:
00092         struct LocusData
00093         {
00094             uint32_t locus_;
00095             uint16_t ploidy_;
00096             reference::Range range_;
00097             std::string zygosity_;
00098             std::string varType_;
00099             std::string reference_;
00100             boost::array<std::string, 2> alleleSeq_;
00101             boost::array<uint32_t, 2> alleleScore_;
00102             boost::array<std::string, 2> hapLink_;
00103             std::vector<std::string> extras_;
00104         };
00105 
00106         void readPending();
00107         void readLocus();
00108         void readMultilineLocus();
00109         void readOlplLocus();
00110         void fillOlplLocus();
00111         void detectFileType();
00112         void addOlplParsers();
00113         void createNoCallLocus(const reference::Range& r);
00114         bool isPureNoCall(const LocusData& d);
00115         void setupXrefAnnotation();
00116         void fillXrefAnnotation();
00117 
00118         const CrrFile* crr_;
00119         boost::shared_ptr<std::istream> istream_;
00120         boost::shared_ptr<util::DelimitedFile> df_;
00121         std::string name_;
00122         Locus locus_;
00123         Call pendingCall_;
00124         LocusData pendingLocusLine_;
00125         bool emptyFile_;
00126         bool eof_;
00127         bool hasPending_;
00128         bool referenceCoverValidation_;
00129         boost::uint64_t headerEnd_;
00130         boost::uint64_t fileEnd_;
00131 
00132         bool oneLinePerLocus_;
00133         std::vector<std::string> annotationColumnHeaders_;
00134         size_t xRefOffset_;
00135     };
00136 
00137 } } 
00138 
00139 #endif // CGATOOLS_VARIANTS_VARIANTFILEITERATOR_HPP_