Commit patch to not break on spaces.
[bowtie.git] / edit.h
1 /*
2  * edit.h
3  *
4  *  Created on: Jul 31, 2009
5  *      Author: Ben Langmead
6  */
7
8 #ifndef EDIT_H_
9 #define EDIT_H_
10
11 #include <iostream>
12 #include <stdint.h>
13 #include "assert_helpers.h"
14 #include "filebuf.h"
15
16 /**
17  * 3 types of edits; mismatch (substitution), insertion in the
18  * reference, deletion in the reference.
19  */
20 enum {
21         EDIT_TYPE_MM = 1,
22         EDIT_TYPE_SNP,
23         EDIT_TYPE_INS,
24         EDIT_TYPE_DEL
25 };
26
27 /**
28  * Encapsulates an edit between the read sequence and the reference
29  * sequence.
30  */
31 struct Edit {
32
33         Edit() : pos(1023) { }
34
35         Edit(int po, int ch, int ty = EDIT_TYPE_MM) :
36                 chr(ch), qchr(0), type(ty), pos(po) { }
37
38         /**
39          * Write Edit to an OutFileBuf.
40          */
41         void serialize(OutFileBuf& fb) const {
42                 assert_eq(4, sizeof(Edit));
43                 fb.writeChars((const char*)this, 4);
44         }
45
46         /**
47          * Read Edit from a FileBuf.
48          */
49         void deserialize(FileBuf& fb) {
50                 fb.get((char*)this, 4);
51         }
52
53         /**
54          * Edit less-than overload.
55          */
56         int operator< (const Edit &rhs) const {
57                 if(pos < rhs.pos) return 1;
58                 if(pos > rhs.pos) return 0;
59                 if(chr < rhs.chr) return 1;
60                 if(chr > rhs.chr) return 0;
61                 return (qchr < rhs.qchr)? 1 : 0;
62         }
63
64         /**
65          * Edit equals overload.
66          */
67         int operator== (const Edit &rhs) const {
68                 return(pos  == rhs.pos &&
69                            chr  == rhs.chr &&
70                            qchr == rhs.qchr &&
71                            type == rhs.type);
72         }
73
74         /**
75          * Return true iff this Edit is initialized.
76          */
77         bool initialized() const {
78                 return pos != 1023;
79         }
80
81         uint32_t chr       :  8; // reference character involved (for subst and ins)
82         uint32_t qchr      :  8; // read character involved (for subst and del
83         uint32_t type      :  4; // 1 -> mm, 2 -> SNP, 3 -> ins, 4 -> del
84         uint32_t pos       : 10; // position w/r/t search root
85         uint32_t reserved  :  2; // padding
86
87         friend std::ostream& operator<< (std::ostream& os, const Edit& e);
88 };
89
90 #endif /* EDIT_H_ */