1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
import os
import pytest
from pyfaidx import Faidx, Fasta, FetchError
path = os.path.dirname(__file__)
os.chdir(path)
@pytest.fixture
def setup_zero():
with open('data/zero_length.fasta', 'w') as fasta:
fasta.write(""">A
ATCG
>B
>C
>D
GTA
GC""")
yield
os.remove('data/zero_length.fasta')
os.remove('data/zero_length.fasta.fai')
def test_index_zero_length(setup_zero):
fasta = Fasta('data/zero_length.fasta')
def test_fetch_zero_length(setup_zero):
fasta = Fasta('data/zero_length.fasta')
b = fasta["B"]
assert str(b) == ''
@pytest.fixture
def remove_index():
yield
try:
os.remove('data/genes.fasta.fai')
except EnvironmentError:
pass # some tests may delete this file
def test_as_raw_zero_length_subsequence(remove_index):
fasta = Fasta('data/genes.fasta', as_raw=True, strict_bounds=True)
expect = ''
result = fasta['gi|557361099|gb|KF435150.1|'][100:100]
assert result == expect
def test_zero_length_subsequence(remove_index):
fasta = Fasta('data/genes.fasta', strict_bounds=True)
expect = ''
result = fasta['gi|557361099|gb|KF435150.1|'][100:100]
assert result.seq == expect
def test_fetch_whole_entry(remove_index):
faidx = Faidx('data/genes.fasta')
expect = ('ATGACATCATTTTCCACCTCTGCTCAGTGTTCAACATCTGA'
'CAGTGCTTGCAGGATCTCTCCTGGACAAATCAATCAGGTACGACCA'
'AAACTGCCGCTTTTGAAGATTTTGCATGCAGCAGGTGCGCAAGG'
'TGAAATGTTCACTGTTAAAGAGGTCATGCACTATTTAGGTCAGTACAT'
'AATGGTGAAGCAACTTTATGATCAGCAGGAGCAGCATATGGTATATTG'
'TGGTGGAGATCTTTTGGGAGAACTACTGGGACGTCAGAGCTTCTCCGTG'
'AAAGACCCAAGCCCTCTCTATGATATGCTAAGAAAGAATCTTGTCACTTT'
'AGCCACTGCTACTACAGCAAAGTGCAGAGGAAAGTTCCACTTCCAGAAAAA'
'GAACTACAGAAGACGATATCCCCACACTGCCTACCTCAGAGCATAAATGCA'
'TACATTCTAGAGAAGGTGATTGAAGTGGGAAAAAATGATGACCTGGAGGACTC')
result = faidx.fetch('gi|557361099|gb|KF435150.1|',
1, 481)
assert str(result) == expect
def test_fetch_middle(remove_index):
faidx = Faidx('data/genes.fasta')
expect = 'TTGAAGATTTTGCATGCAGCAGGTGCGCAAGGTGAAATGTTCACTGTTAAA'
result = faidx.fetch('gi|557361099|gb|KF435150.1|',
100, 150)
assert str(result) == expect
def test_fetch_end(remove_index):
faidx = Faidx('data/genes.fasta')
expect = 'TC'
result = faidx.fetch('gi|557361099|gb|KF435150.1|',
480, 481)
assert str(result) == expect
def test_fetch_border(remove_index):
""" Fetch past the end of a gene entry """
faidx = Faidx('data/genes.fasta')
expect = 'TC'
result = faidx.fetch('gi|557361099|gb|KF435150.1|',
480, 500)
assert str(result) == expect
def test_rev(remove_index):
faidx = Faidx('data/genes.fasta')
expect = 'GA'
result = faidx.fetch('gi|557361099|gb|KF435150.1|',
480, 481)
assert str(-result) == expect, result
def test_fetch_past_bounds(remove_index):
""" Fetch past the end of a gene entry """
faidx = Faidx('data/genes.fasta', strict_bounds=True)
with pytest.raises(FetchError):
result = faidx.fetch('gi|557361099|gb|KF435150.1|', 480, 5000)
def test_fetch_negative(remove_index):
""" Fetch starting with a negative coordinate """
faidx = Faidx('data/genes.fasta', strict_bounds=True)
with pytest.raises(FetchError):
result = faidx.fetch('gi|557361099|gb|KF435150.1|', -10, 10)
def test_fetch_reversed_coordinates(remove_index):
""" Fetch starting with a negative coordinate """
faidx = Faidx('data/genes.fasta', strict_bounds=True)
with pytest.raises(FetchError):
result = faidx.fetch('gi|557361099|gb|KF435150.1|', 50, 10)
def test_fetch_keyerror(remove_index):
""" Fetch a key that does not exist """
faidx = Faidx('data/genes.fasta', strict_bounds=True)
with pytest.raises(FetchError):
result = faidx.fetch('gi|joe|gb|KF435150.1|', 1, 10)
def test_blank_string(remove_index):
""" seq[0:0] should return a blank string mdshw5/pyfaidx#53 """
fasta = Fasta('data/genes.fasta', as_raw=True)
assert fasta['gi|557361099|gb|KF435150.1|'][0:0] == ''
def test_slice_from_beginning(remove_index):
fasta = Fasta('data/genes.fasta', as_raw=True)
assert fasta['gi|557361099|gb|KF435150.1|'][:4] == 'ATGA'
def test_slice_from_end(remove_index):
fasta = Fasta('data/genes.fasta', as_raw=True)
assert fasta['gi|557361099|gb|KF435150.1|'][-4:] == 'ACTC'
def test_issue_74_start(remove_index):
f0 = Fasta('data/genes.fasta', one_based_attributes=False)
f1 = Fasta('data/genes.fasta', one_based_attributes=True)
assert f0['gi|557361099|gb|KF435150.1|'][0:90].start == f1['gi|557361099|gb|KF435150.1|'][0:90].start - 1
def test_issue_74_consistency(remove_index):
f0 = Fasta('data/genes.fasta', one_based_attributes=False)
f1 = Fasta('data/genes.fasta', one_based_attributes=True)
assert str(f0['gi|557361099|gb|KF435150.1|'][0:90]) == str(f1['gi|557361099|gb|KF435150.1|'][0:90])
def test_issue_74_end_faidx(remove_index):
f0 = Faidx('data/genes.fasta', one_based_attributes=False)
f1 = Faidx('data/genes.fasta', one_based_attributes=True)
end0 = f0.fetch('gi|557361099|gb|KF435150.1|', 1, 90).end
end1 = f1.fetch('gi|557361099|gb|KF435150.1|', 1, 90).end
assert end0 == end1
def test_issue_74_end_fasta(remove_index):
f0 = Fasta('data/genes.fasta', one_based_attributes=False)
f1 = Fasta('data/genes.fasta', one_based_attributes=True)
end0 = f0['gi|557361099|gb|KF435150.1|'][1:90].end
end1 = f1['gi|557361099|gb|KF435150.1|'][1:90].end
print((end0, end1))
assert end0 == end1
def test_issue_79_fix(remove_index):
f = Fasta('data/genes.fasta')
s = f['gi|557361099|gb|KF435150.1|'][100:105]
print((s.start, s.end))
assert (101, 105) == (s.start, s.end)
def test_issue_79_fix_negate(remove_index):
f = Fasta('data/genes.fasta')
s = f['gi|557361099|gb|KF435150.1|'][100:105]
s = -s
print((s.start, s.end))
assert (105, 101) == (s.start, s.end)
def test_issue_79_fix_one_based_false(remove_index):
f = Fasta('data/genes.fasta', one_based_attributes=False)
s = f['gi|557361099|gb|KF435150.1|'][100:105]
print((s.start, s.end))
assert (100, 105) == (s.start, s.end)
def test_issue_79_fix_one_based_false_negate(remove_index):
f = Fasta('data/genes.fasta', one_based_attributes=False)
s = f['gi|557361099|gb|KF435150.1|'][100:105]
print(s.__dict__)
s = -s
print(s.__dict__)
assert (105, 100) == (s.start, s.end)
|