|
8 | 8 | - =, hashable |
9 | 9 | - |
10 | 10 | # VCF |
11 | | -- from cyvcf2 |
| 11 | +- from cyvcf2 |
12 | 12 |
|
13 | 13 | # Interval |
14 | 14 | - validation of the interval |
15 | 15 | - access to all the attributes |
16 | 16 | - from_pybedtools and to_pybedtools |
17 | 17 | - shift, swapt_strand, trim, etc |
18 | 18 | """ |
19 | | - |
20 | | - |
21 | 19 | from kipoiseq.dataclasses import Variant, Interval |
| 20 | +import pytest |
| 21 | +import cyvcf2 |
| 22 | +import pybedtools |
| 23 | + |
22 | 24 |
|
23 | 25 | def test_variant(): |
24 | 26 | v = Variant("chr1", 10, 'C', 'T') |
25 | 27 |
|
26 | | - # |
27 | | - pass |
| 28 | + assert v.start == 9 |
| 29 | + assert v.chrom == 'chr1' |
| 30 | + assert v.pos == 10 |
| 31 | + assert v.ref == 'C' |
| 32 | + assert v.alt == 'T' |
| 33 | + assert isinstance(v.info, dict) |
| 34 | + assert len(v.info) == 0 |
| 35 | + assert v.qual == 0 |
| 36 | + assert v.filter == 'PASS' |
| 37 | + v.info['test'] = 10 |
| 38 | + assert v.info['test'] == 10 |
| 39 | + assert isinstance(str(v), str) |
| 40 | + |
| 41 | + # make sure the original got unchangd |
| 42 | + v2 = v.copy() |
| 43 | + v.info['test'] = 20 |
| 44 | + assert v2.info['test'] == 10 |
| 45 | + v.__repr__() |
| 46 | + |
| 47 | + # __str__, from_str |
| 48 | + assert v == Variant.from_str(str(v)) |
| 49 | + |
| 50 | + # hash test |
| 51 | + assert isinstance(hash(v), int) |
| 52 | + assert hash(v) == hash(Variant.from_str(str(v))) |
| 53 | + |
| 54 | + # fixed arguments |
| 55 | + with pytest.raises(AttributeError): |
| 56 | + v.chrom = 'asd' |
| 57 | + with pytest.raises(AttributeError): |
| 58 | + v.pos = 10 |
| 59 | + with pytest.raises(AttributeError): |
| 60 | + v.ref = 'asd' |
| 61 | + with pytest.raises(AttributeError): |
| 62 | + v.alt = 'asd' |
| 63 | + |
| 64 | + # non-fixed arguments |
| 65 | + v.id = 'asd' |
| 66 | + v.qual = 10 |
| 67 | + v.filter = 'asd' |
| 68 | + v.source = 2 |
| 69 | + |
| 70 | + assert isinstance(Variant("chr1", '10', 'C', 'T').pos, int) |
| 71 | + |
| 72 | + # from cyvcf2 |
| 73 | + vcf = cyvcf2.VCF('tests/data/test.vcf.gz') |
| 74 | + cv = list(vcf)[0] |
| 75 | + |
| 76 | + v2 = Variant.from_cyvcf(cv) |
| 77 | + assert isinstance(v2.source, cyvcf2.Variant) |
| 78 | + |
| 79 | + |
| 80 | +def test_interval(): |
| 81 | + interval = Interval("chr1", 10, 20, strand='-') |
| 82 | + interval.__repr__() |
| 83 | + |
| 84 | + assert interval.start == 10 |
| 85 | + assert interval.end == 20 |
| 86 | + assert interval.chrom == 'chr1' |
| 87 | + assert interval.name == '' |
| 88 | + assert isinstance(interval.attrs, dict) |
| 89 | + assert len(interval.attrs) == 0 |
| 90 | + interval.attrs['test'] = 10 |
| 91 | + assert interval.attrs['test'] == 10 |
| 92 | + assert isinstance(str(interval), str) |
| 93 | + assert interval.neg_strand |
| 94 | + |
| 95 | + assert interval.width() == 10 |
| 96 | + assert len(interval) == 10 |
| 97 | + |
| 98 | + # __str__, from_str |
| 99 | + assert interval == Interval.from_str(str(interval)) |
| 100 | + |
| 101 | + # make sure the original got unchangd |
| 102 | + i2 = interval.copy() |
| 103 | + interval.attrs['test'] = 20 |
| 104 | + assert i2.attrs['test'] == 10 |
| 105 | + |
| 106 | + # hash test |
| 107 | + assert isinstance(hash(interval), int) |
| 108 | + assert hash(interval) == hash(Interval.from_str(str(interval))) |
| 109 | + |
| 110 | + # fixed arguments |
| 111 | + with pytest.raises(AttributeError): |
| 112 | + interval.chrom = 'asd' |
| 113 | + with pytest.raises(AttributeError): |
| 114 | + interval.start = 10 |
| 115 | + with pytest.raises(AttributeError): |
| 116 | + interval.end = 300 |
| 117 | + with pytest.raises(AttributeError): |
| 118 | + interval.strand = '+' |
| 119 | + assert interval.strand == '-' |
| 120 | + |
| 121 | + # non-fixed arguments |
| 122 | + interval.name = 'asd' |
| 123 | + interval.score = 10 |
| 124 | + |
| 125 | + assert interval == Interval.from_pybedtools(interval.to_pybedtools()) |
| 126 | + assert isinstance(interval.to_pybedtools(), pybedtools.Interval) |
| 127 | + |
| 128 | + i2 = interval.shift(10, use_strand=False) |
| 129 | + |
| 130 | + # original unchanged |
| 131 | + assert interval.start == 10 |
| 132 | + assert interval.end == 20 |
| 133 | + |
| 134 | + assert i2.start == 20 |
| 135 | + assert i2.end == 30 |
| 136 | + |
| 137 | + i2 = interval.shift(10) # use_strand = True by default |
| 138 | + assert i2.start == 0 |
| 139 | + assert i2.end == 10 |
| 140 | + |
| 141 | + assert not interval.shift(20, use_strand=True).is_valid() |
| 142 | + |
| 143 | + i2 = interval.shift(15, use_strand=True).truncate() |
| 144 | + assert i2.start == 0 |
| 145 | + assert i2.end == 5 |
| 146 | + |
| 147 | + assert interval.center() == 15 |
| 148 | + |
| 149 | + # resize |
| 150 | + i2 = interval.resize(11) |
| 151 | + assert i2.start == 10 and i2.end == 21 |
| 152 | + |
| 153 | + i2 = interval.resize(12) |
| 154 | + assert i2.start == 9 and i2.end == 21 |
| 155 | + |
| 156 | + i2 = interval.resize(9) |
| 157 | + assert i2.start == 11 and i2.end == 20 |
| 158 | + |
| 159 | + i2 = interval.swap_strand() |
| 160 | + assert interval.strand == "-" |
| 161 | + assert i2.strand == "+" |
| 162 | + assert i2.strand == '+' |
| 163 | + assert len(i2) == 10 |
| 164 | + i2 = i2.resize(11) |
| 165 | + assert i2.start == 9 and i2.end == 20 |
| 166 | + |
| 167 | + i2 = interval.copy() |
| 168 | + assert i2.center(use_strand=True) == 15 |
| 169 | + assert i2.center(use_strand=False) == 15 |
| 170 | + |
| 171 | + i2 = interval.swap_strand() |
| 172 | + assert i2.strand == "+" |
| 173 | + |
| 174 | + i3 = i2.resize(11).shift(1) |
| 175 | + assert i3.center(use_strand=True) == 16 |
| 176 | + assert i3.center(use_strand=False) == 15 |
| 177 | + |
| 178 | + i3 = i2.resize(11).shift(1) |
| 179 | + assert i3.center(use_strand=True) == 16 |
| 180 | + assert i3.center(use_strand=False) == 15 |
| 181 | + |
| 182 | + i2 = interval.trim(1, 10) |
| 183 | + assert i2.start == 10 and i2.end == 19 |
| 184 | + |
| 185 | + i2 = interval.trim(1, 10, use_strand=False) |
| 186 | + assert i2.start == 11 and i2.end == 20 |
0 commit comments