0
|
1 """riboSeqR Galaxy unit tests"""
|
|
2 import sys
|
|
3 import unittest
|
|
4 import rpy2.robjects as robjects
|
|
5 from riboseqr import prepare, triplet, metagene, ribosome_profile, utils
|
|
6
|
|
7
|
|
8 R = robjects.r
|
|
9 DATA_DIR = 'test-data'
|
|
10 RIBO_FILES = ', '.join(
|
|
11 '{0}/{1}'.format(DATA_DIR, item) for item in
|
|
12 ['chlamy236_plus_deNovo_plusOnly_Index17',
|
|
13 'chlamy236_plus_deNovo_plusOnly_Index3',
|
|
14 'chlamy236_plus_deNovo_plusOnly_Index5',
|
|
15 'chlamy236_plus_deNovo_plusOnly_Index7'])
|
|
16
|
|
17 RNA_FILES = ', '.join(
|
|
18 '{0}/{1}'.format(DATA_DIR, item) for item in
|
|
19 ['chlamy236_plus_deNovo_plusOnly_Index10',
|
|
20 'chlamy236_plus_deNovo_plusOnly_Index12',
|
|
21 'chlamy236_plus_deNovo_plusOnly_Index14',
|
|
22 'chlamy236_plus_deNovo_plusOnly_Index16'])
|
|
23
|
|
24 FASTA_FILE = '{0}/{1}'.format(DATA_DIR, 'rsem_chlamy236_deNovo.transcripts.fa')
|
|
25
|
|
26
|
|
27 class PrepareTestCase(unittest.TestCase):
|
|
28
|
|
29 def test_process_riboseqr_input(self):
|
|
30 """Given riboSeqR format input files (bowtie output), save ribodata to \
|
|
31 an R data file.
|
|
32
|
|
33 """
|
|
34 do_prepare()
|
|
35 R('load("{}/Robjects.rda")'.format(DATA_DIR))
|
|
36 ribodat, ribodat_ref = R['riboDat'], R['riboDat_REF']
|
|
37
|
|
38 self.assertEqual(
|
|
39 '{}'.format(ribodat), '{}'.format(ribodat_ref),
|
|
40 'Generated RiboDat object must be equal to the reference')
|
|
41
|
|
42 def test_process_args(self):
|
|
43 """Test processing arguments. """
|
|
44 # "ATG" -> c("ATG")
|
|
45 rs = utils.process_args('ATG', ret_mode='charvector')
|
|
46 self.assertEqual(rs, 'c("ATG")','Return string as a character vector.')
|
|
47
|
|
48 # stop codons "TAG,TAA,TGA" -> c("TAG", "TAA", "TGA"). Also
|
|
49 # replicate names, seqnames.
|
|
50 rs = utils.process_args('TAG,TAA,TGA', ret_mode='charvector')
|
|
51 self.assertEqual(
|
|
52 rs, "c('TAG', 'TAA', 'TGA')",
|
|
53 'Return comma separated strings as a character vector.')
|
|
54
|
|
55 # "" -> None
|
|
56 rs = utils.process_args('')
|
|
57 self.assertIsNone(rs, 'Return empty string as None.')
|
|
58
|
|
59 # "27,28" -> c(27, 28)
|
|
60 rs = utils.process_args("27,28", ret_type='int', ret_mode='charvector')
|
|
61 self.assertEqual(
|
|
62 rs, "c(27, 28)", 'Return number strings as a character vector.')
|
|
63
|
|
64 # "27,28" -> [27, 28]
|
|
65 rs = utils.process_args("27,28", ret_type='int', ret_mode='list')
|
|
66 self.assertEqual(rs, [27, 28], 'Return number strings as a list.')
|
|
67
|
|
68 # "0,2" -> list(0,2)
|
|
69 rs = utils.process_args("0,2", ret_type='int', ret_mode='listvector')
|
|
70 self.assertEqual(
|
|
71 rs, "list(0, 2)", 'Return number strings as a list vector.')
|
|
72
|
|
73 # "50" -> 50
|
|
74 rs = utils.process_args("50", ret_type='int')
|
|
75 self.assertEqual(rs, 50, 'Return number string as a number.')
|
|
76
|
|
77 # "-200" -> -200
|
|
78 rs = utils.process_args("-200", ret_type='int')
|
|
79 self.assertEqual(rs, -200, 'Return number string as a number.')
|
|
80
|
|
81 # "TRUE" -> TRUE
|
|
82 rs = utils.process_args("TRUE", ret_type='bool')
|
|
83 self.assertEqual(rs, 'TRUE', 'Return bool string as bool.')
|
|
84
|
|
85 # 'chlamy17,chlamy3' -> 'chlamy17,chlamy3' for ribo and rna names
|
|
86 rs = utils.process_args('chlamy17,chlamy3')
|
|
87 self.assertEqual(rs, 'chlamy17,chlamy3', 'Return csv string as string.')
|
|
88
|
|
89 # 'chlamy17.idx, chlamy3.idx' -> ['chlamy17.idx', 'chlamy3.idx']
|
|
90 rs = utils.process_args('chlamy17.idx, chlamy3.idx', ret_mode='list')
|
|
91 self.assertEqual(rs, ['chlamy17.idx', 'chlamy3.idx'],
|
|
92 'Return files as a list.')
|
|
93
|
|
94
|
|
95 class TripletTestCase(unittest.TestCase):
|
|
96
|
|
97 def test_find_periodicity(self):
|
|
98 """Test triplet periodicity. """
|
|
99 do_prepare()
|
|
100 do_periodicity()
|
|
101 fcs, fs, fasta_cds = R['fCs'], R['fS'], R['fastaCDS']
|
|
102
|
|
103 R('load("{}/Robjects.rda")'.format(DATA_DIR))
|
|
104 fcs_ref, fs_ref, fasta_cds_ref = (
|
|
105 R['fCs_REF'], R['fS_REF'], R['fastaCDS_REF'])
|
|
106
|
|
107 self.assertEqual(
|
|
108 (str(fcs), str(fs), str(fasta_cds)),
|
|
109 (str(fcs_ref), str(fs_ref), str(fasta_cds_ref)),
|
|
110 msg='Generated fCs, fS and fastaCDS objects must be equal '
|
|
111 'to reference')
|
|
112
|
|
113
|
|
114 class MetageneTestCase(unittest.TestCase):
|
|
115
|
|
116 def test_do_analysis(self):
|
|
117 """Test metagene analysis. """
|
|
118 R('load("{}/Robjects.rda")'.format(DATA_DIR))
|
|
119 do_prepare()
|
|
120 do_periodicity()
|
|
121 do_metagene()
|
|
122
|
|
123 self.assertEqual(str(R['ffCs']),
|
|
124 str(R['ffCs_REF']), 'ffCs must be equal to reference')
|
|
125
|
|
126
|
|
127 def do_prepare():
|
|
128 """Run the prepare step to generate riboDat from input files."""
|
|
129 prepare.generate_ribodata(
|
|
130 ribo_files=RIBO_FILES, rna_files=RNA_FILES, replicate_names='WT,WT,M,M',
|
|
131 rdata_save='/tmp/Prepare.rda', sam_format=False, output_path="/tmp",
|
|
132 html_file='/tmp/Prepare-report.html')
|
|
133
|
|
134
|
|
135 def do_periodicity():
|
|
136 """Run the periodicity step"""
|
|
137 triplet.find_periodicity(
|
|
138 rdata_load='/tmp/Prepare.rda', start_codons='ATG',
|
|
139 stop_codons='TAG,TAA,TGA', fasta_file=FASTA_FILE,
|
|
140 include_lengths='25:30', analyze_plot_lengths='26:30',
|
|
141 rdata_save='/tmp/Periodicity.rda', output_path="/tmp",
|
|
142 html_file='/tmp/Periodicity-report.html')
|
|
143
|
|
144
|
|
145 def do_metagene():
|
|
146 """Run the metagene step"""
|
|
147 metagene.do_analysis(
|
|
148 rdata_load='/tmp/Periodicity.rda', selected_lengths='27,28',
|
|
149 selected_frames='1,0', hit_mean='50', unique_hit_mean='10',
|
|
150 ratio_check='TRUE', min5p='-20', max5p='200', min3p='-200', max3p='20',
|
|
151 cap='200', plot_title='Metagene analysis', plot_lengths='27',
|
|
152 rdata_save='/tmp/Metagene.rda', output_path="/tmp",
|
|
153 html_file='/tmp/Metagene-report.html')
|