Exemplo n.º 1
0
    def test_can_insert_then_insert_and_get_second_insert_only_as_changes(self):
        genome = Genome.create("Foo")
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = "atggcatattcgcagct"
        genome.add_fragment("chrI", s)
        g1 = genome

        # insert
        g_u = g1.update()
        with g_u.update_fragment_by_name("chrI") as f:
            f.insert_bases(3, "gataca")
        g2 = g_u

        # insert again
        g_u = g2.update()
        with g_u.update_fragment_by_name("chrI") as f:
            f.insert_bases(9, "gataca")
        g3 = g_u

        changes = g2.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertCountEqual(
            [c.location for c in changes], [(1, 2), (3, 8), (9, len(s) + 6)]
        )

        changes = g3.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertCountEqual(
            [c.location for c in changes], [(3, 8), (9, 14), (15, len(s) + 6 + 6)]
        )
Exemplo n.º 2
0
    def test_find_annotation_by_qualifier(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f = genome.add_fragment('chrI', s)
        f.annotate(3,
                   8,
                   'Foo gene',
                   'gene',
                   1,
                   qualifiers=dict(foo='bar,baz', far='foo,fu'))

        # finds bar
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            'bar')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            'bAr')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # does not find bar,b
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            'bar,b')
        self.assertEquals(len(annotations), 0)

        # can limit to a field
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            'bar', fields=['foo'])
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # bar in qualifier, but not in 'far' field
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            'bar', fields=['far'])
        self.assertEquals(len(annotations), 0)

        # can search in multiple fields
        annotations = \
            genome.indexed_genome().find_annotation_by_qualifier('bar', fields=['far', 'foo'])
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')
Exemplo n.º 3
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()
Exemplo n.º 4
0
    def test_can_insert_then_insert_and_get_second_insert_only_as_changes(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)
        g1 = genome

        # insert
        g_u = g1.update()
        with g_u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
        g2 = g_u

        # insert again
        g_u = g2.update()
        with g_u.update_fragment_by_name('chrI') as f:
            f.insert_bases(9, 'gataca')
        g3 = g_u

        changes = g2.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertItemsEqual([c.location for c in changes], [(1, 2), (3, 8), (9, len(s) + 6)])

        changes = g3.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertItemsEqual([c.location for c in changes], [
                              (3, 8), (9, 14), (15, len(s) + 6 + 6)])
Exemplo n.º 5
0
    def test_find_annotation_by_name(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f = genome.add_fragment('chrI', s)
        f.annotate(3, 8, 'Foo gene', 'gene', 1)

        annotations = genome.indexed_genome().find_annotation_by_name('Foo gene')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_name('foo gene')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        annotations = genome.indexed_genome().find_annotation_by_name('Foo bar')
        self.assertEquals(len(annotations), 0)
Exemplo n.º 6
0
    def test_find_annotation_by_name(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f = genome.add_fragment('chrI', s)
        f.annotate(3, 8, 'Foo gene', 'gene', 1)

        annotations = genome.indexed_genome().find_annotation_by_name(
            'Foo gene')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_name(
            'foo gene')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        annotations = genome.indexed_genome().find_annotation_by_name(
            'Foo bar')
        self.assertEquals(len(annotations), 0)
Exemplo n.º 7
0
    def test_find_annotation_by_name(self):
        genome = Genome.create("Foo")
        s = "atggcatattcgcagct"
        f = genome.add_fragment("chrI", s)
        f.annotate(3, 8, "Foo gene", "gene", 1)

        annotations = genome.indexed_genome().find_annotation_by_name("Foo gene")
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_name("foo gene")
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")

        annotations = genome.indexed_genome().find_annotation_by_name("Foo bar")
        self.assertEquals(len(annotations), 0)
Exemplo n.º 8
0
    def on_post(self, request):
        genome_parser = RequestParser()
        genome_parser.add_argument('name', field_type=str, required=True, location='json')
        genome_parser.add_argument('notes', field_type=str, location='json')

        args = genome_parser.parse_args(request)
        genome = Genome.create(name=args['name'], notes=args['notes'])
        return GenomeView.to_dict(genome), 201
Exemplo n.º 9
0
 def test_add_fragments_to_genome_in_place(self):
     genome = Genome.create('Foo')
     self.assertEquals(len(genome.fragments.all()), 0)
     s = 'atggcatattcgcagct'
     genome.add_fragment('chrI', s)
     self.assertEquals(len(genome.fragments.all()), 1)
     self.assertEquals(genome.fragments.all()[0].name, 'chrI')
     self.assertEquals(genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 10
0
 def test_add_fragments_to_genome_in_place(self):
     genome = Genome.create("Foo")
     self.assertEquals(len(genome.fragments.all()), 0)
     s = "atggcatattcgcagct"
     genome.add_fragment("chrI", s)
     self.assertEquals(len(genome.fragments.all()), 1)
     self.assertEquals(genome.fragments.all()[0].name, "chrI")
     self.assertEquals(genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 11
0
    def test_user_defined_fragments_does_not_include_genomic_fragment(self):
        genome = Genome.create("Foo")
        s = "atggcatattcgcagct"
        genome.add_fragment("chrI", s)

        f = Fragment.create_with_sequence("Bar", "aacctaaaattataa")
        self.assertEquals(len(Fragment.user_defined_fragments()), 1)
        self.assertEquals(Fragment.user_defined_fragments()[0].name, "Bar")
        self.assertEquals(Fragment.user_defined_fragments()[0].id, f.id)
    def test_user_defined_fragments_does_not_include_genomic_fragment(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        f = Fragment.create_with_sequence('Bar', 'aacctaaaattataa')
        self.assertEquals(len(Fragment.user_defined_fragments()), 1)
        self.assertEquals(Fragment.user_defined_fragments()[0].name, 'Bar')
        self.assertEquals(Fragment.user_defined_fragments()[0].id, f.id)
Exemplo n.º 13
0
 def test_add_fragments_to_genome_in_place(self):
     genome = Genome.create('Foo')
     self.assertEquals(len(genome.fragments.all()), 0)
     s = 'atggcatattcgcagct'
     genome.add_fragment('chrI', s)
     self.assertEquals(len(genome.fragments.all()), 1)
     self.assertEquals(genome.fragments.all()[0].name, 'chrI')
     self.assertEquals(
         genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 14
0
    def test_user_defined_fragments_does_not_include_genomic_fragment(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        f = Fragment.create_with_sequence('Bar', 'aacctaaaattataa')
        self.assertEquals(len(Fragment.user_defined_fragments()), 1)
        self.assertEquals(Fragment.user_defined_fragments()[0].name, 'Bar')
        self.assertEquals(Fragment.user_defined_fragments()[0].id, f.id)
Exemplo n.º 15
0
    def test_find_annotation_by_qualifier(self):
        genome = Genome.create("Foo")
        s = "atggcatattcgcagct"
        f = genome.add_fragment("chrI", s)
        f.annotate(
            3, 8, "Foo gene", "gene", 1, qualifiers=dict(foo="bar,baz", far="foo,fu")
        )

        # finds bar
        annotations = genome.indexed_genome().find_annotation_by_qualifier("bar")
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_qualifier("bAr")
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")

        # does not find bar,b
        annotations = genome.indexed_genome().find_annotation_by_qualifier("bar,b")
        self.assertEquals(len(annotations), 0)

        # can limit to a field
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            "bar", fields=["foo"]
        )
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")

        # bar in qualifier, but not in 'far' field
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            "bar", fields=["far"]
        )
        self.assertEquals(len(annotations), 0)

        # can search in multiple fields
        annotations = genome.indexed_genome().find_annotation_by_qualifier(
            "bar", fields=["far", "foo"]
        )
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, "Foo gene")
Exemplo n.º 16
0
    def test_get_changed_locations_by_fragment(self):
        genome = Genome.create("Foo")
        self.assertEquals(len(genome.fragments.all()), 0)

        s0 = "atggcatattcgcagct"
        s1 = "gtacggctagtcgatt"
        s2 = "acgatcgggattgagtcgattc"

        # add initial sequence
        f1 = genome.add_fragment("chrI", s0 + s1 + s2)
        f2 = genome.add_fragment("chrII", s0 + s1 + s2)

        # annotate it to break it up into chunks
        with genome.annotate_fragment_by_name("chrI") as f:
            f.annotate(1, len(s0), "F1", "feature", 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), "F2", "feature", 1)
            f.annotate(
                len(s0) + len(s1) + 1, len(s0) + len(s1) + len(s2), "F3", "feature", 1
            )

        with genome.annotate_fragment_by_name("chrII") as f:
            f.annotate(1, len(s0), "F1", "feature", 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), "F2", "feature", 1)
            f.annotate(
                len(s0) + len(s1) + 1, len(s0) + len(s1) + len(s2), "F3", "feature", 1
            )

        # insert
        u = genome.update()
        with u.update_fragment_by_name("chrI") as f:
            f.insert_bases(3, "gataca")
            f1 = f
        with u.update_fragment_by_name("chrII") as f:
            f.insert_bases(3, "gataca")
            f.insert_bases(6 + len(s0) + len(s1) + 2, "gataca")
            f2 = f

        g2 = Genome.objects.get(pk=u.pk)
        changes = g2.indexed_genome().changed_locations_by_fragment()
        for f in changes:
            if f.id == f1.id:
                self.assertEquals(changes[f], [[1, len(s0) + 6]])
            elif f.id == f2.id:
                self.assertEquals(
                    changes[f],
                    [
                        [1, len(s0) + 6],
                        [
                            len(s0) + len(s1) + 6 + 1,
                            len(s0) + 6 + len(s1) + len(s2) + 6,
                        ],
                    ],
                )
            else:
                raise Exception("Unexpected fragment")
Exemplo n.º 17
0
    def on_post(self, request):
        genome_parser = RequestParser()
        genome_parser.add_argument('name',
                                   field_type=str,
                                   required=True,
                                   location='json')
        genome_parser.add_argument('notes', field_type=str, location='json')

        args = genome_parser.parse_args(request)
        genome = Genome.create(name=args['name'], notes=args['notes'])
        return GenomeView.to_dict(genome), 201
Exemplo n.º 18
0
    def test_can_update_fragment_by_id_and_assign_new_name(self):
        genome = Genome.create("Foo")
        f0 = genome.add_fragment("chrI", "atggcatattcgcagct")
        self.assertCountEqual([f.name for f in genome.fragments.all()], ["chrI"])

        # insert
        g = genome.update()
        with g.update_fragment_by_fragment_id(f0.id, "foobar") as f:
            f.insert_bases(3, "gataca")

        self.assertCountEqual([fr.name for fr in g.fragments.all()], ["foobar"])
Exemplo n.º 19
0
    def test_can_update_fragment_by_id_and_assign_new_name(self):
        genome = Genome.create('Foo')
        f0 = genome.add_fragment('chrI', 'atggcatattcgcagct')
        self.assertItemsEqual([f.name for f in genome.fragments.all()], ['chrI'])

        # insert
        g = genome.update()
        with g.update_fragment_by_fragment_id(f0.id, 'foobar') as f:
            f.insert_bases(3, 'gataca')

        self.assertItemsEqual([fr.name for fr in g.fragments.all()], ['foobar'])
Exemplo n.º 20
0
    def on_post(self, request):
        genome_parser = RequestParser()
        genome_parser.add_argument("name",
                                   field_type=str,
                                   required=True,
                                   location="json")
        genome_parser.add_argument("notes", field_type=str, location="json")

        args = genome_parser.parse_args(request)
        genome = Genome.create(name=args["name"], notes=args["notes"])
        return GenomeView.to_dict(genome), 201
Exemplo n.º 21
0
    def test_update_fragment_by_id(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f1 = genome.add_fragment('chrI', s)

        g2 = genome.update()
        with g2.update_fragment_by_fragment_id(f1.id) as f2:
            f2.insert_bases(3, 'gataca')

        self.assertEquals(g2.fragments.all()[0].indexed_fragment(
        ).sequence, s[0:2] + 'gataca' + s[2:])
        self.assertEquals(genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 22
0
    def test_update_fragment_by_id(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f1 = genome.add_fragment('chrI', s)

        g2 = genome.update()
        with g2.update_fragment_by_fragment_id(f1.id) as f2:
            f2.insert_bases(3, 'gataca')

        self.assertEquals(g2.fragments.all()[0].indexed_fragment().sequence,
                          s[0:2] + 'gataca' + s[2:])
        self.assertEquals(
            genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 23
0
    def test_update_fragment_by_id(self):
        genome = Genome.create("Foo")
        s = "atggcatattcgcagct"
        f1 = genome.add_fragment("chrI", s)

        g2 = genome.update()
        with g2.update_fragment_by_fragment_id(f1.id) as f2:
            f2.insert_bases(3, "gataca")

        self.assertEquals(
            g2.fragments.all()[0].indexed_fragment().sequence, s[0:2] + "gataca" + s[2:]
        )
        self.assertEquals(genome.fragments.all()[0].indexed_fragment().sequence, s)
Exemplo n.º 24
0
    def test_can_update_fragment_by_id_and_assign_new_name(self):
        genome = Genome.create('Foo')
        f0 = genome.add_fragment('chrI', 'atggcatattcgcagct')
        self.assertItemsEqual([f.name for f in genome.fragments.all()],
                              ['chrI'])

        # insert
        g = genome.update()
        with g.update_fragment_by_fragment_id(f0.id, 'foobar') as f:
            f.insert_bases(3, 'gataca')

        self.assertItemsEqual([fr.name for fr in g.fragments.all()],
                              ['foobar'])
Exemplo n.º 25
0
    def test_get_changed_locations_by_fragment(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        s0 = 'atggcatattcgcagct'
        s1 = 'gtacggctagtcgatt'
        s2 = 'acgatcgggattgagtcgattc'

        # add initial sequence
        f1 = genome.add_fragment('chrI', s0 + s1 + s2)
        f2 = genome.add_fragment('chrII', s0 + s1 + s2)

        # annotate it to break it up into chunks
        with genome.annotate_fragment_by_name('chrI') as f:
            f.annotate(1, len(s0), 'F1', 'feature', 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), 'F2', 'feature', 1)
            f.annotate(
                len(s0) + len(s1) + 1,
                len(s0) + len(s1) + len(s2), 'F3', 'feature', 1)

        with genome.annotate_fragment_by_name('chrII') as f:
            f.annotate(1, len(s0), 'F1', 'feature', 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), 'F2', 'feature', 1)
            f.annotate(
                len(s0) + len(s1) + 1,
                len(s0) + len(s1) + len(s2), 'F3', 'feature', 1)

        # insert
        u = genome.update()
        with u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
            f1 = f
        with u.update_fragment_by_name('chrII') as f:
            f.insert_bases(3, 'gataca')
            f.insert_bases(6 + len(s0) + len(s1) + 2, 'gataca')
            f2 = f

        g2 = Genome.objects.get(pk=u.pk)
        changes = g2.indexed_genome().changed_locations_by_fragment()
        for f in changes:
            if f.id == f1.id:
                self.assertEquals(changes[f], [[1, len(s0) + 6]])
            elif f.id == f2.id:
                self.assertEquals(changes[f],
                                  [[1, len(s0) + 6],
                                   [
                                       len(s0) + len(s1) + 6 + 1,
                                       len(s0) + 6 + len(s1) + len(s2) + 6
                                   ]])
            else:
                raise Exception('Unexpected fragment')
Exemplo n.º 26
0
    def test_can_set_name_on_update(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        # insert
        u = genome.update(name=u'Bar bar')
        with u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
        u = Genome.objects.get(pk=u.pk)
        self.assertEquals(u.name, u'Bar bar')
Exemplo n.º 27
0
    def test_can_set_name_on_update(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        # insert
        u = genome.update(name=u'Bar bar')
        with u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
        u = Genome.objects.get(pk=u.pk)
        self.assertEquals(u.name, u'Bar bar')
Exemplo n.º 28
0
    def test_can_set_name_on_update(self):
        genome = Genome.create("Foo")
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = "atggcatattcgcagct"
        genome.add_fragment("chrI", s)

        # insert
        u = genome.update(name=u"Bar bar")
        with u.update_fragment_by_name("chrI") as f:
            f.insert_bases(3, "gataca")
        u = Genome.objects.get(pk=u.pk)
        self.assertEquals(u.name, u"Bar bar")
Exemplo n.º 29
0
    def test_find_annotation_by_qualifier(self):
        genome = Genome.create('Foo')
        s = 'atggcatattcgcagct'
        f = genome.add_fragment('chrI', s)
        f.annotate(3, 8, 'Foo gene', 'gene', 1, qualifiers=dict(foo='bar,baz', far='foo,fu'))

        # finds bar
        annotations = genome.indexed_genome().find_annotation_by_qualifier('bar')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # case insensitive
        annotations = genome.indexed_genome().find_annotation_by_qualifier('bAr')
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # does not find bar,b
        annotations = genome.indexed_genome().find_annotation_by_qualifier('bar,b')
        self.assertEquals(len(annotations), 0)

        # can limit to a field
        annotations = genome.indexed_genome().find_annotation_by_qualifier('bar', fields=['foo'])
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')

        # bar in qualifier, but not in 'far' field
        annotations = genome.indexed_genome().find_annotation_by_qualifier('bar', fields=['far'])
        self.assertEquals(len(annotations), 0)

        # can search in multiple fields
        annotations = \
            genome.indexed_genome().find_annotation_by_qualifier('bar', fields=['far', 'foo'])
        self.assertEquals(len(annotations), 1)
        self.assertEquals(f.id in annotations, True)
        self.assertEquals(len(annotations[f.id]), 1)
        self.assertEquals(annotations[f.id][0].base_first, 3)
        self.assertEquals(annotations[f.id][0].base_last, 8)
        self.assertEquals(annotations[f.id][0].feature.name, 'Foo gene')
Exemplo n.º 30
0
    def test_add_fragments_to_genome_not_in_place_creates_and_updates_child(self):
        parent = Genome.create('Foo')
        self.assertEquals(len(Genome.objects.all()), 1)
        u = parent.update()
        s = 'atggcatattcgcagct'
        u.add_fragment('chrI', s)

        child = u
        # created a child genome
        self.assertItemsEqual([g.id for g in Genome.objects.all()], [parent.id, child.id])
        # child genome points to parent genome
        self.assertEquals(child.parent.id, parent.id)
        # child genome has changes, parent does not
        self.assertEquals(len(child.fragments.all()), 1)
        self.assertEquals(len(parent.fragments.all()), 0)
Exemplo n.º 31
0
    def test_add_fragments_to_genome_not_in_place_creates_and_updates_child(self):
        parent = Genome.create("Foo")
        self.assertEquals(len(Genome.objects.all()), 1)
        u = parent.update()
        s = "atggcatattcgcagct"
        u.add_fragment("chrI", s)

        child = u
        # created a child genome
        self.assertCountEqual(
            [g.id for g in Genome.objects.all()], [parent.id, child.id]
        )
        # child genome points to parent genome
        self.assertEquals(child.parent.id, parent.id)
        # child genome has changes, parent does not
        self.assertEquals(len(child.fragments.all()), 1)
        self.assertEquals(len(parent.fragments.all()), 0)
Exemplo n.º 32
0
    def test_can_insert_and_get_changes(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        # insert
        g_u = genome.update()
        with g_u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
        g = g_u

        changes = g.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertItemsEqual([c.location for c in changes], [(1, 2), (3, 8), (9, len(s) + 6)])
Exemplo n.º 33
0
    def test_get_changed_locations_by_fragment(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        s0 = 'atggcatattcgcagct'
        s1 = 'gtacggctagtcgatt'
        s2 = 'acgatcgggattgagtcgattc'

        # add initial sequence
        f1 = genome.add_fragment('chrI', s0 + s1 + s2)
        f2 = genome.add_fragment('chrII', s0 + s1 + s2)

        # annotate it to break it up into chunks
        with genome.annotate_fragment_by_name('chrI') as f:
            f.annotate(1, len(s0), 'F1', 'feature', 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), 'F2', 'feature', 1)
            f.annotate(len(s0) + len(s1) + 1, len(s0) + len(s1) + len(s2), 'F3', 'feature', 1)

        with genome.annotate_fragment_by_name('chrII') as f:
            f.annotate(1, len(s0), 'F1', 'feature', 1)
            f.annotate(len(s0) + 1, len(s0) + len(s1), 'F2', 'feature', 1)
            f.annotate(len(s0) + len(s1) + 1, len(s0) + len(s1) + len(s2), 'F3', 'feature', 1)

        # insert
        u = genome.update()
        with u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
            f1 = f
        with u.update_fragment_by_name('chrII') as f:
            f.insert_bases(3, 'gataca')
            f.insert_bases(6 + len(s0) + len(s1) + 2, 'gataca')
            f2 = f

        g2 = Genome.objects.get(pk=u.pk)
        changes = g2.indexed_genome().changed_locations_by_fragment()
        for f in changes:
            if f.id == f1.id:
                self.assertEquals(changes[f], [[1, len(s0) + 6]])
            elif f.id == f2.id:
                self.assertEquals(
                    changes[f],
                    [[1, len(s0) + 6],
                     [len(s0) + len(s1) + 6 + 1, len(s0) + 6 + len(s1) + len(s2) + 6]])
            else:
                raise Exception('Unexpected fragment')
Exemplo n.º 34
0
    def test_can_remove_and_get_changes_back(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)
        g = genome

        # remove
        g_u = g.update()
        with g_u.update_fragment_by_name('chrI') as f:
            f.remove_bases(3, 4)
        g = g_u

        changes = g.indexed_genome().changes()
        self.assertEquals(len(changes), 2)
        self.assertItemsEqual([c.location for c in changes], [(1, 2), (3, len(s) - 4)])
Exemplo n.º 35
0
    def test_can_remove_and_get_changes_back(self):
        genome = Genome.create("Foo")
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = "atggcatattcgcagct"
        genome.add_fragment("chrI", s)
        g = genome

        # remove
        g_u = g.update()
        with g_u.update_fragment_by_name("chrI") as f:
            f.remove_bases(3, 4)
        g = g_u

        changes = g.indexed_genome().changes()
        self.assertEquals(len(changes), 2)
        self.assertCountEqual([c.location for c in changes], [(1, 2), (3, len(s) - 4)])
Exemplo n.º 36
0
    def test_can_insert_and_get_changes(self):
        genome = Genome.create('Foo')
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = 'atggcatattcgcagct'
        genome.add_fragment('chrI', s)

        # insert
        g_u = genome.update()
        with g_u.update_fragment_by_name('chrI') as f:
            f.insert_bases(3, 'gataca')
        g = g_u

        changes = g.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertItemsEqual([c.location for c in changes], [(1, 2), (3, 8),
                                                              (9, len(s) + 6)])
Exemplo n.º 37
0
    def test_can_insert_and_get_changes(self):
        genome = Genome.create("Foo")
        self.assertEquals(len(genome.fragments.all()), 0)

        # add initial sequence
        s = "atggcatattcgcagct"
        genome.add_fragment("chrI", s)

        # insert
        g_u = genome.update()
        with g_u.update_fragment_by_name("chrI") as f:
            f.insert_bases(3, "gataca")
        g = g_u

        changes = g.indexed_genome().changes()
        self.assertEquals(len(changes), 3)
        self.assertCountEqual(
            [c.location for c in changes], [(1, 2), (3, 8), (9, len(s) + 6)]
        )
Exemplo n.º 38
0
 def setUp(self):
     self.genome = Genome.create("Test")
     self.root_sequence = "agttcgaggctga"
     self.root = Fragment.create_with_sequence("Foo", self.root_sequence)
Exemplo n.º 39
0
 def setUp(self):
     self.genome = Genome.create('Test')
     self.root_sequence = 'agttcgaggctga'
     self.root = Fragment.create_with_sequence('Foo', self.root_sequence)
Exemplo n.º 40
0
 def test_changes_return_empty_array_if_no_parent(self):
     genome = Genome.create('Foo')
     self.assertEquals(len(genome.fragments.all()), 0)
     s = 'atggcatattcgcagct'
     genome.add_fragment('chrI', s)
     self.assertEquals(genome.indexed_genome().changes(), [])
Exemplo n.º 41
0
 def test_create_genome(self):
     self.assertEquals(len(Genome.objects.all()), 0)
     genome = Genome.create('Foo')
     self.assertEquals(len(Genome.objects.all()), 1)
     self.assertEquals(Genome.objects.all()[0].name, 'Foo')
     self.assertEquals(Genome.objects.all()[0].id, genome.id)
Exemplo n.º 42
0
 def test_changes_return_empty_array_if_no_parent(self):
     genome = Genome.create("Foo")
     self.assertEquals(len(genome.fragments.all()), 0)
     s = "atggcatattcgcagct"
     genome.add_fragment("chrI", s)
     self.assertEquals(genome.indexed_genome().changes(), [])
Exemplo n.º 43
0
#
# Example:
# python split_gff.py GRCh38 GRCh38_latest_genomic.gff GRCh38_latest_genomic.fna
# in docker, run
# time scripts/_python example/import_multi.py 'GRCh38.2' example/GRCh38/*combined.gff
# time scripts/_python example/import_multi.py GCA_003668045.2 example/GCA_003668045.2/*combined.gff

import django

django.setup()

import sys
from edge.models import Genome
from edge.importer import GFFImporter

name = sys.argv[1]
fns = sys.argv[2:]
"""
if Genome.objects.filter(name=name).count() > 0:
  raise Exception('There is already a genome named "%s"' % (name,))
"""

if Genome.objects.filter(name=name).count() > 0:
    genome = Genome.objects.get(name=name)
else:
    genome = Genome.create(name)

for gff_fasta_fn in fns:
    print(gff_fasta_fn)
    GFFImporter(genome, gff_fasta_fn).do_import()
Exemplo n.º 44
0
 def test_create_genome(self):
     self.assertEquals(len(Genome.objects.all()), 0)
     genome = Genome.create("Foo")
     self.assertEquals(len(Genome.objects.all()), 1)
     self.assertEquals(Genome.objects.all()[0].name, "Foo")
     self.assertEquals(Genome.objects.all()[0].id, genome.id)