yat/omic/VcfHeader.h

Code
Comments
Other
Rev Date Author Line
3750 17 Oct 18 peter 1 #ifndef theplu_yat_omic_vcf_header
3750 17 Oct 18 peter 2 #define theplu_yat_omic_vcf_header
3750 17 Oct 18 peter 3
3750 17 Oct 18 peter 4 // $Id$
3750 17 Oct 18 peter 5
3750 17 Oct 18 peter 6 /*
3999 08 Oct 20 peter 7   Copyright (C) 2018, 2019, 2020 Peter Johansson
3750 17 Oct 18 peter 8
3750 17 Oct 18 peter 9   The yat library is free software; you can redistribute it and/or
3750 17 Oct 18 peter 10   modify it under the terms of the GNU General Public License as
3750 17 Oct 18 peter 11   published by the Free Software Foundation; either version 3 of the
3750 17 Oct 18 peter 12   License, or (at your option) any later version.
3750 17 Oct 18 peter 13
3750 17 Oct 18 peter 14   The yat library is distributed in the hope that it will be useful,
3750 17 Oct 18 peter 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
3750 17 Oct 18 peter 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3750 17 Oct 18 peter 17   General Public License for more details.
3750 17 Oct 18 peter 18
3750 17 Oct 18 peter 19   You should have received a copy of the GNU General Public License
3754 17 Oct 18 peter 20   along with yat. If not, see <http://www.gnu.org/licenses/>.
3750 17 Oct 18 peter 21 */
3750 17 Oct 18 peter 22
3750 17 Oct 18 peter 23 #include <iosfwd>
3750 17 Oct 18 peter 24 #include <string>
3750 17 Oct 18 peter 25 #include <vector>
3750 17 Oct 18 peter 26
3750 17 Oct 18 peter 27 namespace theplu {
3750 17 Oct 18 peter 28 namespace yat {
3750 17 Oct 18 peter 29 namespace omic {
3750 17 Oct 18 peter 30
3750 17 Oct 18 peter 31   /**
3750 17 Oct 18 peter 32      \since New in yat 0.16
3750 17 Oct 18 peter 33    */
3750 17 Oct 18 peter 34   class VcfHeader
3750 17 Oct 18 peter 35   {
3750 17 Oct 18 peter 36   public:
3750 17 Oct 18 peter 37     /**
3750 17 Oct 18 peter 38        \brief Constructor
3750 17 Oct 18 peter 39
3750 17 Oct 18 peter 40        Creates an empty header
3750 17 Oct 18 peter 41      */
3750 17 Oct 18 peter 42     VcfHeader(const std::string& version);
3750 17 Oct 18 peter 43
3750 17 Oct 18 peter 44     /**
3750 17 Oct 18 peter 45        Consumes lines until a line does not start with '#' and
3750 17 Oct 18 peter 46        constructs a VcfHeader.
3750 17 Oct 18 peter 47      */
3750 17 Oct 18 peter 48     VcfHeader(std::istream& is);
3750 17 Oct 18 peter 49
3750 17 Oct 18 peter 50     /**
3750 17 Oct 18 peter 51        Adds a line ##\a key = \a value
3750 17 Oct 18 peter 52      */
3750 17 Oct 18 peter 53     void add(const std::string& key, const std::string& value);
3750 17 Oct 18 peter 54
3750 17 Oct 18 peter 55     /**
3750 17 Oct 18 peter 56        Add a line
3750 17 Oct 18 peter 57        ##key=<ID,Number=number,Type=type,Description=description>
3750 17 Oct 18 peter 58
3750 17 Oct 18 peter 59        If number is 0, it's replaced with ".".
3750 17 Oct 18 peter 60      */
3750 17 Oct 18 peter 61     void add(const std::string& key, const std::string& ID, int number,
3750 17 Oct 18 peter 62              const std::string& type, const std::string& description);
3750 17 Oct 18 peter 63
3750 17 Oct 18 peter 64     /**
3750 17 Oct 18 peter 65        Same as add("INFO", ID, number, type, description)
3750 17 Oct 18 peter 66
3809 05 Jul 19 peter 67        \a number should be integer or special character as
3750 17 Oct 18 peter 68        A: one per alt allele
3750 17 Oct 18 peter 69        R: one per allele
3750 17 Oct 18 peter 70        G: one per genotype
3750 17 Oct 18 peter 71        .: unknown
3750 17 Oct 18 peter 72      */
3750 17 Oct 18 peter 73     void add_info(const std::string& ID, const std::string& number,
3750 17 Oct 18 peter 74                   const std::string& type, const std::string& description);
3750 17 Oct 18 peter 75
3750 17 Oct 18 peter 76     /**
3750 17 Oct 18 peter 77        Same as add("FORMAT", ID, number, type, description)
3809 05 Jul 19 peter 78
3809 05 Jul 19 peter 79        \a number should be integer or special character as
3809 05 Jul 19 peter 80        A: one per alt allele
3809 05 Jul 19 peter 81        R: one per allele
3809 05 Jul 19 peter 82        G: one per genotype
3809 05 Jul 19 peter 83        .: unknown
3750 17 Oct 18 peter 84      */
3750 17 Oct 18 peter 85     void add_format(const std::string& ID, const std::string& number,
3750 17 Oct 18 peter 86                     const std::string& type, const std::string& description);
3750 17 Oct 18 peter 87
3750 17 Oct 18 peter 88     /**
3750 17 Oct 18 peter 89        add a line with something like:
3750 17 Oct 18 peter 90        ##bcftools_viewCommand=view -l 9 -O z -; Date=Wed Apr 19 10:49:09 2017
3750 17 Oct 18 peter 91     */
3750 17 Oct 18 peter 92     void add_program_command(int argc, char* argv[]);
3750 17 Oct 18 peter 93
3750 17 Oct 18 peter 94     /**
3750 17 Oct 18 peter 95        add a line with something like
3750 17 Oct 18 peter 96        ##bcftools_normVersion=1.4+htslib-1.4
3750 17 Oct 18 peter 97     */
3750 17 Oct 18 peter 98     void add_program_version(const std::string& prog,
3750 17 Oct 18 peter 99                              const std::string& version);
3750 17 Oct 18 peter 100
3750 17 Oct 18 peter 101     /**
3891 26 Mar 20 peter 102        \brief erase line \a line
3891 26 Mar 20 peter 103
3891 26 Mar 20 peter 104        The last line, starting with '\#CHROM', cannot be erased. Use
3891 26 Mar 20 peter 105        function samples(const std::vector<std::string>&) to alter that
3891 26 Mar 20 peter 106        line.
3905 08 May 20 peter 107
3905 08 May 20 peter 108        \since New in yat 0.18
3891 26 Mar 20 peter 109     */
3891 26 Mar 20 peter 110     void erase(size_t line);
3891 26 Mar 20 peter 111
3891 26 Mar 20 peter 112     /**
3750 17 Oct 18 peter 113        \return the last line of the header
3750 17 Oct 18 peter 114      */
3750 17 Oct 18 peter 115     const std::string& header(void) const;
3750 17 Oct 18 peter 116
3750 17 Oct 18 peter 117     /**
3750 17 Oct 18 peter 118        Set the last line of the header
3750 17 Oct 18 peter 119      */
3750 17 Oct 18 peter 120     void header(const std::string& header);
3750 17 Oct 18 peter 121
3750 17 Oct 18 peter 122     /**
3750 17 Oct 18 peter 123        \return line \a i
3750 17 Oct 18 peter 124     */
3750 17 Oct 18 peter 125     const std::string& line(size_t i) const;
3750 17 Oct 18 peter 126
3750 17 Oct 18 peter 127     /**
3750 17 Oct 18 peter 128        \return everything between prefix '##' and first '=' of line \a line
3750 17 Oct 18 peter 129     */
3750 17 Oct 18 peter 130     std::string key(size_t line) const;
3750 17 Oct 18 peter 131
3750 17 Oct 18 peter 132     /**
3750 17 Oct 18 peter 133        Sort lines
3891 26 Mar 20 peter 134
3891 26 Mar 20 peter 135        Lines are sorted with respect to their categories
3891 26 Mar 20 peter 136        a) \c '\#\#fileformat='
3891 26 Mar 20 peter 137        b) \c '\#\#reference=' and \c '\#\#contig='
3891 26 Mar 20 peter 138        c) \c '\#\#INFO='
3891 26 Mar 20 peter 139        d) \c '\#\#FILTER='
3891 26 Mar 20 peter 140        e) \c '\#\#FORMAT='
3891 26 Mar 20 peter 141        f) other
3891 26 Mar 20 peter 142        g) \c '\#CHROM'
3750 17 Oct 18 peter 143     */
3750 17 Oct 18 peter 144     void nicify(void);
3750 17 Oct 18 peter 145
3750 17 Oct 18 peter 146     /**
3750 17 Oct 18 peter 147        everything right of first '=' on line \a line
3750 17 Oct 18 peter 148     */
3750 17 Oct 18 peter 149     std::string value(size_t line) const;
3750 17 Oct 18 peter 150
3750 17 Oct 18 peter 151     /**
3750 17 Oct 18 peter 152        returns "" if \a key is missing is false otherwise
3750 17 Oct 18 peter 153        corresponding value e.g.  value(i, "Description") returns value
3750 17 Oct 18 peter 154        for key Description of line \a line
3750 17 Oct 18 peter 155     */
3750 17 Oct 18 peter 156     std::string value(size_t line, const std::string& key) const;
3750 17 Oct 18 peter 157
3750 17 Oct 18 peter 158     /**
3750 17 Oct 18 peter 159        \return number of lines
3750 17 Oct 18 peter 160      */
3750 17 Oct 18 peter 161     size_t size(void) const;
3750 17 Oct 18 peter 162
3750 17 Oct 18 peter 163     /**
3750 17 Oct 18 peter 164        Sample names occur tab-delimited in the last line starting from
3750 17 Oct 18 peter 165        10th column.
3750 17 Oct 18 peter 166
3750 17 Oct 18 peter 167        \return samples name
3750 17 Oct 18 peter 168      */
3750 17 Oct 18 peter 169     const std::vector<std::string>& samples(void) const;
3750 17 Oct 18 peter 170
3750 17 Oct 18 peter 171     /**
3750 17 Oct 18 peter 172        Set sample names
3750 17 Oct 18 peter 173      */
3750 17 Oct 18 peter 174     void samples(const std::vector<std::string>& s);
3750 17 Oct 18 peter 175
3750 17 Oct 18 peter 176   private:
3750 17 Oct 18 peter 177     friend std::ostream& operator<<(std::ostream& os, const VcfHeader& b);
3750 17 Oct 18 peter 178     std::vector<std::string> lines_;
3750 17 Oct 18 peter 179     mutable std::vector<std::string> samples_;
3750 17 Oct 18 peter 180
3750 17 Oct 18 peter 181     void add(const std::string& key,
3750 17 Oct 18 peter 182              const std::string& ID, const std::string& number,
3750 17 Oct 18 peter 183              const std::string& type, const std::string& description);
3750 17 Oct 18 peter 184     void key_value(size_t line, std::string& key, std::string& value) const;
3750 17 Oct 18 peter 185     // remove line with ##KEY=<ID=id*
3750 17 Oct 18 peter 186     void remove(const std::string& key, const std::string& id);
3760 18 Oct 18 peter 187
3760 18 Oct 18 peter 188     // functor used in nicify
3760 18 Oct 18 peter 189     struct Compare
3760 18 Oct 18 peter 190     {
3760 18 Oct 18 peter 191       bool operator()(const std::string& lhs, const std::string& rhs) const;
3760 18 Oct 18 peter 192     private:
3760 18 Oct 18 peter 193       int index(const std::string& s) const;
3760 18 Oct 18 peter 194       bool match(const std::string& s, const std::string& prefix) const;
3760 18 Oct 18 peter 195     };
3750 17 Oct 18 peter 196   };
3750 17 Oct 18 peter 197
3750 17 Oct 18 peter 198   /**
3750 17 Oct 18 peter 199      \since new in yat 0.16
3750 17 Oct 18 peter 200
3750 17 Oct 18 peter 201      \relates VcfHeader
3750 17 Oct 18 peter 202    */
3750 17 Oct 18 peter 203   std::ostream& operator<<(std::ostream&, const VcfHeader& header);
3750 17 Oct 18 peter 204
3750 17 Oct 18 peter 205 }}}
3750 17 Oct 18 peter 206
3750 17 Oct 18 peter 207 #endif