Exemplo n.º 1
0
    def testSymmetricFindScoresDifferingSubjectAndQuery(self):
        """
        The score of matching a sequence A against a sequence B must
        be the same as when matching B against A, including when the number
        of hashes in the two differs and the scores are not 1.0.
        """
        subject = AARead('subject', 'AFRRRFRRRFASAASAFRRRFRRRF')
        query = AARead('query', 'FRRRFRRRFASAVVVVVV')
        dbParams1 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks])
        db = Database(dbParams1)
        _, index, _ = db.addSubject(subject)
        hashCount1 = db.getSubjectByIndex(index).hashCount
        findParams = FindParameters(significanceFraction=0.0)
        result = db.find(query, findParams)
        score1 = result.analysis['0']['bestBinScore']

        dbParams2 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks])
        db = Database(dbParams2)
        _, index, _ = db.addSubject(query)
        hashCount2 = db.getSubjectByIndex(index).hashCount
        result = db.find(subject, findParams)
        score2 = result.analysis['0']['bestBinScore']

        self.assertNotEqual(hashCount1, hashCount2)
        self.assertEqual(score1, score2)
        self.assertNotEqual(1.0, score1)
Exemplo n.º 2
0
    def testChecksumAfterSaveRestore(self):
        """
        A database that has a sequence added to it, which is then saved and
        restored, and then has a second sequence is added to it must have the
        same checksum as a database that simply has the two sequences added to
        it without the intervening save/restore.
        """
        seq1 = 'FRRRFRRRFASAASA'
        seq2 = 'MMMMMMMMMFRRRFR'
        dbParams1 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks, Troughs])
        db1 = Database(dbParams1)
        db1.addSubject(AARead('id1', seq1))
        fp = StringIO()
        db1.save(fp)
        fp.seek(0)
        db1 = Database.restore(fp)
        db1.addSubject(AARead('id2', seq2))

        dbParams2 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks, Troughs])
        db2 = Database(dbParams2)
        db2.addSubject(AARead('id1', seq1))
        db2.addSubject(AARead('id2', seq2))

        self.assertEqual(db1.checksum(), db2.checksum())
Exemplo n.º 3
0
    def testSymmetricFindScoresSameSubjectAndQuery(self):
        """
        The score of matching a sequence A against a sequence B must
        be the same as when matching B against A, and that score must
        be 1.0 when the subject and the query are identical.
        """
        sequence = 'AFRRRFRRRFASAASAFRRRFRRRF'
        subject = AARead('subject', sequence)
        query = AARead('query', sequence)
        dbParams = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                      trigPoints=[Peaks])
        db = Database(dbParams)
        db.addSubject(subject)
        findParams = FindParameters(significanceFraction=0.0)
        result = db.find(query, findParams)
        score1 = result.analysis['0']['bestBinScore']

        dbParams = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                      trigPoints=[Peaks])
        db = Database(dbParams)
        db.addSubject(query)
        result = db.find(subject, findParams)
        score2 = result.analysis['0']['bestBinScore']

        self.assertEqual(score1, score2)
        self.assertEqual(1.0, score1)
Exemplo n.º 4
0
 def testFindMatchAfterSaveRestore(self):
     """
     A matching subject found before a save/restore must also be found
     following a database save/restore.
     """
     subject = AARead('subject', 'AFRRRFRRRFASAASAVVVVVVASAVVVASA')
     query = AARead('query', 'FRRRFRRRFASAASAFRRRFRRRFFRRRFRRRFFRRRFRRRF')
     dbParams = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                   trigPoints=[Peaks])
     db1 = Database(dbParams)
     db1.addSubject(subject)
     result = db1.find(query)
     expected = {
         '0': [{
             'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
             'queryTrigPoint': TrigPoint('Peaks', 'P', 10),
             'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
             'subjectTrigPoint': TrigPoint('Peaks', 'P', 11),
         }, {
             'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
             'queryTrigPoint': TrigPoint('Peaks', 'P', 13),
             'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
             'subjectTrigPoint': TrigPoint('Peaks', 'P', 14),
         }]
     }
     self.assertEqual(expected, result.matches)
     fp = StringIO()
     db1.save(fp)
     fp.seek(0)
     db2 = Database.restore(fp)
     result = db2.find(query)
     self.assertEqual(expected, result.matches)
Exemplo n.º 5
0
 def testFindOneMatchingInsignificant(self):
     """
     One matching subject should be found, but is not significant with the
     default value of significanceFraction.
     """
     subject = AARead('subject', 'AFRRRFRRRFASAASAVVVVVVASAVVVASA')
     query = AARead('query', 'FRRRFRRRFASAASAFRRRFRRRFFRRRFRRRFFRRRFRRRF')
     dbParams = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                   trigPoints=[Peaks])
     db = Database(dbParams)
     db.addSubject(subject)
     result = db.find(query)
     self.assertEqual(
         {
             '0': [{
                 'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                 'queryTrigPoint': TrigPoint('Peaks', 'P', 10),
                 'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
                 'subjectTrigPoint': TrigPoint('Peaks', 'P', 11),
             }, {
                 'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                 'queryTrigPoint': TrigPoint('Peaks', 'P', 13),
                 'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
                 'subjectTrigPoint': TrigPoint('Peaks', 'P', 14),
             }]
         }, result.matches)
     self.assertEqual(0, len(list(result.significantSubjects())))
Exemplo n.º 6
0
 def testFindOneMatchingSignificant(self):
     """
     One matching and significant subject must be found if the
     significanceFraction is sufficiently low.
     """
     sequence = 'AFRRRFRRRFASAASA'
     subject = AARead('subject', sequence)
     query = AARead('query', sequence)
     dbParams = DatabaseParameters(landmarks=[AlphaHelix],
                                   trigPoints=[Peaks],
                                   maxDistance=11)
     db = Database(dbParams)
     db.addSubject(subject)
     findParams = FindParameters(significanceFraction=0.0)
     result = db.find(query, findParams)
     self.assertEqual(
         {
             '0': [
                 {
                     'queryLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
                     'queryTrigPoint': TrigPoint('Peaks', 'P', 11),
                     'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9,
                                                 2),
                     'subjectTrigPoint': TrigPoint('Peaks', 'P', 11),
                 },
             ],
         }, result.matches)
Exemplo n.º 7
0
 def testFindOneMatchingSignificantWithSubjectIndicesIncludingIt(self):
     """
     One matching and significant subject must be found, including when a
     non-empty subjectIndices is passed which includes the found index (and
     other non-matched subject indices)
     """
     sequence = 'AFRRRFRRRFASAASA'
     subject = AARead('subject', sequence)
     query = AARead('query', sequence)
     dbParams = DatabaseParameters(landmarks=[AlphaHelix],
                                   trigPoints=[Peaks],
                                   maxDistance=11)
     db = Database(dbParams)
     db.addSubject(subject)
     findParams = FindParameters(significanceFraction=0.0)
     result = db.find(query, findParams, subjectIndices={'0', 'x', 'y'})
     self.assertEqual(
         {
             '0': [
                 {
                     'queryLandmark': Landmark('AlphaHelix', 'A', 1, 9, 2),
                     'queryTrigPoint': TrigPoint('Peaks', 'P', 11),
                     'subjectLandmark': Landmark('AlphaHelix', 'A', 1, 9,
                                                 2),
                     'subjectTrigPoint': TrigPoint('Peaks', 'P', 11),
                 },
             ],
         }, result.matches)
Exemplo n.º 8
0
    def testFindTwoMatchingInSameSubject(self):
        """
        Two matching hashes in the subject must be found correctly.
        """
        sequence = 'FRRRFRRRFASAASA'
        subject = AARead('subject', sequence)
        query = AARead('query', sequence)
        dbParams = DatabaseParameters(landmarks=[AlphaHelix],
                                      trigPoints=[Peaks])
        db = Database(dbParams)
        db.addSubject(subject)
        result = db.find(query)

        self.assertEqual(
            {
                '0': [{
                    'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                    'queryTrigPoint': TrigPoint('Peaks', 'P', 10),
                    'subjectLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                    'subjectTrigPoint': TrigPoint('Peaks', 'P', 10),
                }, {
                    'queryLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                    'queryTrigPoint': TrigPoint('Peaks', 'P', 13),
                    'subjectLandmark': Landmark('AlphaHelix', 'A', 0, 9, 2),
                    'subjectTrigPoint': TrigPoint('Peaks', 'P', 13),
                }]
            }, result.matches)
Exemplo n.º 9
0
 def testFindBug493Minimal(self):
     """
     A minimal failing test case for
     https://github.com/acorg/light-matter/issues/493
     """
     query = SSAARead(
         '2HLA:A',
         'ALKEDLRSWTAADMAAQTTKHKWEAAHVAEQWRAYLEGTCVEWLRRYLENGKETLQRTDAPK'
         'THMTHHAVSDHEATLRCWALSFYPAEITLTWQRDGEDQTQDTELVETRPAGDGTFQKWVAVV',
         'EE-TTSS-EEESSHHHHHHHHHHHHTTTHHHHHHHHHTHHHHHHHHHHHHHHHHHT--B--E'
         'EEEEEEE-SSSEEEEEEEEEEEBSS-EEEEEEETTEEE-TTEEE---EE-SSS-EEEEEEEE')
     subject = SSAARead(
         '3D2U:A',
         'HVLRYGYTGIFDDTSHMTLTVVGIFDGQHFFTYHVQSSDKASSRANGTISWMANVSAAYPTY'
         'PVTHPVVKGGVRNQNDNRAEAFCTSYGFFPGEIQITFIHYGDKVPEDSEPQCNPLLPTLDGT',
         '-EEEEEEEEEESSSS-EEEEEEEEETTEEEEEEEEESS-SSS-EEEE-STHHHHHHHHSTTH'
         '--B--EEEEEEEEEETTEEEEEEEEEEEBSS--EEEEEEESS---TT---EE---EE-TTS-')
     dbParams = DatabaseParameters(landmarks=['PDB ExtendedStrand'],
                                   trigPoints=[],
                                   limitPerLandmark=50,
                                   distanceBase=1.1)
     db = Database(dbParams)
     _, subjectIndex, _ = db.addSubject(subject)
     findParams = FindParameters(significanceFraction=0.01)
     result = db.find(query, findParams, storeFullAnalysis=True)
     significantBins = result.analysis[subjectIndex]['significantBins']
     for binInfo in significantBins:
         normalizeBin(binInfo['bin'], len(query))
Exemplo n.º 10
0
 def testInMemoryDatabaseIsReturned(self):
     """
     Passing a database keyword with an in-memory database results in that
     database being returned.
     """
     original = Database()
     db = DatabaseSpecifier().getDatabaseFromKeywords(database=original)
     self.assertIs(original, db)
Exemplo n.º 11
0
 def testInitialDatabaseHasNoSubjectInfo(self):
     """
     The database must not have any stored subject information if no
     subjects have been added.
     """
     dbParams = DatabaseParameters()
     db = Database(dbParams)
     self.assertEqual([], list(db.getSubjects()))
Exemplo n.º 12
0
 def testAddUnknownBackend(self):
     """
     If an unknown backend is added, a BackendException must be raised.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix])
     db = Database(dbParams)
     error = "^Unknown backend 'name'\.$"
     six.assertRaisesRegex(self, BackendException, error, db.addBackend,
                           'name')
Exemplo n.º 13
0
 def testInitialStatistics(self):
     """
     The database statistics must be initially correct.
     """
     dbParams = DatabaseParameters()
     db = Database(dbParams)
     self.assertEqual(0, db.subjectCount())
     self.assertEqual(0, db.totalResidues())
     self.assertEqual(0, db.totalCoveredResidues())
Exemplo n.º 14
0
 def testOneReadTwoLandmarksStatistics(self):
     """
     If one subject is added, the database statistics must be correct.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     db.addSubject(AARead('id', 'FRRRFRRRFAAAAAAAAAAAAAAFRRRFRRRFRRRF'))
     self.assertEqual(1, db.subjectCount())
     self.assertEqual(36, db.totalResidues())
     self.assertEqual(22, db.totalCoveredResidues())
Exemplo n.º 15
0
 def testAddNewBackend(self):
     """
     When a new backend is added, the returned parameters must be those of
     the database.
     """
     dbParams1 = DatabaseParameters(landmarks=[AlphaHelix],
                                    trigPoints=[Peaks])
     db = Database(dbParams1)
     name, checksum, dbParams2 = db.addBackend()
     self.assertIs(dbParams1, dbParams2)
Exemplo n.º 16
0
 def testGetSubjectByIndexKeyError(self):
     """
     If an unknown subject index is passed to getSubjectByIndex, a KeyError
     must be raised.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     error = "^'xxx'$"
     six.assertRaisesRegex(self, KeyError, error, db.getSubjectByIndex,
                           'xxx')
Exemplo n.º 17
0
 def testGetIndexBySubjectKeyError(self):
     """
     If a non-existent subject is passed to getIndexBySubject, a KeyError
     must be raised.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     error = "^'id'$"
     six.assertRaisesRegex(self, KeyError, error, db.getIndexBySubject,
                           Subject(AARead('id', 'FF')))
Exemplo n.º 18
0
 def testRestoreInvalidJSON(self):
     """
     If a database restore is attempted from a file that does not contain
     valid JSON, a ValueError error must be raised.
     """
     dbParams = DatabaseParameters()
     db = Database(dbParams)
     error = '^Expected object or value$'
     six.assertRaisesRegex(self, ValueError, error, db.restore,
                           StringIO('xxx'))
Exemplo n.º 19
0
 def testGetSubjectBySubject(self):
     """
     If a subject is added, getIndexBySubject must be able to return it
     given an identical subject to look up.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     _, index, _ = db.addSubject(AARead('id', 'FRRRFRRRF'))
     self.assertEqual(
         index, db.getIndexBySubject(Subject(AARead('id', 'FRRRFRRRF'))))
Exemplo n.º 20
0
 def testGetSubjectHashCount(self):
     """
     If a subject is added, getSubjectByIndex must return a Subject
     instance that has the correct hash count.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     subject = AARead('id', 'FRRRFRRRFAFRRRFRRRF')
     _, index, _ = db.addSubject(subject)
     self.assertEqual(1, db.getSubjectByIndex(index).hashCount)
Exemplo n.º 21
0
 def testAddSameSubjectReturnsSameIndex(self):
     """
     If an identical subject is added multiple times, the same subject
     index must be returned.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     self.assertEqual(
         db.addSubject(AARead('id', 'FRRRFRRRF'))[1],
         db.addSubject(AARead('id', 'FRRRFRRRF'))[1])
Exemplo n.º 22
0
 def testReconnectSameNameBackend(self):
     """
     If a backend tries to connect but re-uses an existing backend name,
     a BackendException must be raised.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix])
     db = Database(dbParams)
     name, checksum, dbParams = db.addBackend()
     error = "^Backend %r is already connected\.$" % name
     six.assertRaisesRegex(self, BackendException, error, db.addBackend,
                           name)
Exemplo n.º 23
0
 def testAddSameSubjectLeavesDatabaseSizeTheSame(self):
     """
     If an identical subject is added multiple times, the database size
     does not increase.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     db.addSubject(AARead('id', 'FRRRFRRRF'))
     self.assertEqual(1, db.subjectCount())
     db.addSubject(AARead('id', 'FRRRFRRRF'))
     self.assertEqual(1, db.subjectCount())
Exemplo n.º 24
0
 def testGetSubjectByIndex(self):
     """
     If a subject is added, getSubjectByIndex must be able to return it
     given its string index.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     subject = AARead('id', 'FRRRFRRRF')
     _, index, _ = db.addSubject(subject)
     self.assertEqual(Subject(AARead('id', 'FRRRFRRRF')),
                      db.getSubjectByIndex(index))
Exemplo n.º 25
0
 def testAddSameSubjectReturnsCorrectPreExisting(self):
     """
     If an identical subject is added multiple times, the expected
     pre-existing values must be returned.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     self.assertEqual([False, True], [
         db.addSubject(AARead('id', 'FRRRFRRRF'))[0],
         db.addSubject(AARead('id', 'FRRRFRRRF'))[0]
     ])
Exemplo n.º 26
0
 def testFindWithOneUnreconnectedBackend(self):
     """
     If a database has one unreconnected backend, calling find() must raise
     BackendException with the expected error message.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix])
     db = Database(dbParams)
     db.disconnectedBackends['dummy'] = None
     query = AARead('id', 'AAA')
     error = "^Backend 'dummy' has not reconnected\.$"
     six.assertRaisesRegex(self, BackendException, error, db.find, query)
Exemplo n.º 27
0
 def testOneReadTwoLandmarksGetSubjects(self):
     """
     If one subject with two landmarks (and hence one hash) is added, an
     entry is appended to the database subject info.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     db.addSubject(AARead('id', 'FRRRFRRRFAFRRRFRRRF'))
     subject = list(db.getSubjects())[0]
     read = AARead('id', 'FRRRFRRRFAFRRRFRRRF')
     self.assertEqual(Subject(read, 1), subject)
     self.assertEqual(1, subject.hashCount)
Exemplo n.º 28
0
 def testTwoReadsTwoLandmarksStatistics(self):
     """
     If two identical reads are added, the database statistics must be
     correct.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix], trigPoints=[])
     db = Database(dbParams)
     db.addSubject(AARead('id1', 'FRRRFRRRFAAAAAAAAAAAAAAFRRRFRRRFRRRF'))
     db.addSubject(AARead('id2', 'FRRRFRRRFAAAAAAAAAAAAAAFRRRFRRRFRRRF'))
     self.assertEqual(2, db.subjectCount())
     self.assertEqual(72, db.totalResidues())
     self.assertEqual(44, db.totalCoveredResidues())
Exemplo n.º 29
0
 def testChecksumEmptyDatabase(self):
     """
     The database checksum must be the same as the checksum for its
     parameters plus the default backend name when no subjects have
     been added to the database.
     """
     dbParams = DatabaseParameters()
     expected = Checksum(dbParams.checksum).update([
         Backend.DEFAULT_NAME,
     ])
     db = Database(dbParams)
     self.assertEqual(expected.value, db.checksum())
Exemplo n.º 30
0
 def testFindNoMatching(self):
     """
     A non-matching key must not be found.
     """
     subject = AARead('subject', 'FRRRFRRRFASAASA')
     query = AARead('query', 'FRRR')
     dbParams = DatabaseParameters(landmarks=[AlphaHelix],
                                   trigPoints=[Peaks])
     db = Database(dbParams)
     db.addSubject(subject)
     result = db.find(query)
     self.assertEqual({}, result.matches)