Whylas  Wang
Whylas Wang

Reputation: 1

BLAST using python subprocess.call about alignment.def and no idea what is wrong

Here are the script I write.

Two question.

First, we need to print the blast result forming XML hit_id, hit_len and hit_def. First two are easy. But hit.def is the same as def. How to avoid it?

Second, we need to run the program on linux. This program should accept two sequence, one is protein and one is nucleic acid by using a optional argument. But I just cannot achieve this function.

It should work when

$ python my_program.py protein.fa tem.txt prot

last one is the argument option. I don't why the python just doesn't accept it as I write

f=sys.argv[3]
f1=str(sys.argv[3])
if f1 is 'prot':
    function(filename,protflag=True)

Python always choose 'else'

# IMPORT
import sys
import subprocess
################################################################################
# FUNCTIONS
#execute BLAST, vailable type are protein and nuclien

    def run_blast(filename,protflag=True):
    """execute BLAST, vailable type are prot or na"""
        if protflag:
            subprocess.call(['blastp','-query','filename','-db','nr','-outfmt','5','-out','prot.xml'])
        else :
            subprocess.call(['blastn','-query','filename','-db','nt','-outfmt','5','-out','na.xml'])



    def read_XML(filename):
        """return hit_id length hit_def"""
        from Bio.Blast import NCBIXML
        name=str(filename)
        record=NCBIXML.read(open(name))
        for i in range(0,len(record.alignments)):
            hit=record.alignments[i]
            print 'hit_id'+hit.id+'\n'+'length'+hit.len+'\n'+'hit_def'+hit.def+'\n'#here is the problem .def


################################################################################
# MAIN


# write a function for the "main method"


    def main(filename=sys.argv[1],protflag=True):
        """excuate BLAST with file then return the hit_id, length and hit_def"""
        f=sys.argv[3]
        f1=str(f)
        if f is 'prot':
            run_blast(filename,True)
            read_XML("prot.xml")
        elif f is 'na':
            run_blast(filename,False)
            read_XML("prot.xml")
        else:
            print 'only prot and na available'


    if __name__ == '__main__':
        # defaults to reading the system, see above :)
        main()

Upvotes: 0

Views: 931

Answers (1)

koukouviou
koukouviou

Reputation: 820

The "else" clause is always chosen because you are using "is" instead of "==" to check the value of sys.argv[3].

From Is there a difference between `==` and `is` in Python?:

is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

You should use

if f1 == 'prot':

Upvotes: 1

Related Questions