annotate tests/test_riboseqr.py @ 0:e01de823e919 draft default tip

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