Question: Creating text file as a temporary file in python
0
gravatar for mdsiddra
8 months ago by
mdsiddra30
mdsiddra30 wrote:

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??

biopython python • 407 views
ADD COMMENTlink modified 10 weeks ago by Biostar ♦♦ 20 • written 8 months ago by mdsiddra30
1

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 REPLYlink modified 8 months ago • written 8 months ago by Bastien HervĂ©4.4k

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

ADD REPLYlink written 8 months ago by mdsiddra30
1
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 REPLYlink modified 10 weeks ago • written 10 weeks ago by RamRS23k

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 REPLYlink modified 8 months ago • written 8 months ago by jrj.healey13k

Alright, Thanks so much for this help.

ADD REPLYlink written 8 months ago by mdsiddra30
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 826 users visited in the last hour