annotate tests/test_plot.py @ 2:b6fd86c539ea

Fix pysam version in riboplot (was 0.7.7 instead of 0.8.3). Fix move sys import to the top in ribocount.py, riboplot.py. Minor: * Include an empty conditional when no RNA coverage is required. * Remove output directories after zip file is created.
author Vimalkumar Velayudhan <vimal@biotechcoder.com>
date Thu, 02 Jul 2015 11:53:59 +0100
parents ca58e9466cbf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
1 import os
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
2 import logging
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
3 import unittest
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
4
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
5 from riboplot import core, config, plot
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
6
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
7 # use testing configuration
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
8 CONFIG = plot.CONFIG = config.TestingConfig()
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
9 logging.disable(logging.CRITICAL)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
10
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
11 RIBO_FILE = os.path.join(CONFIG.DATA_DIR, '5hRPFsorted.bam')
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
12 RNA_FILE = os.path.join(CONFIG.DATA_DIR, '5hmRNAsorted.bam')
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
13 TRANSCRIPT_NAME = 'gi|148357119|ref|NM_001098396.1|'
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
14 TRANSCRIPTOME_FASTA = os.path.join(CONFIG.DATA_DIR, 'zebrafish.fna')
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
15 TRANSCRIPTOME_FASTA_MINUS1 = os.path.join(CONFIG.DATA_DIR, 'zebrafish_minus1.fna')
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
16
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
17 class RNACountsTestCase(unittest.TestCase):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
18
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
19 def test_get_rna_counts(self):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
20 """Test get RNA counts for transcript from RNA-Seq BAM file"""
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
21 counts = plot.get_rna_counts(rna_file=RNA_FILE, transcript_name=TRANSCRIPT_NAME)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
22 self.assertIsInstance(counts, dict)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
23 self.assertTrue(len(counts) > 0)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
24
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
25 def test_missing_rna_file(self):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
26 """Exit with error if RNA BAM file does not exist. """
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
27 self.assertRaises(OSError, plot.get_rna_counts, rna_file='{}.absent'.format(RNA_FILE),
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
28 transcript_name=TRANSCRIPT_NAME)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
29
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
30 def test_missing_bedtools(self):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
31 """Exit with error if bedtools is missing."""
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
32 # reset env temporarily
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
33 paths = os.environ['PATH']
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
34 os.environ['PATH'] = ''
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
35 self.assertRaises(OSError, plot.get_rna_counts, rna_file=RNA_FILE,
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
36 transcript_name=TRANSCRIPT_NAME)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
37 os.environ['PATH'] = paths
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
38
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
39
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
40 class PlotTestCase(unittest.TestCase):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
41
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
42 def test_get_codon_positions(self):
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
43 """Test get codon positions. """
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
44 # input is the sequence obtained from get_transcript so no new lines.
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
45 fasta = ('AACCGGAGCACCCAGAGAAAACCCACGCAAACGCAGGGAGAATTTGCAAACTCCACACA'
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
46 'GAAATGCCAGCTGATCCAGCCGAGCCTCGAGTCAGCATCCTTGCTTGTTGGATGCCTGA'
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
47 'TTGCAGTTCAACTCCAAACTCAGTTGGACCAGCTGATCAGTG')
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
48 codon_positions = plot.get_start_stops(fasta)
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
49 expected = {1: {'starts': [], 'stops': []},
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
50 2: {'starts': [], 'stops': [71, 116, 152]},
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
51 3: {'starts': [63, 111], 'stops': []}}
ca58e9466cbf First commit
Vimalkumar Velayudhan <vimal@biotechcoder.com>
parents:
diff changeset
52 self.assertEqual(codon_positions, expected)