def test_finds_genomes_with_name(self): from edge.models import Genome a = Genome(name="Foo") a.save() Genome(name="Bar %s" % a.id).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["name"] for g in d], ["Foo", "Bar %s" % a.id]) # finds genome by ID and query res = self.client.get("%s?q=%s" % (url, a.id)) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertCountEqual([g["name"] for g in d], ["Foo", "Bar %s" % a.id]) # finds one res = self.client.get("%s?q=oo" % url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertCountEqual([g["name"] for g in d], ["Foo"]) # finds none res = self.client.get("%s?q=ooo" % url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertCountEqual([g["name"] for g in d], [])
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)
def test_genome_list_paginates(self): from edge.models import Genome Genome(name="Foo").save() Genome(name="Bar").save() Genome(name="Far").save() Genome(name="Baz").save() url = reverse("genome_list") res = self.client.get(url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 4) res = self.client.get("%s?s=1" % url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 3) res = self.client.get("%s?s=1&p=2" % url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2) res = self.client.get("%s?p=2" % url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2)
def test_genome_list_paginates(self): from edge.models import Genome Genome(name='Foo').save() Genome(name='Bar').save() Genome(name='Far').save() Genome(name='Baz').save() res = self.client.get('/edge/genomes/') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 4) res = self.client.get('/edge/genomes/?s=1') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 3) res = self.client.get('/edge/genomes/?s=1&p=2') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2) res = self.client.get('/edge/genomes/?p=2') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2)
def test_finds_genomes_with_name(self): from edge.models import Genome a = Genome(name='Foo') a.save() Genome(name='Bar %s' % a.id).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['name'] for g in d], ['Foo', 'Bar %s' % a.id]) # finds genome by ID and query res = self.client.get('/edge/genomes/?q=%s' % a.id) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertItemsEqual([g['name'] for g in d], ['Foo', 'Bar %s' % a.id]) # finds one res = self.client.get('/edge/genomes/?q=oo') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertItemsEqual([g['name'] for g in d], ['Foo']) # finds none res = self.client.get('/edge/genomes/?q=ooo') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertItemsEqual([g['name'] for g in d], [])
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)
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) # 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'], 20) self.assertEquals(d[0]['subject_end'], 7)
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)
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)
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)
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)
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)
def build_genome_and_ancestors(self, names): # build a genome of name in names with each as its subsquent genome's parent g_parent = Genome(name=names[0]) g_parent.save() g_child = g_parent for name in names[:-1]: g_child = Genome(name=name) g_child.parent = g_parent g_child.save() g_parent = g_child return g_child
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()
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')
def test_import_feature_ending_at_last_base(self): data = """##gff-version 3 chrI\tTest\tchromosome\t1\t160\t.\t.\t.\tID=i1;Name=f1 chrI\tTest\tcds\t20\t28\t.\t-\t.\tID=i2;Name=f2 chrI\tTest\trbs\t20\t160\t.\t+\t.\tID=i3 ### ##FASTA >chrI CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACACATCCTAACACTACCCTAAC ACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTTACCCTCCATTACCCTGCCTCCACTCGTTACCCTGTCCCAT """ with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f: f.write(data) f.close() genome = Genome.import_gff('Foo', f.name) os.unlink(f.name) # verify chrI fragment chrI = [fr.indexed_fragment() for fr in genome.fragments.all() if fr.name == 'chrI'][0] self.assertEquals(len(chrI.sequence), 160) # verify skips annotation on entire sequence self.assertEquals(len(chrI.annotations()), 2) self.assertEquals(chrI.annotations()[1].base_first, 20) self.assertEquals(chrI.annotations()[1].base_last, 160) self.assertEquals(chrI.annotations()[1].feature.name, 'i3') # no name, loaded ID self.assertEquals(chrI.annotations()[1].feature.strand, 1) self.assertEquals(chrI.annotations()[0].base_first, 20) self.assertEquals(chrI.annotations()[0].base_last, 28) self.assertEquals(chrI.annotations()[0].feature.name, 'f2') self.assertEquals(chrI.annotations()[0].feature.strand, -1)
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)] )
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)
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)])
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)
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)
def test_genome_list_does_not_return_inactive_genomes(self): from edge.models import Genome Genome(name='Foo', active=False).save() Genome(name='Bar', active=False).save() Genome(name='Far').save() Genome(name='Baz').save() res = self.client.get('/edge/genomes/') self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2) self.assertEqual('Foo' in res.content, False) self.assertEqual('Bar' in res.content, False) self.assertEqual('Far' in res.content, True) self.assertEqual('Baz' in res.content, True)
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)
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)
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)
def test_genome_list_does_not_return_inactive_genomes(self): from edge.models import Genome Genome(name="Foo", active=False).save() Genome(name="Bar", active=False).save() Genome(name="Far").save() Genome(name="Baz").save() url = reverse("genome_list") res = self.client.get(url) self.assertEquals(res.status_code, 200) d = json.loads(res.content) self.assertEqual(len(d), 2) self.assertEqual(b"Foo" in res.content, False) self.assertEqual(b"Bar" in res.content, False) self.assertEqual(b"Far" in res.content, True) self.assertEqual(b"Baz" in res.content, True)
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)
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)
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
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, [])
def test_derives_new_genome_with_multiple_fragments(self): g1 = Genome(name="Foo") g1.save() url = reverse("derive-genome-with-new-fragments", kwargs={"genome_id": g1.id}) res = self.client.post( url, data=json.dumps([ { "name": "test-fragment", "sequence": "AGCTAGCTTCGATCGA" }, { "name": "circular-fragment", "sequence": "AGCTAGCTTCGATCGAAGCTATTATATCGATA", "circular": True, }, ]), content_type="application/json", ) self.assertEquals(res.status_code, 201) child = Genome.objects.get(parent=g1.id) self.assertNotEquals(child.id, g1.id) fragments = [{ "id": fragment.id, "uri": "/edge/fragments/{}/".format(fragment.id), "name": fragment.name, "circular": fragment.circular, "parent_id": None, "length": fragment.indexed_fragment().length, } for fragment in child.fragments.all()] self.assertEquals( json.loads(res.content), { "fragments": fragments, "id": child.id, "name": child.name, "notes": None, "parent_id": g1.id, "parent_name": g1.name, "uri": "/edge/genomes/{}/".format(child.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)
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")
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)
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_derives_new_genome_after_adding_fragment(self): g1 = Genome(name="Foo") g1.save() url = reverse("derive-genome-with-new-fragments", kwargs={"genome_id": g1.id}) res = self.client.post( url, data=json.dumps([{ "name": "test-fragment", "sequence": "AGCTAGCTTCGATCGA" }]), content_type="application/json", ) self.assertEquals(res.status_code, 201) child = Genome.objects.get(parent=g1.id) self.assertNotEquals(child.id, g1.id) fragment = child.fragments.first() self.assertEquals( json.loads(res.content), { "fragments": [{ "id": fragment.id, "uri": "/edge/fragments/{}/".format(fragment.id), "name": fragment.name, "circular": fragment.circular, "parent_id": None, "length": 16, }], "id": child.id, "name": child.name, "notes": None, "parent_id": g1.id, "parent_name": g1.name, "uri": "/edge/genomes/{}/".format(child.id), }, )
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)
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'])
def test_import_gff_creates_fragments_and_annotate_features(self): data = """##gff-version 3 chrI\tTest\tchromosome\t1\t160\t.\t.\t.\tID=i1;Name=f1 chrI\tTest\tcds\t30\t80\t.\t-\t.\tID=i2;Name=f2 chrI\tTest\trbs\t20\t28\t.\t+\t.\tID=i3 chrII\tTest\tgene\t40\t60\t.\t-\t.\tID=f4;gene=g4 chrII\tTest\tgene\t20\t80\t.\t+\t.\tID=i5;Name=f5 ### ##FASTA >chrI CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACACATCCTAACACTACCCTAAC ACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTTACCCTCCATTACCCTGCCTCCACTCGTTACCCTGTCCCAT >chrII CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACACATCCTAACACTACCCTAAC ACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTTACCCTCCATTACCCTGCCTCCACTCGTTACCCTGTCCCAT """ with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f: f.write(data) f.close() genome = Genome.import_gff('Foo', f.name) os.unlink(f.name) # created one fragment for each sequence in GFF file self.assertItemsEqual([fr.name for fr in genome.fragments.all()], ['chrI', 'chrII']) # verify chrI fragment chrI = [fr.indexed_fragment() for fr in genome.fragments.all() if fr.name == 'chrI'][0] self.assertEquals(len(chrI.sequence), 160) # verify skips annotation on entire sequence self.assertEquals(len(chrI.annotations()), 2) self.assertEquals(chrI.annotations()[0].base_first, 20) self.assertEquals(chrI.annotations()[0].base_last, 28) self.assertEquals(chrI.annotations()[0].feature.name, 'i3') # no name, loaded ID self.assertEquals(chrI.annotations()[0].feature.strand, 1) self.assertEquals(chrI.annotations()[1].base_first, 30) self.assertEquals(chrI.annotations()[1].base_last, 80) self.assertEquals(chrI.annotations()[1].feature.name, 'f2') self.assertEquals(chrI.annotations()[1].feature.strand, -1) # verify chrII fragment chrII = [fr.indexed_fragment() for fr in genome.fragments.all() if fr.name == 'chrII'][0] self.assertEquals(len(chrII.sequence), 160) # consecutive annotations merged even though they span multiple chunks self.assertEquals(len(chrII.annotations()), 2) self.assertEquals(chrII.annotations()[0].base_first, 20) self.assertEquals(chrII.annotations()[0].base_last, 80) self.assertEquals(chrII.annotations()[0].feature.name, 'f5') self.assertEquals(chrII.annotations()[0].feature.strand, 1) self.assertEquals(chrII.annotations()[1].base_first, 40) self.assertEquals(chrII.annotations()[1].base_last, 60) self.assertEquals(chrII.annotations()[1].feature.name, 'g4') # has gene, use gene name self.assertEquals(chrII.annotations()[1].feature.strand, -1)
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)
def import_gff(name, fn): """ Creates a new genome using the specified GFF file. name: Name of genome fn: path to GFF file """ from edge.models import Genome if Genome.objects.filter(name=name).count() > 0: raise Exception('There is already a genome named "%s"' % (name,)) g = Genome.import_gff(name, fn) return g
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')
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')
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)
def import_with_qualifiers(self, qualifiers): data = """##gff-version 3 chrI\tTest\tcds\t30\t80\t.\t-\t.\t%s ### ##FASTA >chrI CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACACATCCTAACACTACCCTAAC ACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTTACCCTCCATTACCCTGCCTCCACTCGTTACCCTGTCCCAT """ % (qualifiers,) with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f: f.write(data) f.close() self.genome = Genome.import_gff('Foo', f.name) os.unlink(f.name)
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)
def test_aligns_sequence_across_boundry_for_circular_fragment(self): s1 = 'atcggtatctactatgcgtatgcgtcatgattatatatattagcggcatg' 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[-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)
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)])
def test_finds_genomes_with_specified_fragment_ids(self): from edge.models import Genome, Fragment, Genome_Fragment 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, [])
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')
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)])
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)
def setUp(self): self.genome = Genome.create('Test') self.root_sequence = 'agttcgaggctga' self.root = Fragment.create_with_sequence('Foo', self.root_sequence)
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)