user4530515
user4530515

Reputation:

tempfile is not accessible when using subprocess.Popen

When I run the following script, the error"Command line argument error: Argument "query". File is not accessible" occurs. I'm using python 3.4.2.

from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import subprocess
import tempfile
import sys

def main():

    # read name file and put all identifications into a list

    infile_I = open('OTU_name.txt','r')
    name = infile_I.read().split('>')
    infile_I.close()

    # extract sequence segments to a temporary file one at a time
    for i in name:
        i = i.replace('\n','')
        for j in SeqIO.parse("GemSIM_OTU_ids.fa","fasta"):
            if str(i) == str(j.id):
                f = tempfile.NamedTemporaryFile()
                record = j.seq
                f.write(bytes(str(record),'UTF-8'))
                f.seek(0)
                f = f.read().decode()
                Result = subprocess.Popen(['blastn','-remote','-db','chromosome','-query',f,'-out',str(i)],stdout=subprocess.PIPE)
                output = Result.communicate()[0]

if __name__== '__main__': main()

Upvotes: 0

Views: 2307

Answers (1)

Jon Clements
Jon Clements

Reputation: 142236

f = tempfile.NamedTemporaryFile() returns a file-like object which you're trying to provide as a command line argument. Instead, you want the actual filename which is available via its .name attribute - although I'm somewhat confused why you're creating a tempfile, writing to it, seeking back to position 0, then replacing your tempfile f object with the contents of the file? I suspect you don't want to do that replacement and use f.name for your query.

Result = subprocess.Popen(['blastn','-remote','-db','chromosome','-query',f.name,'-out',str(i)],stdout=subprocess.PIPE)

Also, there's some convenient wrapper functions around subprocess.Popen such as subprocess.check_output which are also somewhat more explicit as to your intent which could be used here instead.

Upvotes: 1

Related Questions