pyfastx icon indicating copy to clipboard operation
pyfastx copied to clipboard

Unexpected behavior when a Sequence object is returned from a function

Open EnzoAndree opened this issue 2 years ago • 1 comments

Hello!

I made a trivial function that returns a sequence from a Fasta class expecting it to return an object of the Sequence class.

However, it returns meaningless and different characters every time the script is run.

On the contrary, if I don't use the function, but extract the gene in the main routine, I can handle the Sequence class object as expected.

Is this behavior to be expected or is it a bug?

For the test I used this file https://github.com/lmdu/pyfastx/blob/master/tests/data/test.fa

Here is the script:

import pyfastx
import sys

def extract_gene(file, gene):
    fasta = pyfastx.Fasta(file)
    return fasta[gene]

if __name__ == '__main__':
    print(f'Python version: {sys.version}')
    print(f'pyfastx version: {pyfastx.version()}\n')
    print('Extracting in __main__\n')
    fasta = pyfastx.Fasta('test.fa')
    gene = fasta['JZ822577.1']
    print(gene)
    print('\n\nExtracting in extract_gene function\n')
    gene = extract_gene('test.fa', 'JZ822577.1')
    print(gene)

output is as follows:

Python version: 3.9.1 | packaged by conda-forge | (default, Jan 26 2021, 01:34:10)
[GCC 9.3.0]
pyfastx version: 0.8.4

Extracting in __main__

CTCTAGAGATTACTTCTTCACATTCCAGATCACTCAGGCTCTTTGTCATTTTAGTTTGACTAGGATATCGAGTATTCAAGCTCATCGCTTTTGGTAATCTTTGCGGTGCATGCCTTTGCATGCTGTATTGCTGCTTCATCATCCCCTTTGACTTGTGTGGCGGTGGCAAGACATCCGAAGAGTTAAGCGATGCTTGTCTAGTCAATTTCCCCATGTACAGAATCATTGTTGTCAATTGGTTGTTTCCTTGATGGTGAAGGGGCTTCAATACATGAGTTCCAAACTAACATTTCTTGACTAACACTTGAGGAAGAAGGACAAGGGTCCCCATGT


Extracting in extract_gene function

@B5��@B5��� ��U� ��U@B5�

EnzoAndree avatar Nov 04 '21 22:11 EnzoAndree

Thank you for reporting this issue. This bug was caused by the destroyed index. When the function 'extract_gene' finishes running, the generated Fasta object will be destroyed immediately due to no reference to it, because fasta object created within function is a local variable. Meanwhile, the index will also be destroyed with Fasta object. So, if we use sequence object with the broken index, it will cause a critical error.

Thanks again! I will fix it in next few days.

lmdu avatar Nov 05 '21 06:11 lmdu

We have fixed this issue in new versions >= 0.9.0

lmdu avatar Jan 01 '23 14:01 lmdu