view commons/tools/tests/Test_refalign2fasta.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
line wrap: on
line source

import unittest
import time
from commons.tools.refalign2fasta import *
from commons.core.utils.FileUtils import FileUtils


class Test_refalign2fasta( unittest.TestCase ):
    
    
    def setUp( self ):
        self._uniqId = "%s_%s" % ( time.strftime("%Y%m%d%H%M%S") , os.getpid() )
        
        
    def tearDown( self ):
        self._uniqId = None
        
        
    def test_getAlignments( self ):
        inFile = "dummyInFile_%s" % ( self._uniqId )
        inFileHandler = open( inFile, "w" )
        inFileHandler.write( "AAAACTTTT-T\tAAAA-TTTGGT\trefseq::1 chr3 1..10\n" )  # 1 insertion and 1 deletion in the copy + 1 mismatch
        inFileHandler.close()
        lExp = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lObs = getAlignments( inFile )
        self.assertEqual( lExp, lObs )
        os.remove( inFile )
        
        
    def test_getGaps_OneGap( self ):
        sequence = "AAAACTTTT-T"
        lExp = [ ( 9, 1 ) ]
        lObs = getGaps( sequence )
        self.assertEqual( lExp, lObs )
        
        
    def test_getGaps_TwoGaps( self ):
        sequence = "AAAA--CTTT-TT"
        lExp = [ ( 4, 2 ), ( 8, 1 ) ]
        lObs = getGaps( sequence )
        self.assertEqual( lExp, lObs )
        
        
    def test_getGapsOnRefSeq_OneSeq( self ):
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lExp = [ [ ( 9, 1 ) ] ]
        lObs = getGapsOnRefSeq( lAlign )
        self.assertEqual( lExp, lObs )
        
        
    def test_getGapsOnRefSeq_TwoSeq( self ):
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
        lExp = [ [ ( 9, 1 ) ] ]
        lExp.append( [ ( 4, 2 ), ( 8, 1 ) ] )
        lObs = getGapsOnRefSeq( lAlign )
        self.assertEqual( lExp, lObs )
        
        
    def test_insertGap( self ):
        sequence = "GGGGAAAGTTG"
        start = 5
        length = 3
        exp = "GGGGA---AAGTTG"
        obs = insertGap( sequence, start, length )
        self.assertEqual( exp, obs )
        
        
    def test_insertListGaps( self ):
        sequence = "GGGGAAAGTTG"
        lGaps =[ ( 5, 3 ), ( 9, 1 ) ]
        exp = "GGGGA---AAGT-TG"
        obs = insertListGaps( sequence, lGaps )
        self.assertEqual( exp, obs )
        
        
    def test_insertGapsInRefSeq( self ):
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
        lGapsOnRefSeqPerAlign = [ [ ( 9, 1 ) ] ]
        lGapsOnRefSeqPerAlign.append( [ ( 4, 2 ), ( 8, 1 ) ] )
        refseqName = "reference_sequence"
        lExp = ( refseqName, "AAAA--CTTT-T-T" )
        lObs = insertGapsInRefSeq( lAlign, lGapsOnRefSeqPerAlign, refseqName )
        self.assertEqual( lExp, lObs )
        
        
    def test_insertgap_seq( self ):
        # AAAACTTTT-T (refseq)
        # AAAA-TTTGGT ( copy 1)
        #
        # AAAA--CTTT-TT (refseq)
        # AAAAGGCTTTGTT (copy 2)
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "refseq::2 chr5 1..10" ) )
        lGapsOnRefSeqPerAlign = [ [ ( 9, 1 ) ] ]
        lGapsOnRefSeqPerAlign.append( [ ( 4, 2 ), ( 8, 1 ) ] )
        lExp = [ ( "refseq::1 chr3 1..10", "AAAA---TTT-GGT" ) ]
        lExp.append( ( "refseq::2 chr5 1..10", "AAAAGGCTTTGT-T" ) )
        lObs = insertgap_seq( lAlign, lGapsOnRefSeqPerAlign )
        self.assertEqual( lExp, lObs )
        
        
    def test_getSeqWithDeletions( self ):
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "refseq::1 chr3 1..10" ) ]
        lExp = [ ( "refseq::1 chr3 1..10", "AAAA-TTTGT" ) ]
        lObs = getSeqWithDeletions( lAlign )
        self.assertEqual( lExp, lObs )
        
        
    def test_saveOnlyWithDeletions( self ):
        refseqName = "Dm-B-G54-Map3"
        lAlign = [ ( "AAAACTTTT-T", "AAAA-TTTGGT", "Dm-B-G54-Map3::1 chr3 1..10" ) ]
        lAlign.append( ( "AAAA--CTTT-TT", "AAAAGGCTTTGTT", "Dm-B-G54-Map3::2 chr5 1..10" ) )
        
        expFile = "dummyExpFile_%s"  %( self._uniqId )
        expFileHandler = open( expFile, "w" )
        expFileHandler.write( ">Dm-B-G54-Map3\n" )
        expFileHandler.write( "AAAACTTTTT\n" )
        expFileHandler.write( ">Dm-B-G54-Map3::1 chr3 1..10\n" )
        expFileHandler.write( "AAAA-TTTGT\n" )
        expFileHandler.write( ">Dm-B-G54-Map3::2 chr5 1..10\n" )
        expFileHandler.write( "AAAACTTTTT\n" )
        expFileHandler.close()
        
        obsFile = "dummyObsFile_%s" % ( self._uniqId )
        
        saveOnlyWithDeletions( lAlign, refseqName, obsFile )
        
        self.assertTrue( FileUtils.are2FilesIdentical( expFile, obsFile ) )
        
        for f in [ expFile, obsFile ]:
            os.remove( f )
            
if __name__ == "__main__":
        unittest.main()