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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
import os
from os.path import getmtime
from pyfaidx import Faidx, FastaIndexingError, IndexNotFoundError, FastaNotFoundError
from nose.tools import raises
from nose.plugins.skip import Skip, SkipTest
from unittest import TestCase
from tempfile import NamedTemporaryFile, mkdtemp
import time
import platform
import shutil
try:
from unittest import mock
except ImportError:
import mock
import six.moves.builtins as builtins
path = os.path.dirname(__file__)
os.chdir(path)
class TestIndexing(TestCase):
def setUp(self):
pass
def tearDown(self):
try:
os.remove('data/genes.fasta.fai')
except EnvironmentError:
pass # some tests may delete this file
def test_build(self):
expect_index = ("gi|563317589|dbj|AB821309.1| 3510 114 70 71\n"
"gi|557361099|gb|KF435150.1| 481 3789 70 71\n"
"gi|557361097|gb|KF435149.1| 642 4368 70 71\n"
"gi|543583796|ref|NR_104216.1| 4573 5141 70 71\n"
"gi|543583795|ref|NR_104215.1| 5317 9901 70 71\n"
"gi|543583794|ref|NR_104212.1| 5374 15415 70 71\n"
"gi|543583788|ref|NM_001282545.1| 4170 20980 70 71\n"
"gi|543583786|ref|NM_001282543.1| 5466 25324 70 71\n"
"gi|543583785|ref|NM_000465.3| 5523 30980 70 71\n"
"gi|543583740|ref|NM_001282549.1| 3984 36696 70 71\n"
"gi|543583738|ref|NM_001282548.1| 4113 40851 70 71\n"
"gi|530384540|ref|XM_005249645.1| 2752 45151 70 71\n"
"gi|530384538|ref|XM_005249644.1| 3004 48071 70 71\n"
"gi|530384536|ref|XM_005249643.1| 3109 51246 70 71\n"
"gi|530384534|ref|XM_005249642.1| 3097 54528 70 71\n"
"gi|530373237|ref|XM_005265508.1| 2794 57830 70 71\n"
"gi|530373235|ref|XM_005265507.1| 2848 60824 70 71\n"
"gi|530364726|ref|XR_241081.1| 1009 63849 70 71\n"
"gi|530364725|ref|XR_241080.1| 4884 65009 70 71\n"
"gi|530364724|ref|XR_241079.1| 2819 70099 70 71\n")
index_file = Faidx('data/genes.fasta').indexname
result_index = open(index_file).read()
assert result_index == expect_index
def test_build_issue_141(self):
expect_index = ("gi|563317589|dbj|AB821309.1| 3510 115 70 72\n"
"gi|557361099|gb|KF435150.1| 481 3842 70 72\n"
"gi|557361097|gb|KF435149.1| 642 4429 70 72\n"
"gi|543583796|ref|NR_104216.1| 4573 5213 70 72\n"
"gi|543583795|ref|NR_104215.1| 5317 10040 70 72\n"
"gi|543583794|ref|NR_104212.1| 5374 15631 70 72\n"
"gi|543583788|ref|NM_001282545.1| 4170 21274 70 72\n"
"gi|543583786|ref|NM_001282543.1| 5466 25679 70 72\n"
"gi|543583785|ref|NM_000465.3| 5523 31415 70 72\n"
"gi|543583740|ref|NM_001282549.1| 3984 37211 70 72\n"
"gi|543583738|ref|NM_001282548.1| 4113 41424 70 72\n"
"gi|530384540|ref|XM_005249645.1| 2752 45784 70 72\n"
"gi|530384538|ref|XM_005249644.1| 3004 48745 70 72\n"
"gi|530384536|ref|XM_005249643.1| 3109 51964 70 72\n"
"gi|530384534|ref|XM_005249642.1| 3097 55292 70 72\n"
"gi|530373237|ref|XM_005265508.1| 2794 58640 70 72\n"
"gi|530373235|ref|XM_005265507.1| 2848 61675 70 72\n"
"gi|530364726|ref|XR_241081.1| 1009 64742 70 72\n"
"gi|530364725|ref|XR_241080.1| 4884 65918 70 72\n"
"gi|530364724|ref|XR_241079.1| 2819 71079 70 72\n")
index_file = Faidx('data/issue_141.fasta').indexname
result_index = open(index_file).read()
os.remove('data/issue_141.fasta.fai')
print(result_index)
assert result_index == expect_index
def test_build_issue_111(self):
expect_index = ("gi|563317589|dbj|AB821309 3510 114 70 71\n"
"gi|557361099|gb|KF435150 481 3789 70 71\n"
"gi|557361097|gb|KF435149 642 4368 70 71\n"
"gi|543583796|ref|NR_104216 4573 5141 70 71\n"
"gi|543583795|ref|NR_104215 5317 9901 70 71\n"
"gi|543583794|ref|NR_104212 5374 15415 70 71\n"
"gi|543583788|ref|NM_001282545 4170 20980 70 71\n"
"gi|543583786|ref|NM_001282543 5466 25324 70 71\n"
"gi|543583785|ref|NM_000465 5523 30980 70 71\n"
"gi|543583740|ref|NM_001282549 3984 36696 70 71\n"
"gi|543583738|ref|NM_001282548 4113 40851 70 71\n"
"gi|530384540|ref|XM_005249645 2752 45151 70 71\n"
"gi|530384538|ref|XM_005249644 3004 48071 70 71\n"
"gi|530384536|ref|XM_005249643 3109 51246 70 71\n"
"gi|530384534|ref|XM_005249642 3097 54528 70 71\n"
"gi|530373237|ref|XM_005265508 2794 57830 70 71\n"
"gi|530373235|ref|XM_005265507 2848 60824 70 71\n"
"gi|530364726|ref|XR_241081 1009 63849 70 71\n"
"gi|530364725|ref|XR_241080 4884 65009 70 71\n"
"gi|530364724|ref|XR_241079 2819 70099 70 71\n")
index = Faidx(
'data/genes.fasta',
read_long_names=True,
key_function=lambda x: x.split('.')[0])
result_index = ''.join(index._index_as_string())
assert result_index == expect_index
def test_order(self):
order = ("gi|563317589|dbj|AB821309.1|", "gi|557361099|gb|KF435150.1|",
"gi|557361097|gb|KF435149.1|",
"gi|543583796|ref|NR_104216.1|",
"gi|543583795|ref|NR_104215.1|",
"gi|543583794|ref|NR_104212.1|",
"gi|543583788|ref|NM_001282545.1|",
"gi|543583786|ref|NM_001282543.1|",
"gi|543583785|ref|NM_000465.3|",
"gi|543583740|ref|NM_001282549.1|",
"gi|543583738|ref|NM_001282548.1|",
"gi|530384540|ref|XM_005249645.1|",
"gi|530384538|ref|XM_005249644.1|",
"gi|530384536|ref|XM_005249643.1|",
"gi|530384534|ref|XM_005249642.1|",
"gi|530373237|ref|XM_005265508.1|",
"gi|530373235|ref|XM_005265507.1|",
"gi|530364726|ref|XR_241081.1|",
"gi|530364725|ref|XR_241080.1|",
"gi|530364724|ref|XR_241079.1|")
result = tuple(Faidx('data/genes.fasta').index.keys())
assert result == order
def test_valgrind_short_lines(self):
""" Makes all full-length lines short and checks that error is raised
in all appropriate circumstances.
"""
# http://stackoverflow.com/a/23212515/717419
if platform.system() == 'Windows':
raise SkipTest
indexed = []
with open('data/genes.fasta') as genes:
fasta = genes.readlines()
n_lines = sum(1 for line in fasta)
for n in range(n_lines):
with NamedTemporaryFile(mode='w') as lines:
for i, line in enumerate(fasta):
if i == n and line[0] != '>' and len(line) == 71:
line = line[:-3] + '\n'
full_line = True
elif i == n:
full_line = False
lines.write(line)
lines.flush()
name = lines.name
if full_line:
try:
Faidx(name)
indexed.append(True)
except FastaIndexingError:
indexed.append(False)
assert not any(indexed)
def test_valgrind_long_lines(self):
""" Makes all full-length lines long and checks that error is raised
in all appropriate circumstances.
"""
# http://stackoverflow.com/a/23212515/717419
if platform.system() == 'Windows':
raise SkipTest
indexed = []
with open('data/genes.fasta') as genes:
fasta = genes.readlines()
n_lines = sum(1 for line in fasta)
for n in range(n_lines):
with NamedTemporaryFile(mode='w') as lines:
for i, line in enumerate(fasta):
if i == n and line[0] != '>' and len(line) == 71:
line = line.rstrip('\n') + 'NNN' + '\n'
full_line = True
elif i == n:
full_line = False
lines.write(line)
lines.flush()
name = lines.name
if full_line:
try:
Faidx(name)
indexed.append(True)
except FastaIndexingError:
indexed.append(False)
assert not any(indexed)
def test_valgrind_blank_lines(self):
""" Makes all full-length lines blank and checks that error is raised
in all appropriate circumstances.
"""
# http://stackoverflow.com/a/23212515/717419
if platform.system() == 'Windows':
raise SkipTest
indexed = []
with open('data/genes.fasta') as genes:
fasta = genes.readlines()
n_lines = sum(1 for line in fasta)
for n in range(n_lines):
with NamedTemporaryFile(mode='w') as lines:
for i, line in enumerate(fasta):
if i == n and line[0] != '>' and len(line) == 71:
line = '\n'
full_line = True
elif i == n:
full_line = False
lines.write(line)
lines.flush()
name = lines.name
if full_line:
try:
Faidx(name)
indexed.append(True)
except FastaIndexingError:
indexed.append(False)
assert not any(indexed)
def test_reindex_on_modification(self):
""" This test ensures that the index is regenerated when the FASTA
modification time is newer than the index modification time.
mdshw5/pyfaidx#50 """
faidx = Faidx('data/genes.fasta')
index_mtime = getmtime(faidx.indexname)
faidx.close()
os.utime('data/genes.fasta', (index_mtime + 10, ) * 2)
time.sleep(2)
faidx = Faidx('data/genes.fasta')
assert getmtime(faidx.indexname) > index_mtime
def test_build_issue_83(self):
""" Ensure that blank lines between entries are treated in the
same way as samtools 1.2. See mdshw5/pyfaidx#83.
"""
expect_index = ("MT 119 4 70 71\nGL000207.1 60 187 60 61\n")
index_file = Faidx('data/issue_83.fasta').indexname
result_index = open(index_file).read()
os.remove('data/issue_83.fasta.fai')
assert result_index == expect_index
def test_build_issue_96_fail_build_faidx(self):
""" Ensure that the fasta file is closed if construction of the 'Faidx' file
when attempting to build an index.
See mdshw5/pyfaidx#96
"""
tmp_dir = mkdtemp()
try:
fasta_path = os.path.join(tmp_dir, 'issue_96.fasta')
# Write simple fasta file with inconsistent sequence line lengths,
# so building an index raises a 'FastaIndexingError'
with open(fasta_path, 'w') as fasta_out:
fasta_out.write(
">seq1\nCTCCGGGCCCAT\nAACACTTGGGGGTAGCTAAAGTGAA\nATAAAGCCTAAA\n"
)
builtins_open = builtins.open
opened_files = []
def test_open(*args, **kwargs):
f = builtins_open(*args, **kwargs)
opened_files.append(f)
return f
with mock.patch('six.moves.builtins.open', side_effect=test_open):
try:
Faidx(fasta_path)
self.assertFail(
"Faidx construction should fail with 'FastaIndexingError'."
)
except FastaIndexingError:
pass
self.assertTrue(all(f.closed for f in opened_files))
finally:
shutil.rmtree(tmp_dir)
def test_build_issue_96_fail_read_malformed_index_duplicate_key(self):
""" Ensure that the fasta file is closed if construction of the 'Faidx' file
fails when attempting to read a pre-existing index. The index is malformed because
it contains mulitple occurrences of the same index.
See mdshw5/pyfaidx#96
"""
tmp_dir = mkdtemp()
try:
fasta_path = os.path.join(tmp_dir, 'issue_96.fasta')
faidx_path = os.path.join(tmp_dir, 'issue_96.fasta.fai')
# Write simple fasta file
with open(fasta_path, 'w') as fasta_out:
fasta_out.write(">seq1\nCTCCGGGCCCAT\nATAAAGCCTAAA\n")
with open(faidx_path, 'w') as faidx_out:
faidx_out.write("seq1\t24\t6\t12\t13\nseq1\t24\t6\t12\t13\n")
builtins_open = builtins.open
opened_files = []
def test_open(*args, **kwargs):
f = builtins_open(*args, **kwargs)
opened_files.append(f)
return f
with mock.patch('six.moves.builtins.open', side_effect=test_open):
try:
Faidx(fasta_path)
self.assertFail(
"Faidx construction should fail with 'ValueError'.")
except ValueError:
pass
self.assertTrue(all(f.closed for f in opened_files))
finally:
shutil.rmtree(tmp_dir)
def test_read_back_index(self):
"""Ensure that index files written with write_fai() can be read back"""
import locale
old_locale = locale.getlocale(locale.LC_NUMERIC)
try:
locale.setlocale(locale.LC_NUMERIC, 'C')
faidx = Faidx('data/genes.fasta')
faidx.write_fai()
faidx = Faidx('data/genes.fasta', build_index=False)
finally:
locale.setlocale(locale.LC_NUMERIC, old_locale)
@raises(IndexNotFoundError)
def test_issue_134_no_build_index(self):
""" Ensure that index file is not built when build_index=False. See mdshw5/pyfaidx#134.
"""
faidx = Faidx('data/genes.fasta', build_index=False)
@raises(FastaIndexingError)
def test_issue_144_no_defline(self):
""" Ensure that an exception is raised when a file contains no deflines. See mdshw5/pyfaidx#144.
"""
tmp_dir = mkdtemp()
try:
fasta_path = os.path.join(tmp_dir, 'issue_144.fasta')
# Write simple fasta file
with open(fasta_path, 'w') as fasta_out:
fasta_out.write("CTCCGGGCCCAT\nATAAAGCCTAAA\n")
faidx = Faidx(fasta_path)
finally:
shutil.rmtree(tmp_dir)
|