diff tests/test_riboseqr.py @ 5:423ad61697c4

Bugfix 1: [triplet] Lengths (frameCounting) if given should be a range (not zero). readingFrame function fails with subscript out of bounds. Bugfix 2: [triplet] Check if transcript name in SAM matches the name in FASTA. Produce an error message if it's not. This fixes the problem where an empty plot is produced (no bars). [ribosome_profile] - A proper error message is now produced if an invalid transcript name is provided. Updated test data
author Vimalkumar Velayudhan <vimalkumarvelayudhan@gmail.com>
date Tue, 27 Oct 2015 12:21:50 +0000
parents c34c364ce75d
children
line wrap: on
line diff
--- a/tests/test_riboseqr.py	Tue Jul 21 15:40:22 2015 +0100
+++ b/tests/test_riboseqr.py	Tue Oct 27 12:21:50 2015 +0000
@@ -1,6 +1,11 @@
 """riboSeqR Galaxy unit tests"""
+import os
+import shutil
 import unittest
-from riboseqr import utils
+import tempfile
+from riboseqr import utils, triplet, ribosome_profile
+
+DATA_DIR = 'test-data'
 
 
 class PrepareTestCase(unittest.TestCase):
@@ -9,7 +14,7 @@
         """Test processing arguments. """
         # "ATG" -> c("ATG")
         rs = utils.process_args('ATG', ret_mode='charvector')
-        self.assertEqual(rs, 'c("ATG")','Return string as a character vector.')
+        self.assertEqual(rs, 'c("ATG")', 'Return string as a character vector.')
 
         # stop codons "TAG,TAA,TGA" -> c("TAG", "TAA", "TGA"). Also
         # replicate names, seqnames.
@@ -56,3 +61,79 @@
         rs = utils.process_args('chlamy17.idx, chlamy3.idx', ret_mode='list')
         self.assertEqual(rs, ['chlamy17.idx', 'chlamy3.idx'],
                          'Return files as a list.')
+
+
+class TripletTestCase(unittest.TestCase):
+    """Test triple periodicity"""
+    html_file = None
+    output_path = None
+    rdata_load = os.path.join(DATA_DIR, 'Prepare riboSeqR input (R data file)')
+    rdata_save = None
+    include_lengths = '25:30'
+    analyze_plot_lengths = '26:30'
+
+    def setUp(self):
+        self.html_file, self.output_path = tempfile.mkstemp()[1], tempfile.mkdtemp()
+        self.rdata_save = os.path.join(self.output_path, 'Periodicity.rda')
+
+    def tearDown(self):
+        os.remove(self.html_file)
+        shutil.rmtree(self.output_path)
+
+    def test_triplet_periodicity(self):
+        """Given a prepared RDA file, produce triplet periodicity output (R data file and plots).
+
+        Note: only checks for the presence of program outputs
+        """
+        triplet.find_periodicity(
+            rdata_load=self.rdata_load,
+            fasta_file=os.path.join(DATA_DIR, 'rsem_chlamy236_deNovo.transcripts.fa'),
+            include_lengths=self.include_lengths, analyze_plot_lengths=self.analyze_plot_lengths,
+            html_file=self.html_file, output_path=self.output_path,
+            rdata_save=self.rdata_save)
+
+        for output in ('periodicity.R', 'Periodicity.rda', 'Periodicity-plot.pdf', 'Periodicity-plot.png'):
+            self.assertTrue(os.path.exists(os.path.join(self.output_path, output)))
+
+    def test_transcript_name(self):
+        """Check that the transcript name is the same in the SAM and FASTA file. If not, raise an error."""
+        self.assertRaises(
+            triplet.TripletPeriodicityError, triplet.find_periodicity,
+            rdata_load=self.rdata_load,
+            fasta_file=os.path.join(DATA_DIR, 'rsem_chlamy236_deNovo.transcripts_mod.fa'),
+            include_lengths=self.include_lengths, analyze_plot_lengths=self.analyze_plot_lengths,
+            html_file=self.html_file, output_path=self.output_path,
+            rdata_save=self.rdata_save)
+
+
+class RibosomeProfileTestCase(unittest.TestCase):
+    """Test plot ribosome profile."""
+    html_file = None
+    output_path = None
+    rdata_load = os.path.join(DATA_DIR, 'Metagene analysis (R data file)')
+
+    def setUp(self):
+        self.html_file, self.output_path = tempfile.mkstemp()[1], tempfile.mkdtemp()
+
+    def tearDown(self):
+        os.remove(self.html_file)
+        shutil.rmtree(self.output_path)
+
+    def test_plot_ribosome_profile(self):
+        """Given the right transcript name, produce a profile plot. """
+        ribosome_profile.plot_transcript(
+            rdata_load=self.rdata_load, transcript_name='CUFF.37930.1',
+            transcript_length='27', transcript_cap='200', html_file=self.html_file,
+            output_path=self.output_path)
+
+        for output in ('ribosome-profile.R', 'Ribosome-profile-plot.pdf', 'Ribosome-profile-plot_1.png',
+                       'Ribosome-profile-plot_2.png', 'Ribosome-profile-plot_3.png', 'Ribosome-profile-plot_4.png'):
+            self.assertTrue(os.path.exists(os.path.join(self.output_path, output)))
+
+    def test_invalid_transcript(self):
+        """If a transcript is missing, produce an error message."""
+        self.assertRaises(
+            ribosome_profile.RibosomeProfileError, ribosome_profile.plot_transcript,
+            rdata_load=self.rdata_load, transcript_name='MCUFF.37930.1',
+            transcript_length='27', transcript_cap='200', html_file=self.html_file,
+            output_path=self.output_path)