Пример #1
0
    def test_finds_sequence_on_specified_genome(self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        s2 = 'agcgtcgatgcatgagtcgatcggcagtcgtgtagtcgtcgtatgcgtta'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1)
        f2 = Fragment.create_with_sequence('Baz', s2)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()

        g2 = Genome(name='Far')
        g2.save()
        f3 = Fragment.create_with_sequence('Bar', s1)
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
            os.unlink(fragment_fasta_fn(f2))
            os.unlink(fragment_fasta_fn(f3))
        except:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = s1[6:20] + 'aaaaaaaaa'
        r = blast_genome(g1, 'blastn', query)
        # only returns hit from genome
        self.assertEquals(len(r), 1)
        self.assertEquals(r[0].fragment_id, f1.id)
        self.assertEquals(r[0].query_start, 1)
        self.assertEquals(r[0].query_end, 14)
        self.assertEquals(r[0].subject_start, 7)
        self.assertEquals(r[0].subject_end, 20)
        self.assertEquals(r[0].strand(), 1)
Пример #2
0
    def test_finds_sequence_on_specified_genome(self):
        s1 = get_random_sequence(200)
        s2 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        f2 = Fragment.create_with_sequence("Baz", s2)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()

        g2 = Genome(name="Far")
        g2.save()
        f3 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
            os.unlink(fragment_fasta_fn(f2))
            os.unlink(fragment_fasta_fn(f3))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = s1[6:20] + "aaaaaaaaa"
        r = blast_genome(g1, "blastn", query)
        # only returns hit from genome
        self.assertEquals(len(r), 1)
        self.assertEquals(r[0].fragment_id, f1.id)
        self.assertEquals(r[0].query_start, 1)
        self.assertEquals(r[0].query_end, 14)
        self.assertEquals(r[0].subject_start, 7)
        self.assertEquals(r[0].subject_end, 20)
        self.assertEquals(r[0].strand(), 1)
Пример #3
0
    def test_finds_genomes_with_specified_fragment_ids(self):
        g1 = Genome(name="Foo")
        g1.save()
        g2 = Genome(name="Bar")
        g2.save()
        f1 = Fragment(circular=True, name="FooF1")
        f1.save()
        f2 = Fragment(circular=True, name="FooF2")
        f2.save()
        f3 = Fragment(circular=True, name="FooF3", parent=f2)
        f3.save()
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()
        Genome_Fragment(genome=g2, fragment=f1, inherited=True).save()
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        # no filter, return both genomes
        url = reverse("genome_list")
        res = self.client.get(url)
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertCountEqual([g["id"] for g in d], [g1.id, g2.id])

        # looking for f1 and f2
        res = self.client.get("%s?f=%d&f=%d" % (url, f1.id, f2.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertCountEqual([g["id"] for g in d], [g1.id])

        # looking for f1 and f3
        res = self.client.get("%s?f=%d&f=%d" % (url, f1.id, f3.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertCountEqual([g["id"] for g in d], [g2.id])

        # looking for f2 and f3
        res = self.client.get("%s?f=%d&f=%d" % (url, f2.id, f3.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])

        # looking for f1
        res = self.client.get("%s?f=%d" % (
            url,
            f1.id,
        ))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])

        # bad input, return []
        res = self.client.get("%s?f=[1,2,3]" % url)
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])
Пример #4
0
    def test_finds_genomes_with_specified_fragment_ids(self):
        g1 = Genome(name='Foo')
        g1.save()
        g2 = Genome(name='Bar')
        g2.save()
        f1 = Fragment(circular=True, name='FooF1')
        f1.save()
        f2 = Fragment(circular=True, name='FooF2')
        f2.save()
        f3 = Fragment(circular=True, name='FooF3', parent=f2)
        f3.save()
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()
        Genome_Fragment(genome=g2, fragment=f1, inherited=True).save()
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        # no filter, return both genomes
        res = self.client.get('/edge/genomes/')
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertItemsEqual([g['id'] for g in d], [g1.id, g2.id])

        # looking for f1 and f2
        res = self.client.get('/edge/genomes/?f=%d&f=%d' % (f1.id, f2.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertItemsEqual([g['id'] for g in d], [g1.id])

        # looking for f1 and f3
        res = self.client.get('/edge/genomes/?f=%d&f=%d' % (f1.id, f3.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertItemsEqual([g['id'] for g in d], [g2.id])

        # looking for f2 and f3
        res = self.client.get('/edge/genomes/?f=%d&f=%d' % (f2.id, f3.id))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])

        # looking for f1
        res = self.client.get('/edge/genomes/?f=%d' % (f1.id, ))
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])

        # bad input, return []
        res = self.client.get('/edge/genomes/?f=[1,2,3]')
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(d, [])
Пример #5
0
    def test_blast_finds_sequence_on_specified_genome(self):
        s1 = get_random_sequence(200)
        s2 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        f2 = Fragment.create_with_sequence("Baz", s2)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()

        g2 = Genome(name="Far")
        g2.save()
        f3 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
            os.unlink(fragment_fasta_fn(f2))
            os.unlink(fragment_fasta_fn(f3))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)

        query = s1[6:20] + "aaaaaaaaa"

        res = self.client.post(
            "/edge/genomes/%s/blast/" % g1.id,
            data=json.dumps(dict(program="blastn", query=query)),
            content_type="application/json",
        )
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)

        # only returns hit from genome
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]["fragment_id"], f1.id)
        self.assertEquals(d[0]["query_start"], 1)
        self.assertEquals(d[0]["query_end"], 14)
        self.assertEquals(d[0]["subject_start"], 7)
        self.assertEquals(d[0]["subject_end"], 20)

        # blast in other genome works too
        res = self.client.post(
            "/edge/genomes/%s/blast/" % g2.id,
            data=json.dumps(dict(program="blastn", query=query)),
            content_type="application/json",
        )
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]["fragment_id"], f3.id)
Пример #6
0
    def test_blast_finds_sequence_on_specified_genome(self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        s2 = 'agcgtcgatgcatgagtcgatcggcagtcgtgtagtcgtcgtatgcgtta'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1)
        f2 = Fragment.create_with_sequence('Baz', s2)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()
        Genome_Fragment(genome=g1, fragment=f2, inherited=False).save()

        g2 = Genome(name='Far')
        g2.save()
        f3 = Fragment.create_with_sequence('Bar', s1)
        Genome_Fragment(genome=g2, fragment=f3, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
            os.unlink(fragment_fasta_fn(f2))
            os.unlink(fragment_fasta_fn(f3))
        except:
            pass
        build_all_genome_dbs(refresh=True)

        query = s1[6:20] + 'aaaaaaaaa'

        res = self.client.post('/edge/genomes/%s/blast/' % g1.id,
                               data=json.dumps(
                                   dict(program='blastn', query=query)),
                               content_type='application/json')
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)

        # only returns hit from genome
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]['fragment_id'], f1.id)
        self.assertEquals(d[0]['query_start'], 1)
        self.assertEquals(d[0]['query_end'], 14)
        self.assertEquals(d[0]['subject_start'], 7)
        self.assertEquals(d[0]['subject_end'], 20)

        # blast in other genome works too
        res = self.client.post('/edge/genomes/%s/blast/' % g2.id,
                               data=json.dumps(
                                   dict(program='blastn', query=query)),
                               content_type='application/json')
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]['fragment_id'], f3.id)
Пример #7
0
    def test_blast_aligns_sequence_to_antisense_strand(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)

        query = str(Seq(s1[6:20]).reverse_complement()) + "tttttttttt"

        res = self.client.post(
            "/edge/genomes/%s/blast/" % g1.id,
            data=json.dumps(dict(program="blastn", query=query)),
            content_type="application/json",
        )
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)

        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]["fragment_id"], f1.id)
        self.assertEquals(d[0]["query_start"], 1)
        self.assertEquals(d[0]["query_end"], 14)
        self.assertEquals(d[0]["subject_start"], 20)
        self.assertEquals(d[0]["subject_end"], 7)
Пример #8
0
 def setUp(self):
     self.sequence = "agttcgaggctga"
     self.genome = Genome.create("Foo")
     self.fragment = Fragment.create_with_sequence("Bar", self.sequence)
     Genome_Fragment(genome=self.genome,
                     fragment=self.fragment,
                     inherited=False).save()
Пример #9
0
    def test_blast_aligns_sequence_to_antisense_strand(self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except:
            pass
        build_all_genome_dbs(refresh=True)

        query = str(Seq(s1[6:20]).reverse_complement()) + 'tttttttttt'

        res = self.client.post('/edge/genomes/%s/blast/' % g1.id,
                               data=json.dumps(
                                   dict(program='blastn', query=query)),
                               content_type='application/json')
        self.assertEquals(res.status_code, 200)
        d = json.loads(res.content)

        self.assertEquals(len(d), 1)
        self.assertEquals(d[0]['fragment_id'], f1.id)
        self.assertEquals(d[0]['query_start'], 1)
        self.assertEquals(d[0]['query_end'], 14)
        self.assertEquals(d[0]['subject_start'], 20)
        self.assertEquals(d[0]['subject_end'], 7)
Пример #10
0
 def build_genome(self, circular, *templates):
     g = Genome(name='Foo')
     g.save()
     for seq in templates:
         f = Fragment.create_with_sequence('Bar', seq, circular=circular)
         Genome_Fragment(genome=g, fragment=f, inherited=False).save()
         try:
             os.unlink(fragment_fasta_fn(f))
         except:
             pass
     build_all_genome_dbs(refresh=True)
     return Genome.objects.get(pk=g.id)
Пример #11
0
    def test_builds_genome_db_with_different_names_on_separate_attempts(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        dbname1 = build_genome_db(g1)
        self.assertEquals(dbname1 is None, False)
        self.assertEquals(dbname1, g1.blastdb)

        g1.blastdb = None
        dbname2 = build_genome_db(g1)
        self.assertEquals(dbname2 == dbname1, False)
Пример #12
0
    def test_does_not_return_genomic_fragments(self):
        from edge.models import Genome, Genome_Fragment

        res = self.client.get('/edge/fragments/')
        self.assertEquals(res.status_code, 200)
        self.assertEquals(len(json.loads(res.content)), 1)

        g = Genome(name='Foo')
        g.save()
        Genome_Fragment(genome=g,
                        fragment_id=self.fragment_id,
                        inherited=False).save()

        res = self.client.get('/edge/fragments/')
        self.assertEquals(res.status_code, 200)
        self.assertEquals(len(json.loads(res.content)), 0)
Пример #13
0
    def test_does_not_return_duplicate_hits_for_circular_fragments(self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1, circular=True)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = s1[5:20] + 'tttttttttt'
        r = blast_genome(g1, 'blastn', query)
        self.assertEquals(len(r), 1)
Пример #14
0
    def test_builds_fragment_fastas(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        fn = fragment_fasta_fn(f1)
        try:
            os.unlink(fn)
        except BaseException:
            pass

        fn = build_fragment_fasta(f1)
        records = list(SeqIO.parse(fn, "fasta"))
        self.assertEquals(len(records), 1)
        self.assertEquals(str(records[0].seq), s1)
Пример #15
0
    def test_does_not_return_duplicate_hits_for_circular_fragments(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1, circular=True)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = s1[5:20] + "tttttttttt"
        r = blast_genome(g1, "blastn", query)
        self.assertEquals(len(r), 1)
Пример #16
0
    def test_does_not_align_sequence_across_boundry_for_non_circular_fragment(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1, circular=False)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = (s1[-10:] + s1[0:10]) + "tttttttttt"
        res = blast_genome(g1, "blastn", query)

        for r in res:
            self.assertEquals(r.subject_start > 0 and r.subject_start <= len(s1), True)
            self.assertEquals(r.subject_end > 0 and r.subject_end <= len(s1), True)
Пример #17
0
    def test_aligns_sequence_to_antisense_strand(self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = str(Seq(s1[6:20]).reverse_complement()) + 'tttttttttt'
        r = blast_genome(g1, 'blastn', query)
        self.assertEquals(len(r), 1)
        self.assertEquals(r[0].fragment_id, f1.id)
        self.assertEquals(r[0].query_start, 1)
        self.assertEquals(r[0].query_end, 14)
        self.assertEquals(r[0].subject_start, 20)
        self.assertEquals(r[0].subject_end, 7)
        self.assertEquals(r[0].strand(), -1)
Пример #18
0
    def test_aligns_sequence_to_antisense_strand(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = str(Seq(s1[6:20]).reverse_complement()) + "tttttttttt"
        r = blast_genome(g1, "blastn", query)
        self.assertEquals(len(r), 1)
        self.assertEquals(r[0].fragment_id, f1.id)
        self.assertEquals(r[0].query_start, 1)
        self.assertEquals(r[0].query_end, 14)
        self.assertEquals(r[0].subject_start, 20)
        self.assertEquals(r[0].subject_end, 7)
        self.assertEquals(r[0].strand(), -1)
Пример #19
0
    def test_does_not_align_sequence_across_boundry_for_non_circular_fragment(
            self):
        s1 = 'atcggtatcttctatgcgtatgcgtcatgattatatatattagcggcatg'
        g1 = Genome(name='Foo')
        g1.save()
        f1 = Fragment.create_with_sequence('Bar', s1, circular=False)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = (s1[-10:] + s1[0:10]) + 'tttttttttt'
        res = blast_genome(g1, 'blastn', query)

        for r in res:
            self.assertEquals(
                r.subject_start > 0 and r.subject_start <= len(s1), True)
            self.assertEquals(r.subject_end > 0 and r.subject_end <= len(s1),
                              True)
Пример #20
0
    def test_aligns_sequence_across_boundry_for_circular_fragment(self):
        s1 = get_random_sequence(200)
        g1 = Genome(name="Foo")
        g1.save()
        f1 = Fragment.create_with_sequence("Bar", s1, circular=True)
        Genome_Fragment(genome=g1, fragment=f1, inherited=False).save()

        try:
            os.unlink(fragment_fasta_fn(f1))
        except BaseException:
            pass
        build_all_genome_dbs(refresh=True)
        g1 = Genome.objects.get(pk=g1.id)

        query = (s1[-10:] + s1[0:10]) + "ttttttttttt"
        res = blast_genome(g1, "blastn", query)

        # we are not removing redundant matches when matching across circular
        # boundaries, since blasting across circular boundary of a genome is a
        # rare case. so in this particular case, you will find two results, one
        # for the end of the query at the start of the genome, one for across
        # the circular boundary.

        found = False
        for r in res:
            if r.query_start == 1 and r.query_end == 20:
                self.assertEquals(r.fragment_id, f1.id)
                self.assertEquals(r.query_start, 1)
                self.assertEquals(r.query_end, 20)
                self.assertEquals(r.subject_start, len(s1) - 10 + 1)
                self.assertEquals(r.subject_end, len(s1) + 10)
                self.assertEquals(r.fragment_length, len(s1))
                self.assertEquals(r.strand(), 1)
                found = True
                break
        self.assertEquals(found, True)