示例#1
0
    def testResidueCountsOneReadTwoHSPsNotOverlapping(self):
        """
        The residueCounts method must return the correct result when just one
        read with two HSPs is aligned to a title and the HSPs do not overlap
        one another.

        HSP1:    ACGT
        HSP2:              CGTT
        """
        read = Read('id', 'ACGT')
        hsp1 = HSP(33, readStart=0, readEnd=4, readStartInSubject=0,
                   readEndInSubject=4, subjectStart=0, subjectEnd=4,
                   readMatchedSequence='ACGT', subjectMatchedSequence='ACGT')
        hsp2 = HSP(33, readStart=0, readEnd=4, readStartInSubject=10,
                   readEndInSubject=14, subjectStart=10, subjectEnd=14,
                   readMatchedSequence='CGTT', subjectMatchedSequence='CGTT')
        titleAlignments = TitleAlignments('subject title', 55)
        titleAlignment = TitleAlignment(read, [hsp1, hsp2])
        titleAlignments.addAlignment(titleAlignment)
        self.assertEqual(
            {
                0: {'A': 1},
                1: {'C': 1},
                2: {'G': 1},
                3: {'T': 1},
                10: {'C': 1},
                11: {'G': 1},
                12: {'T': 1},
                13: {'T': 1},
            },
            titleAlignments.residueCounts())
示例#2
0
 def testSummary(self):
     """
     The summary method must return the correct result.
     """
     titleAlignments = TitleAlignments('subject title', 10)
     titleAlignments.addAlignment(
         TitleAlignment(Read('id1', 'ACGT'), [
             HSP(30, subjectStart=0, subjectEnd=2),
         ]))
     titleAlignments.addAlignment(
         TitleAlignment(Read('id2', 'ACGT'), [
             HSP(55, subjectStart=2, subjectEnd=4),
             HSP(40, subjectStart=8, subjectEnd=9),
         ]))
     self.assertEqual(
         {
             'bestScore': 55,
             'coverage': 0.5,
             'hspCount': 3,
             'medianScore': 40,
             'readCount': 2,
             'subjectLength': 10,
             'subjectTitle': 'subject title',
         },
         titleAlignments.summary())
示例#3
0
    def testResidueCountsTwoReadsTwoHSPsLeftOverhang(self):
        """
        The residueCounts method must return the correct result when two
        reads, each with one HSP are aligned to a title and the leftmost HSP
        is aligned before the left edge of the subject (i.e, will include
        negative subject offsets).

        Subject:      GTT
        HSP1:       ACGT
        HSP2:        CGTT
        """
        read1 = Read('id', 'ACGT')
        hsp1 = HSP(33, readStart=0, readEnd=4, readStartInSubject=-2,
                   readEndInSubject=2, subjectStart=0, subjectEnd=2,
                   readMatchedSequence='GT', subjectMatchedSequence='GT')
        read2 = Read('id', 'CGTT')
        hsp2 = HSP(33, readStart=0, readEnd=4, readStartInSubject=-1,
                   readEndInSubject=3, subjectStart=0, subjectEnd=3,
                   readMatchedSequence='GTT', subjectMatchedSequence='GTT')
        titleAlignments = TitleAlignments('subject title', 55)
        titleAlignment = TitleAlignment(read1, [hsp1])
        titleAlignments.addAlignment(titleAlignment)
        titleAlignment = TitleAlignment(read2, [hsp2])
        titleAlignments.addAlignment(titleAlignment)
        self.assertEqual(
            {
                -2: {'A': 1},
                -1: {'C': 2},
                0: {'G': 2},
                1: {'T': 2},
                2: {'T': 1},
            },
            titleAlignments.residueCounts())
示例#4
0
    def testResidueCountsCaseConvertUpperIsDefault(self):
        """
        The residueCounts method must convert to uppercase by default.

        HSP1:       AcgT
        HSP2:        CGTT
        """
        read = Read('id', 'ACGT')
        hsp1 = HSP(33, readStart=0, readEnd=4, readStartInSubject=10,
                   readEndInSubject=14, subjectStart=10, subjectEnd=14,
                   readMatchedSequence='AcgT', subjectMatchedSequence='ACGT')
        hsp2 = HSP(33, readStart=0, readEnd=4, readStartInSubject=11,
                   readEndInSubject=15, subjectStart=11, subjectEnd=15,
                   readMatchedSequence='CGTT', subjectMatchedSequence='CGTT')
        titleAlignments = TitleAlignments('subject title', 55)
        titleAlignment = TitleAlignment(read, [hsp1, hsp2])
        titleAlignments.addAlignment(titleAlignment)
        self.assertEqual(
            {
                10: {'A': 1},
                11: {'C': 2},
                12: {'G': 2},
                13: {'T': 2},
                14: {'T': 1},
            },
            titleAlignments.residueCounts())
示例#5
0
    def testResidueCountsCaseConvertLower(self):
        """
        The residueCounts method must return the correct result when asked to
        convert residues to lower case.

        HSP1:       AcgT
        HSP2:        CGTT
        """
        read = Read('id', 'ACGT')
        hsp1 = HSP(33, readStart=0, readEnd=4, readStartInSubject=10,
                   readEndInSubject=14, subjectStart=10, subjectEnd=14,
                   readMatchedSequence='AcgT', subjectMatchedSequence='ACGT')
        hsp2 = HSP(33, readStart=0, readEnd=4, readStartInSubject=11,
                   readEndInSubject=15, subjectStart=11, subjectEnd=15,
                   readMatchedSequence='CGTT', subjectMatchedSequence='CGTT')
        titleAlignments = TitleAlignments('subject title', 55)
        titleAlignment = TitleAlignment(read, [hsp1, hsp2])
        titleAlignments.addAlignment(titleAlignment)
        self.assertEqual(
            {
                10: {'a': 1},
                11: {'c': 2},
                12: {'g': 2},
                13: {'t': 2},
                14: {'t': 1},
            },
            titleAlignments.residueCounts(convertCaseTo='lower'))
示例#6
0
    def testToDict(self):
        """
        The toDict method must return the expected dictionary.
        """
        hsp = HSP(0, readStart=1, readEnd=2,
                  readStartInSubject=3, readEndInSubject=4,
                  subjectStart=5, subjectEnd=6,
                  readMatchedSequence='aaa', subjectMatchedSequence='ccc',
                  readFrame=7, subjectFrame=8, identicalCount=9,
                  positiveCount=10)

        self.assertEqual(
            {
                'score': 0,
                'readStart': 1,
                'readEnd': 2,
                'readStartInSubject': 3,
                'readEndInSubject': 4,
                'subjectStart': 5,
                'subjectEnd': 6,
                'readFrame': 7,
                'subjectFrame': 8,
                'identicalCount': 9,
                'positiveCount': 10,
                'readMatchedSequence': 'aaa',
                'subjectMatchedSequence': 'ccc',
            },
            hsp.toDict())
示例#7
0
 def testAddHsp(self):
     """
     It must be possible to add an HSP to an alignment.
     """
     alignment = Alignment(45, 'title')
     alignment.addHsp(HSP(3))
     self.assertEqual(HSP(3), alignment.hsps[0])
示例#8
0
    def testToDict(self):
        """
        The toDict method must return the expected dictionary.
        """
        hsp = HSP(0,
                  readStart=1,
                  readEnd=2,
                  readStartInSubject=3,
                  readEndInSubject=4,
                  subjectStart=5,
                  subjectEnd=6,
                  readMatchedSequence='aaa',
                  subjectMatchedSequence='ccc',
                  readFrame=7,
                  subjectFrame=8,
                  identicalCount=9,
                  positiveCount=10)

        self.assertEqual(
            {
                'score': 0,
                'readStart': 1,
                'readEnd': 2,
                'readStartInSubject': 3,
                'readEndInSubject': 4,
                'subjectStart': 5,
                'subjectEnd': 6,
                'readFrame': 7,
                'subjectFrame': 8,
                'identicalCount': 9,
                'positiveCount': 10,
                'readMatchedSequence': 'aaa',
                'subjectMatchedSequence': 'ccc',
            }, hsp.toDict())
示例#9
0
    def testResidueCountsOneReadTwoHSPsNotAtStartOfSubject(self):
        """
        The residueCounts method must return the correct result when just one
        read with two HSPs is aligned to a title and the leftmost HSP is not
        aligned with the left edge of the subject.

        HSP1:       ACGT
        HSP2:        CGTT
        """
        read = Read('id', 'ACGT')
        hsp1 = HSP(33, readStart=0, readEnd=4, readStartInSubject=10,
                   readEndInSubject=14, subjectStart=10, subjectEnd=14,
                   readMatchedSequence='ACGT', subjectMatchedSequence='ACGT')
        hsp2 = HSP(33, readStart=0, readEnd=4, readStartInSubject=11,
                   readEndInSubject=15, subjectStart=11, subjectEnd=15,
                   readMatchedSequence='CGTT', subjectMatchedSequence='CGTT')
        titleAlignments = TitleAlignments('subject title', 55)
        titleAlignment = TitleAlignment(read, [hsp1, hsp2])
        titleAlignments.addAlignment(titleAlignment)
        self.assertEqual(
            {
                10: {'A': 1},
                11: {'C': 2},
                12: {'G': 2},
                13: {'T': 2},
                14: {'T': 1},
            },
            titleAlignments.residueCounts())
示例#10
0
    def testTitleCollection(self):
        """
        A title that occurs in the alignments of multiple reads must have
        the data from both reads collected properly.
        """
        mockOpener = mock_open(read_data=(
            dumps(PARAMS) + '\n' + dumps(RECORD2) + '\n' +
            dumps(RECORD3) + '\n'))
        with patch.object(builtins, 'open', mockOpener):
            reads = Reads()
            read2 = Read('id2', 'A' * 70)
            read3 = Read('id3', 'A' * 70)
            reads.add(read2)
            reads.add(read3)
            readsAlignments = DiamondReadsAlignments(reads, 'file.json')
            titlesAlignments = TitlesAlignments(readsAlignments)

            title = 'gi|887699|gb|DQ37780 Cowpox virus 15'
            titleAlignments = titlesAlignments[title]
            self.assertEqual(title, titleAlignments.subjectTitle)
            self.assertEqual(30000, titleAlignments.subjectLength)
            self.assertEqual(2, len(titleAlignments))

            self.assertEqual(read2, titleAlignments[0].read)
            self.assertEqual(HSP(20), titleAlignments[0].hsps[0])

            self.assertEqual(read3, titleAlignments[1].read)
            self.assertEqual(HSP(20), titleAlignments[1].hsps[0])
示例#11
0
    def testExpectedTitleDetails(self):
        """
        An instance of TitleAlignments in a TitlesAlignments instance must
        have the expected attributes.
        """
        mockOpener = mock_open(read_data=(
            dumps(PARAMS) + '\n' + dumps(RECORD0) + '\n'))
        with patch.object(builtins, 'open', mockOpener):
            reads = Reads()
            read = Read('id0', 'A' * 70)
            reads.add(read)
            readsAlignments = DiamondReadsAlignments(reads, 'file.json')
            titlesAlignments = TitlesAlignments(readsAlignments)

            title = 'gi|887699|gb|DQ37780 Squirrelpox virus 1296/99'
            titleAlignments = titlesAlignments[title]
            self.assertEqual(title, titleAlignments.subjectTitle)
            self.assertEqual(37000, titleAlignments.subjectLength)
            self.assertEqual(1, len(titleAlignments))
            self.assertEqual(read, titleAlignments[0].read)
            self.assertEqual(HSP(20), titleAlignments[0].hsps[0])

            title = 'gi|887699|gb|DQ37780 Squirrelpox virus 55'
            titleAlignments = titlesAlignments[title]
            self.assertEqual(title, titleAlignments.subjectTitle)
            self.assertEqual(38000, titleAlignments.subjectLength)
            self.assertEqual(1, len(titleAlignments))
            self.assertEqual(read, titleAlignments[0].read)
            self.assertEqual(HSP(25), titleAlignments[0].hsps[0])
示例#12
0
 def testFullCoverage(self):
     """
     The coverage method must return the correct value when the title is
     fully covered by its reads.
     """
     hsp1 = HSP(7, subjectStart=0, subjectEnd=50)
     hsp2 = HSP(8, subjectStart=50, subjectEnd=100)
     titleAlignments = TitleAlignments('subject title', 100)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(1.0, titleAlignments.coverage())
示例#13
0
 def testMedianScoreOfTwo(self):
     """
     The medianScore function must return the median score for the HSPs in
     all the alignments matching a title when given 2 scores.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(15)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(11, titleAlignments.medianScore())
示例#14
0
    def testTwoReadsInMiddle(self):
        """
        When two reads are added to the middle of an interval, there should be
        three reductions (after first empty area, after 2nd empty area, after
        final empty area.
        """
        ri = ReadIntervals(132)
        ri.add(32, 42)
        ri.add(58, 68)
        adjuster = OffsetAdjuster(ri)
        self.assertEqual([
            (32, 27),
            (58, 12),
            (132, 58),
        ], adjuster.adjustments())
        self.assertEqual(132 - 27 - 12 - 58, adjuster.adjustOffset(132))

        # Test an HSP at the beginning is unchanged.
        hsp = HSP(10,
                  readEndInSubject=10,
                  readStartInSubject=0,
                  subjectEnd=10,
                  subjectStart=0)
        adjuster.adjustHSP(hsp)
        self.assertEqual(10, hsp.readEndInSubject)
        self.assertEqual(0, hsp.readStartInSubject)
        self.assertEqual(10, hsp.subjectEnd)
        self.assertEqual(0, hsp.subjectStart)

        # Test an HSP in the first read region.
        hsp = HSP(10,
                  readEndInSubject=42,
                  readStartInSubject=32,
                  subjectEnd=40,
                  subjectStart=35)
        adjuster.adjustHSP(hsp)
        self.assertEqual(15, hsp.readEndInSubject)
        self.assertEqual(5, hsp.readStartInSubject)
        self.assertEqual(13, hsp.subjectEnd)
        self.assertEqual(8, hsp.subjectStart)

        # Test an HSP in the second read region.
        hsp = HSP(10,
                  readEndInSubject=68,
                  readStartInSubject=58,
                  subjectEnd=66,
                  subjectStart=60)
        adjuster.adjustHSP(hsp)
        self.assertEqual(29, hsp.readEndInSubject)
        self.assertEqual(19, hsp.readStartInSubject)
        self.assertEqual(27, hsp.subjectEnd)
        self.assertEqual(21, hsp.subjectStart)
示例#15
0
 def testFullCoverageCounts(self):
     """
     The coverageCounts method must return the correct result when the title
     is fully covered by its reads.
     """
     hsp1 = HSP(7, subjectStart=0, subjectEnd=5)
     hsp2 = HSP(8, subjectStart=5, subjectEnd=10)
     titleAlignments = TitleAlignments('subject title', 10)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     c = Counter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
     self.assertEqual(c, titleAlignments.coverageCounts())
示例#16
0
    def testOneAlignment(self):
        """
        When one alignment is present that alignment must be returned by
        bestAlignment.
        """
        alignment = Alignment(44, 'Seq 1')
        alignment.addHsp(HSP(10))
        alignment.addHsp(HSP(9))

        alignments = [alignment]
        hit = ReadAlignments(Read('id1', 'aaa'), alignments)
        best = bestAlignment(hit)
        self.assertEqual('Seq 1', best.subjectTitle)
        self.assertEqual(44, best.subjectLength)
示例#17
0
 def testReadCount(self):
     """
     The readCount function must indicate how many reads matched a title.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(14)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(2, titleAlignments.readCount())
示例#18
0
 def testHspCount(self):
     """
     The hspCount function must indicate how many HSPs were found in
     total for all the alignments to a title.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(14)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(3, titleAlignments.hspCount())
示例#19
0
 def testPartialCoverage(self):
     """
     The coverage method must return the correct value when the title is
     partially covered by its reads.
     """
     hsp1 = HSP(7, subjectStart=10, subjectEnd=20)
     hsp2 = HSP(15, subjectStart=30, subjectEnd=40)
     hsp3 = HSP(21, subjectStart=50, subjectEnd=60)
     titleAlignments = TitleAlignments('subject title', 100)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(0.3, titleAlignments.coverage())
示例#20
0
 def testBetterThanTrue(self):
     """
     The hasScoreBetterThan function must return True if there is an HSP
     with a score better than the passed value.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(15)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertTrue(titleAlignments.hasScoreBetterThan(20))
示例#21
0
 def testWorstHsp(self):
     """
     The worstHsp function must return the HSP with the worst score for all
     the HSPs for all the alignments matching a title.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(15)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual(hsp1, titleAlignments.worstHsp())
示例#22
0
 def testReads(self):
     """
     The reads function must return a Reads instance with the reads for
     the title.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(14)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read1 = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read1, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read2 = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read2, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual([read1, read2], list(titleAlignments.reads()))
示例#23
0
 def testHSPs(self):
     """
     The hsps function must produce a list of all HSPs.
     """
     hsp1 = HSP(7)
     hsp2 = HSP(14)
     hsp3 = HSP(21)
     titleAlignments = TitleAlignments('subject title', 55)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual([7, 14, 21],
                      [hsp.score.score for hsp in titleAlignments.hsps()])
示例#24
0
 def testExpectedAttributes(self):
     """
     An HSP must have the expected attributes.
     """
     hsp = HSP(7,
               readStart=1,
               readEnd=2,
               readStartInSubject=3,
               readEndInSubject=4,
               subjectStart=5,
               subjectEnd=6,
               readMatchedSequence='aaa',
               subjectMatchedSequence='ccc',
               readFrame=8,
               subjectFrame=9)
     self.assertEqual(1, hsp.readStart)
     self.assertEqual(2, hsp.readEnd)
     self.assertEqual(3, hsp.readStartInSubject)
     self.assertEqual(4, hsp.readEndInSubject)
     self.assertEqual(5, hsp.subjectStart)
     self.assertEqual(6, hsp.subjectEnd)
     self.assertEqual('aaa', hsp.readMatchedSequence)
     self.assertEqual('ccc', hsp.subjectMatchedSequence)
     self.assertEqual(7, hsp.score.score)
     self.assertEqual(8, hsp.readFrame)
     self.assertEqual(9, hsp.subjectFrame)
示例#25
0
 def testResidueCountsOneReadOneHSPPartialMatch(self):
     """
     The residueCounts method must return the correct result when just one
     read with one HSP is aligned to a title and only part of the read
     matched the subject (all the read bases are still counted and
     returned).
     """
     read = Read('id', 'ACGT')
     hsp = HSP(33,
               readStart=0,
               readEnd=2,
               readStartInSubject=0,
               readEndInSubject=4,
               subjectStart=0,
               subjectEnd=4,
               readMatchedSequence='ACGT',
               subjectMatchedSequence='ACGT')
     titleAlignments = TitleAlignments('subject title', 55)
     titleAlignment = TitleAlignment(read, [hsp])
     titleAlignments.addAlignment(titleAlignment)
     self.assertEqual({
         0: {
             'A': 1
         },
         1: {
             'C': 1
         },
         2: {
             'G': 1
         },
         3: {
             'T': 1
         },
     }, titleAlignments.residueCounts())
示例#26
0
 def testCoverageCountsOverlap(self):
     """
     The coverageCounts method must return the correct results when the
     title is partially covered by its reads that overlap.
     """
     hsp1 = HSP(7, subjectStart=1, subjectEnd=2)
     hsp2 = HSP(15, subjectStart=3, subjectEnd=6)
     hsp3 = HSP(21, subjectStart=5, subjectEnd=6)
     titleAlignments = TitleAlignments('subject title', 10)
     read = Read('id1', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     read = Read('id2', 'AAA')
     titleAlignment = TitleAlignment(read, [hsp3])
     titleAlignments.addAlignment(titleAlignment)
     c = Counter([1, 3, 4, 5, 5])
     self.assertEqual(c, titleAlignments.coverageCounts())
示例#27
0
 def testHsps(self):
     """
     The hsps function must yield all the hsps for all titles in a
     TitlesAlignments instance.
     """
     mockOpener = mock_open(read_data=(
         dumps(PARAMS) + '\n' + dumps(RECORD0) + '\n' +
         dumps(RECORD1) + '\n' + dumps(RECORD2) + '\n'))
     with patch.object(builtins, 'open', mockOpener):
         reads = Reads()
         reads.add(Read('id0', 'A' * 70))
         reads.add(Read('id1', 'A' * 70))
         reads.add(Read('id2', 'A' * 70))
         readsAlignments = DiamondReadsAlignments(reads, 'file.json')
         titlesAlignments = TitlesAlignments(readsAlignments)
         result = list(titlesAlignments.hsps())
         self.assertEqual(
             sorted([HSP(20), HSP(25), HSP(20), HSP(20), HSP(20)]),
             sorted(result))
示例#28
0
 def testCoverageInfoOneReadWithTwoHSPs(self):
     """
     When a title has one read with two HSPs aligned to it, the coverageInfo
     method must return the correct indices and bases from that read.
     """
     titleAlignments = TitleAlignments('subject title', 55)
     hsp1 = HSP(15, subjectStart=1, subjectEnd=4, readMatchedSequence='A-A')
     hsp2 = HSP(10, subjectStart=3, subjectEnd=6, readMatchedSequence='CGT')
     read = Read('id1', 'AAACGT')
     titleAlignment = TitleAlignment(read, [hsp1, hsp2])
     titleAlignments.addAlignment(titleAlignment)
     coverage = titleAlignments.coverageInfo()
     self.assertEqual(
         {
             1: [(15, 'A')],
             2: [(15, '-')],
             3: [(15, 'A'), (10, 'C')],
             4: [(10, 'G')],
             5: [(10, 'T')],
         }, coverage)
示例#29
0
    def testTwoJSONInputsWithSubjectInCommon(self):
        """
        If two JSON files are passed to L{BlastReadsAlignments} with a matched
        subject in common and a TitlesAlignments is made, the title in the
        TitlesAlignments must have information from both reads, including the
        correct HSP scores.
        """
        class SideEffect(object):
            def __init__(self):
                self.first = True

            def sideEffect(self, _ignoredFilename, **kwargs):
                if self.first:
                    self.first = False
                    return File([dumps(PARAMS) + '\n', dumps(RECORD2) + '\n'])
                else:
                    return File([dumps(PARAMS) + '\n', dumps(RECORD4) + '\n'])

        title = 'gi|887699|gb|DQ37780 Cowpox virus 15'

        sideEffect = SideEffect()
        with patch.object(builtins, 'open') as mockMethod:
            mockMethod.side_effect = sideEffect.sideEffect
            reads = Reads()
            reads.add(Read('id2', 'A' * 70))
            reads.add(Read('id4', 'A' * 70))
            readsAlignments = BlastReadsAlignments(
                reads, ['file1.json', 'file2.json'])
            titlesAlignments = TitlesAlignments(readsAlignments)
            titleAlignments = titlesAlignments[title]
            self.assertEqual(title, titleAlignments.subjectTitle)
            self.assertEqual(4, titleAlignments.hspCount())
            self.assertEqual('id2', titleAlignments[0].read.id)
            self.assertEqual('id4', titleAlignments[1].read.id)
            # First matching read has one HSP.
            self.assertEqual(HSP(20), titleAlignments[0].hsps[0])
            # Second matching read has three HSPs.
            self.assertEqual(HSP(10), titleAlignments[1].hsps[0])
            self.assertEqual(HSP(5), titleAlignments[1].hsps[1])
            self.assertEqual(HSP(3), titleAlignments[1].hsps[2])
示例#30
0
    def testCoverageInfoTwoReadsWithThreeHSPs(self):
        """
        When a title has two reads (one with two HSPs, one with one) aligned
        to it, the coverageInfo method must return the correct indices and
        bases from the read.
        """
        titleAlignments = TitleAlignments('subject title', 55)

        # First read.
        hsp1 = HSP(15, subjectStart=1, subjectEnd=4, readMatchedSequence='A-A')
        hsp2 = HSP(10, subjectStart=3, subjectEnd=6, readMatchedSequence='CGT')
        read = Read('id1', 'AAACGT')
        titleAlignment = TitleAlignment(read, [hsp1, hsp2])
        titleAlignments.addAlignment(titleAlignment)

        # Second read.
        hsp1 = HSP(20,
                   subjectStart=5,
                   subjectEnd=10,
                   readMatchedSequence='CGGTA')
        read = Read('id2', 'AAACGTCGGTAAAA')
        titleAlignment = TitleAlignment(read, [hsp1])
        titleAlignments.addAlignment(titleAlignment)

        coverage = titleAlignments.coverageInfo()
        self.assertEqual(
            {
                1: [(15, 'A')],
                2: [(15, '-')],
                3: [(15, 'A'), (10, 'C')],
                4: [(10, 'G')],
                5: [(10, 'T'), (20, 'C')],
                6: [(20, 'G')],
                7: [(20, 'G')],
                8: [(20, 'T')],
                9: [(20, 'A')],
            }, coverage)
示例#31
0
def jsonDictToAlignments(lightDict, database):
    """
    Take a dict of light matter results (converted from JSON) for a single
    read and produce a list of alignments.

    @param lightDict: A C{dict}, created from a line of light matter result
        JSON.
    @param database: A C{light.database.Database} instance.
    @return: A C{list} of L{light.alignment.LightAlignment} instances.
    """
    lightAlignments = []

    for alignment in lightDict['alignments']:
        subject = database.getSubjectByIndex(alignment['subjectIndex'])
        lightAlignment = LightAlignment(len(subject), subject.read.id)
        for hspDict in alignment['hsps']:
            hsp = HSP(hspDict['score'])
            # This is ugly: manually put the additional light matter HSP
            # info onto the HSP instance. For now.
            hsp.hspInfo = hspDict['hspInfo']
            lightAlignment.addHsp(hsp)
        lightAlignments.append(lightAlignment)

    return lightAlignments