File size: 2,058 Bytes
ea71194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from io import StringIO
import pytest
from Bio import SeqIO
from app.tools.annotated_sequence import export_genbank
from app.tools.sequence_utilities import SequenceUtilitiesError


def annotation(start, end, strand=1):
    return {'label': 'origin', 'start': start, 'end': end, 'strand': strand, 'type': 'misc_feature'}


def test_roundtrip():
    result = export_genbank('GAATTCAAAA', 'plasmid', True, [annotation(8, 3)], ['EcoRI'])
    record = SeqIO.read(StringIO(result['genbank']), 'genbank')
    assert str(record.seq) == 'GAATTCAAAA'
    assert record.annotations['topology'] == 'circular'
    assert len(record.features) == 2
    assert str(record.features[0].extract(record.seq)) == 'AAAGAA'
    assert record.features[1].qualifiers['label'] == ['EcoRI']


def test_reverse_origin():
    result = export_genbank('GAATTCAAAA', 'plasmid', True, [annotation(8, 3, -1)], [])
    record = SeqIO.read(StringIO(result['genbank']), 'genbank')
    assert str(record.features[0].extract(record.seq)) == 'TTCTTT'
    assert result['features'][0]['sequence'] == 'TTCTTT'


@pytest.mark.parametrize('feature,circular', [(annotation(8, 3), False), (annotation(1, 11), True), (annotation(0, 3), True)])
def test_invalid_coordinates(feature, circular):
    with pytest.raises(SequenceUtilitiesError):
        export_genbank('GAATTCAAAA', 'plasmid', circular, [feature], [])


def test_label_and_determinism():
    bad = annotation(1, 3); bad['label'] = 'bad\nORIGIN'
    with pytest.raises(SequenceUtilitiesError):
        export_genbank('GAATTC', 'plasmid', False, [bad], [])
    assert export_genbank('GAATTC', 'plasmid', False, [], []) == export_genbank('GAATTC', 'plasmid', False, [], [])


@pytest.mark.asyncio
async def test_endpoint():
    from app.routers.seq_tools import AnnotatedSequenceRequest, annotated_genbank_endpoint
    result = await annotated_genbank_endpoint(AnnotatedSequenceRequest(sequence='GAATTC'))
    assert result['validation']['biological_annotations_validated'] is False
    assert result['results']['filename'] == 'BioNexus_DNA.gb'