Creating text file as a temporary file in python
0
0
Entering edit mode
5.9 years ago
mdsiddra ▴ 30

I have used biopython 1.72 to read from multiple aligned protein data files. I have extracted some required data from the files to other text files which are then used as input to next function. I need to know if I can create that text file to be a temporary file so that as the script ends the file is also destroyed.

Here is the example code I've used so far:

from Bio import SeqIO

with open ("Example.aln" , 'r') as fo:  ### Opening a file from user
    list1 = (fo.read().split("\n\n\n")) ### Splitting the file every 3 newline charc

str1 = ''.join(list1[1])            
list2 = str1.split("\n\n")                 

outfile = open ("sample.txt",'w')
outfile.write("This is a text File displaying information about the given sequence file\n\n")
outfile.write(list2[2])
outfile.close()

fo.close() ## Closing the file taken from user

A clustal format file is taken from user and some of the data is extracted to a text file named 'sample.txt'. This file is then used as an input to another function which opens this text file and performs something else.

What I am asking for is that if I can create this intermediate text file as a temporary file so that it is destroyed as the function exits.

I've tried using the python tempfile Module for this , but it does not work the way I am looking for.

Can I get any help??

Python biopython • 3.0k views
ADD COMMENT
1
Entering edit mode

You can create the sample.txt file then use :

os.remove("sample.txt")

But, if your .aln file is not that big, I suggest you to keep it in a dataframe or in an array in python

NB :

import os
ADD REPLY
0
Entering edit mode

Thankyou for the response. Lemme try this with my file if it works.

ADD REPLY
1
Entering edit mode
with open ("Example.aln" , 'r') as fo:  ### Opening a file from user
    list1 = (fo.read().split("\n\n\n")) ### Splitting the file every 3 newline charc

The above code will produce multiple records only if you have 3 continuous new line characters in the input files, it will not split the file for "every 3 newline characters", it does not keep a count of how many new line characters it has encountered so far.

If you want something of this sort:

line1
line2
line3
SPLIT GOES HERE
line4
line5
line6
SPLIT GOES HERE
line7
line8
line9

the code above is not the way to go. On the other hand, if your input file is like this:

entry1


SPLIT GOES HERE
entry2


SPLIT GOES HERE
entry3


only then would your code work fine. I'm calling the entities above "entry" and not "line" because they have 1 or 2 new-line characters in them but will be counted as one entry.

ADD REPLY
0
Entering edit mode

Your fo.close() line is redundant. When you use a with open() as ..: context manager, the file is closed automatically when you leave the indented block.

This may be why you were getting stuck with tempfile, but without seeing the code I can't say for sure.

More generally I think you can re-write the above code in to:

from Bio import SeqIO

with open("Example.aln" , 'r') as fo, open('sample.txt', 'w') as outfile: 
    list1 = (fo.read().split("\n\n\n"))
    str1 = ''.join(list1[1])            
    list2 = str1.split("\n\n")                 

    outfile.write("This is a text File displaying information about the given sequence file\n\n")
    outfile.write(list2[2])

I dont think you need to make fo.read().split() a tuple generator with the additional ( ) but I'm not sure what your input file looks like.

ADD REPLY
0
Entering edit mode

Alright, Thanks so much for this help.

ADD REPLY

Login before adding your answer.

Traffic: 1801 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6