File: test_fastx.py

package info (click to toggle)
pyfastx 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,336 kB
  • sloc: ansic: 4,826; python: 1,816; sh: 505; perl: 66; makefile: 31
file content (68 lines) | stat: -rw-r--r-- 1,864 bytes parent folder | download | duplicates (2)
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
import os
import pyfaidx
import pyfastx
import unittest

join = os.path.join
data_dir = join(os.path.dirname(os.path.abspath(__file__)), 'data')

gzip_fasta = join(data_dir, 'test.fa.gz')
gzip_fastq = join(data_dir, 'test.fq.gz')
flat_fasta = join(data_dir, 'test.fa')
flat_fastq = join(data_dir, 'test.fq')

class FastxTest(unittest.TestCase):
	def setUp(self):
		self.faidx = pyfaidx.Fasta(flat_fasta)

	def tearDown(self):
		del self.faidx

		if os.path.exists('{}.fai'.format(flat_fasta)):
			os.remove('{}.fai'.format(flat_fasta))

	def test_fasta_iter(self):
		for name, seq, comment in pyfastx.Fastx(gzip_fasta, comment=True):
			s = self.faidx[name]
			self.assertEqual(str(seq), seq)
			self.assertEqual(' '.join(s.long_name.split()[1:]), comment)

	def test_fasta_upper(self):
		for name, seq in pyfastx.Fastx(flat_fasta, uppercase=True):
			self.assertEqual(str(self.faidx[name]), seq)

	def test_fastq_iter(self):
		reads = {}
		with open(flat_fastq) as fh:
			for line in fh:
				line = line.strip()

				if line[0] == '@':
					name = line.split()[0][1:]
					comment = ' '.join(line.split()[1:])
					reads[name] = [comment]
				elif line == '+':
					continue
				else:
					reads[name].append(line)

		for name, seq, qual, comment in pyfastx.Fastx(gzip_fastq, "fastq", comment=True):
			r = reads[name]

			self.assertEqual(r[0], comment)
			self.assertEqual(r[1], seq)
			self.assertEqual(r[2], qual)

	def test_fastx_repr(self):
		fa = pyfastx.Fastx(gzip_fasta, "fasta")
		self.assertEqual(repr(fa), "<Fastx> fasta {}".format(gzip_fasta))

		fq = pyfastx.Fastx(gzip_fastq, "fastq")
		self.assertEqual(repr(fq), "<Fastx> fastq {}".format(gzip_fastq))

	def test_exception(self):
		with self.assertRaises(FileExistsError):
			_ = pyfastx.Fastx('test_file')

		with self.assertRaises(RuntimeError):
			_ = pyfastx.Fastx(gzip_fasta, format="fastx")