comparison commons/tools/tests/Test_refalign2fasta.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
1 import unittest
2 import time
3 from commons.tools.refalign2fasta import *
4 from commons.core.utils.FileUtils import FileUtils
5
6
7 class Test_refalign2fasta( unittest.TestCase ):
8
9
10 def setUp( self ):
11 self._uniqId = "%s_%s" % ( time.strftime("%Y%m%d%H%M%S") , os.getpid() )
12
13
14 def tearDown( self ):
15 self._uniqId = None
16
17
18 def test_getAlignments( self ):
19 inFile = "dummyInFile_%s" % ( self._uniqId )
20 inFileHandler = open( inFile, "w" )
21 inFileHandler.write( "AAAACTTTT-T\tAAAA-TTTGGT\trefseq::1 chr3 1..10\n" ) # 1 insertion and 1 deletion in the copy + 1 mismatch
22 inFileHandler.close()
23 lExp = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
24 lObs = getAlignments( inFile )
25 self.assertEqual( lExp, lObs )
26 os.remove( inFile )
27
28
29 def test_getGaps_OneGap( self ):
30 sequence = "AAAACTTTT-T"
31 lExp = [ ( 9, 1 ) ]
32 lObs = getGaps( sequence )
33 self.assertEqual( lExp, lObs )
34
35
36 def test_getGaps_TwoGaps( self ):
37 sequence = "AAAA--CTTT-TT"
38 lExp = [ ( 4, 2 ), ( 8, 1 ) ]
39 lObs = getGaps( sequence )
40 self.assertEqual( lExp, lObs )
41
42
43 def test_getGapsOnRefSeq_OneSeq( self ):
44 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
45 lExp = [ [ ( 9, 1 ) ] ]
46 lObs = getGapsOnRefSeq( lAlign )
47 self.assertEqual( lExp, lObs )
48
49
50 def test_getGapsOnRefSeq_TwoSeq( self ):
51 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
52 lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
53 lExp = [ [ ( 9, 1 ) ] ]
54 lExp.append( [ ( 4, 2 ), ( 8, 1 ) ] )
55 lObs = getGapsOnRefSeq( lAlign )
56 self.assertEqual( lExp, lObs )
57
58
59 def test_insertGap( self ):
60 sequence = "GGGGAAAGTTG"
61 start = 5
62 length = 3
63 exp = "GGGGA---AAGTTG"
64 obs = insertGap( sequence, start, length )
65 self.assertEqual( exp, obs )
66
67
68 def test_insertListGaps( self ):
69 sequence = "GGGGAAAGTTG"
70 lGaps =[ ( 5, 3 ), ( 9, 1 ) ]
71 exp = "GGGGA---AAGT-TG"
72 obs = insertListGaps( sequence, lGaps )
73 self.assertEqual( exp, obs )
74
75
76 def test_insertGapsInRefSeq( self ):
77 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
78 lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
79 lGapsOnRefSeqPerAlign = [ [ ( 9, 1 ) ] ]
80 lGapsOnRefSeqPerAlign.append( [ ( 4, 2 ), ( 8, 1 ) ] )
81 refseqName = "reference_sequence"
82 lExp = ( refseqName, "AAAA--CTTT-T-T" )
83 lObs = insertGapsInRefSeq( lAlign, lGapsOnRefSeqPerAlign, refseqName )
84 self.assertEqual( lExp, lObs )
85
86
87 def test_insertgap_seq( self ):
88 # AAAACTTTT-T (refseq)
89 # AAAA-TTTGGT ( copy 1)
90 #
91 # AAAA--CTTT-TT (refseq)
92 # AAAAGGCTTTGTT (copy 2)
93 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
94 lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
95 lGapsOnRefSeqPerAlign = [ [ ( 9, 1 ) ] ]
96 lGapsOnRefSeqPerAlign.append( [ ( 4, 2 ), ( 8, 1 ) ] )
97 lExp = [ ( "refseq::1 chr3 1..10", "AAAA---TTT-GGT" ) ]
98 lExp.append( ( "refseq::2 chr5 1..10", "AAAAGGCTTTGT-T" ) )
99 lObs = insertgap_seq( lAlign, lGapsOnRefSeqPerAlign )
100 self.assertEqual( lExp, lObs )
101
102
103 def test_getSeqWithDeletions( self ):
104 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
105 lExp = [ ( "refseq::1 chr3 1..10", "AAAA-TTTGT" ) ]
106 lObs = getSeqWithDeletions( lAlign )
107 self.assertEqual( lExp, lObs )
108
109
110 def test_saveOnlyWithDeletions( self ):
111 refseqName = "Dm-B-G54-Map3"
112 lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "Dm-B-G54-Map3::1 chr3 1..10" ) ]
113 lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "Dm-B-G54-Map3::2 chr5 1..10" ) )
114
115 expFile = "dummyExpFile_%s" %( self._uniqId )
116 expFileHandler = open( expFile, "w" )
117 expFileHandler.write( ">Dm-B-G54-Map3\n" )
118 expFileHandler.write( "AAAACTTTTT\n" )
119 expFileHandler.write( ">Dm-B-G54-Map3::1 chr3 1..10\n" )
120 expFileHandler.write( "AAAA-TTTGT\n" )
121 expFileHandler.write( ">Dm-B-G54-Map3::2 chr5 1..10\n" )
122 expFileHandler.write( "AAAACTTTTT\n" )
123 expFileHandler.close()
124
125 obsFile = "dummyObsFile_%s" % ( self._uniqId )
126
127 saveOnlyWithDeletions( lAlign, refseqName, obsFile )
128
129 self.assertTrue( FileUtils.are2FilesIdentical( expFile, obsFile ) )
130
131 for f in [ expFile, obsFile ]:
132 os.remove( f )
133
134 if __name__ == "__main__":
135 unittest.main()