Python Tip #3: Temporary Files

Today I had a problem at work. I was using a tool called NEEDLE, which is command-line based, and requires two files as input. I read somewhere in the documentation that it could receive input from stdin, but since I didn’t manage to figure out how to do it, I tried to provide the tool with files. However, I don’t want my script to populate the folder it is working on with files. Also, if used in a webserver for example, I would have to worry about not having duplicates around. And to delete them in a proper time. So, how to do it?

I found the module tempfile on my first google search.

My script required a file with some data inside. So, I just had to create a temporary file with that data inside. How to do it:

import tempfile

f = tempfile.NamedTemporaryFile()

I chose NamedTempFile because “This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked).”. As I needed to provide a filename to NEEDLE, I needed this.

Then, I wrote to the file, just like a regular file-like object: f.write(data)

And finally, I ran the tool with file.name as argument. The whole script is below:

import tempfile, os # Included in Python

def localPairwiseAlign(modelseq, templateseq, keep):
    """Pairwise Alignment using Needleman-Wunsch Algorithm available LOCALLY"""

    f1 = tempfile.NamedTemporaryFile()
    f1.write('>SEQUENCE\n'+str(modelseq)+'\n')
    f1.flush()

    f2 = tempfile.NamedTemporaryFile()
    f2.write('>SEQUENCE\n'+str(templateseq)+'\n')
    f2.flush()

    needleOutput = os.popen('needle -asequence %s -bsequence %s -gapopen 10.0 -gapextend 0.5 -outfile /dev/stdout -aformat3 markx10' %(f1.name, f2.name)).read()

    f1.close()
    f2.close()

    if keep:
        print "\t## Saving NEEDLE output"
        file = open('NEEDLE.output', 'a')
        file.write(needleOutput)
        file.close()

    return needleOutput

Comments?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s