File: regressions.py

package info (click to toggle)
bowtie2 2.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,492 kB
  • sloc: cpp: 63,838; perl: 7,232; sh: 1,131; python: 987; makefile: 541; ansic: 122
file content (207 lines) | stat: -rwxr-xr-x 8,742 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python3

import os
import inspect
import unittest
import logging
import shutil
import bt2face
import dataface
import btdata
from optparse import OptionParser


class TestRegressions(unittest.TestCase):
    """
    Main regression fixture.
    """
    
    def test_288(self):
        """ Check if --un option works correctly when used with --no-unal
        """
        # run --un and record unaligned file size and orig file size
        # run --no-unal and record file size
        # check if --no-unali + --un files size equal the orig file size
        out_sam     = 'test288_out.sam'
        not_algn    = 'test288_nal.fastq'

        ref_index = os.path.join(g_bdata.index_dir_path,'lambda_virus')
        reads     = os.path.join(g_bdata.reads_dir_path,'longreads.fq')

        args = "--quiet -x %s -U %s -a --un %s -S %s" % (ref_index,reads,not_algn,out_sam)
        ret = g_bt.run(args)
        self.assertEqual(ret,0)
        sam_size     = dataface.SamFile(out_sam).size()
        no_algn_size = dataface.FastaQFile(not_algn).size()
        args = "--quiet -x %s -U %s -a --un %s --no-unal -S %s" % (ref_index,reads,not_algn,out_sam)
        ret = g_bt.run(args)
        self.assertEqual(ret,0)
        no_al_sam_size = dataface.SamFile(out_sam).size()
        self.assertEqual(no_al_sam_size + no_algn_size, sam_size)
        os.remove(out_sam)
        os.remove(not_algn)


    def test_279(self):
        """ Check if --un-conc option works correctly when used with --no-unal
        """
        out_no_conc = 'test279_nconc.fq'
        out_conc    = 'test279_conc.fq'
        out_sam     = 'test279.sam'
        out_p1_nc   = 'test279_nconc.1.fq'
        out_p2_nc   = 'test279_nconc.2.fq'
        out_p1_conc = 'test279_conc.1.fq'
        out_p2_conc = 'test279_conc.2.fq'
        ref_index   = os.path.join(g_bdata.index_dir_path,'lambda_virus')
        pairs_1     = os.path.join(g_bdata.reads_dir_path,'reads_1.fq')
        pairs_2     = os.path.join(g_bdata.reads_dir_path,'reads_2.fq')

        args = "--quiet -x %s -1 %s -2 %s --un-conc %s --dovetail --al-conc %s -S %s" % (ref_index,pairs_1,pairs_2,out_no_conc,out_conc,out_sam)
        ret = g_bt.run(args)
        self.assertEqual(ret,0)
        conc_p1_size_step1 = dataface.FastaQFile(out_p1_conc).size()
        conc_p2_size_step1 = dataface.FastaQFile(out_p2_conc).size()
        nc_p1_size_step1   = dataface.FastaQFile(out_p1_nc).size()
        nc_p2_size_step1   = dataface.FastaQFile(out_p2_nc).size()
        self.assertEqual(conc_p1_size_step1, conc_p2_size_step1, "Number of concordant sequences in pass 1 should match.")
        self.assertEqual(nc_p1_size_step1, nc_p2_size_step1, "Number of non-concordant sequences in pass 1 should match.")

        args = "--quiet -x %s -1 %s -2 %s --no-unal --un-conc %s --dovetail --al-conc %s -S %s" % (ref_index,pairs_1,pairs_2,out_no_conc,out_conc,out_sam)
        ret = g_bt.run(args)
        self.assertEqual(ret,0)
        conc_p1_size_step2 = dataface.FastaQFile(out_p1_conc).size()
        conc_p2_size_step2 = dataface.FastaQFile(out_p2_conc).size()
        nc_p1_size_step2   = dataface.FastaQFile(out_p1_nc).size()
        nc_p2_size_step2   = dataface.FastaQFile(out_p2_nc).size()
        self.assertEqual(conc_p1_size_step2, conc_p2_size_step2, "Number of concordant sequences in pass 2 should match.")
        self.assertEqual(nc_p1_size_step2, nc_p2_size_step2, "Number of non-concordant sequences in pass 2 should match.")
        self.assertEqual(conc_p1_size_step1, conc_p1_size_step2, "Number of concordant sequences in both steps should match.")
        self.assertEqual(conc_p2_size_step1, conc_p2_size_step2, "Number of concordant sequences in both steps should match.")
        self.assertEqual(nc_p1_size_step1, nc_p1_size_step2, "Number of non-concordant sequences in both steps should match.")
        self.assertEqual(nc_p2_size_step1, nc_p2_size_step2, "Number of non-concordant sequences in both steps should match.")

        self.assertTrue(1,"temp")
        os.remove(out_p1_conc)
        os.remove(out_p2_conc)
        os.remove(out_p1_nc)
        os.remove(out_p2_nc)
        os.remove(out_sam)
         
    
    def test_version(self):
        """ Check if --version option works correctly. 
        """
        args = "--version"
        ret  = g_bt.silent_run(args)
        self.assertEqual(ret,0)
        
       
    def test_x_option(self):
        """ Check if -x is indeed mandatory. 
        """
        ref_index = os.path.join(g_bdata.index_dir_path,'lambda_virus')
        reads     = os.path.join(g_bdata.reads_dir_path,'longreads.fq')
        out_sam   = 'test_x_option.sam'

        args = "%s -U %s -S %s " % (ref_index,reads,out_sam)
        ret = g_bt.silent_run(args)
        self.assertNotEqual(ret,0)
        args = "-x %s -U %s -S %s " % (ref_index,reads,out_sam)
        ret = g_bt.silent_run(args)
        self.assertEqual(ret, 0)
        os.remove(out_sam)


    def test_313(self):
        """ Check if --un-conc, --al-conc, --un, --al do not fail in case of a dot 
            character present in their path name.
        """
        ref_index   = os.path.join(g_bdata.index_dir_path,'lambda_virus')
        pairs_1     = os.path.join(g_bdata.reads_dir_path,'reads_1.fq')
        pairs_2     = os.path.join(g_bdata.reads_dir_path,'reads_2.fq')
        no_dot_dir  = 'nodotdir'
        dot_dir     = 'v.12_.test'
        un_basename = 'bname'
        out_spec    = os.path.join(no_dot_dir,un_basename)
        out_spec_dot= os.path.join(dot_dir,un_basename)
        args        = "--quiet -x %s -1 %s -2 %s --un-conc %s -S %s" % (ref_index,pairs_1,pairs_2,out_spec,os.devnull)
        
        try:
            os.makedirs(no_dot_dir)
            os.makedirs(dot_dir)
        except:
            pass

        ret = g_bt.silent_run(args)
        self.assertEqual(ret, 0)
        mate1_count = dataface.FastaQFile(out_spec + '.1').size()
        args        = "--quiet -x %s -1 %s -2 %s --un-conc %s -S %s" % (ref_index,pairs_1,pairs_2,out_spec_dot,os.devnull)
        ret = g_bt.silent_run(args)
        self.assertEqual(ret, 0, "--un-conc should work with dots in its path.")
        mate1_count_dot = dataface.FastaQFile(out_spec_dot + '.1').size()
        self.assertEqual(mate1_count, mate1_count_dot, "Number of seqs should be the same.")

        args        = "--quiet -x %s -1 %s -2 %s --un-conc %s -S %s" % (ref_index,pairs_1,pairs_2,dot_dir,os.devnull)
        ret = g_bt.silent_run(args)
        self.assertEqual(ret, 0, "--un-conc should work with a path only.")
        mate1_fs    = os.path.join(dot_dir,"un-conc-mate.1")
        self.assertTrue(os.path.isfile(mate1_fs), "--un-conc output is missing for path only case.")
        mate1_count_dot_only = dataface.FastaQFile(mate1_fs).size()
        self.assertEqual(mate1_count, mate1_count_dot_only, "Number of seqs should be the same.")

        args= "--quiet -x %s -1 %s -2 %s --un-conc %s --al-conc %s --un %s -S %s" % (ref_index,pairs_1,pairs_2,dot_dir,dot_dir,dot_dir,os.devnull)
        ret = g_bt.run(args)
        self.assertEqual(ret, 0)
        mate1_al    = os.path.join(dot_dir,"al-conc-mate.1")
        mate1_un    = os.path.join(dot_dir,"un-seqs")
        self.assertTrue(
            os.path.isfile(mate1_fs) and 
            os.path.isfile(mate1_al) and 
            os.path.isfile(mate1_un),
            "--un-conc, --al-conc and --un should not clobber"
        )
        shutil.rmtree(no_dot_dir)
        shutil.rmtree(dot_dir)
        

   
def get_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(TestRegressions)
    
            
def parse_args():
    usage = " %prog [options] \n\n"
    usage += "Some regression tests.\n"
    parser = OptionParser(usage=usage)
    parser.add_option("-v", "--verbose", 
                    action="store_true",dest="verbose", default=False,
                    help="Print more info about each test.")

    (options, args) = parser.parse_args()
    return options
    
    
g_bdata = None
g_bt    = None

if __name__ == "__main__":
    logging.basicConfig(format='%(levelname)s:%(message)s',level=logging.ERROR)    
    options = parse_args()

    runner = unittest.TextTestRunner()
    if options.verbose:
        logging.getLogger().setLevel(level=logging.INFO)
        runner = unittest.TextTestRunner(verbosity=2)
   
    src_file_path  = os.path.realpath(inspect.getsourcefile(parse_args))
    curr_path      = os.path.dirname(src_file_path)
    bw2_subdir     = 'bowtie2'

    i = curr_path.find(bw2_subdir)
    bt2_path = curr_path[:i+len(bw2_subdir)] 
    
    g_bdata = btdata.ExampleData(bt2_path)
    g_bt    = bt2face.BowtieSuite(bt2_path)
    runner.run(get_suite())