4

I am using Python 3.5 on the following code.

def raxml(DIR,cleaned,num_cores,seqtype):
        assert cleaned.endswith(".aln-cln"),\
                "raxml infile "+cleaned+" not ends with .aln-cln"
        assert seqtype == "aa" or seqtype == "dna","Input data type: dna or aa"
        assert len(read_fasta_file(DIR+cleaned)) >= 4,\
                "less than 4 sequences in "+DIR+cleaned
        clusterID = cleaned.split(".")[0]
        tree = DIR+clusterID+".raxml.tre"
        raw_tree = "RAxML_bestTree."+cleaned
        model = "PROTCATWAG" if seqtype == "aa" else "GTRCAT"
        if not os.path.exists(tree) and not os.path.exists(raw_tree):
                # raxml crashes if input file starts with . 
                infasta = cleaned if DIR == "./" else DIR+cleaned
                cmd = ["raxml","-T",str(num_cores),"-p","12345","-s",\
                       infasta,"-n",cleaned,"-m",model]
                print (" ".join(cmd))
                p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
                out = p.communicate()
                assert p.returncode == 0,"Error raxml"+out[0]
        try:
                os.rename(raw_tree,tree)
                os.remove("RAxML_info."+cleaned)
                os.remove("RAxML_log."+cleaned)
                os.remove("RAxML_parsimonyTree."+cleaned)
                os.remove("RAxML_result."+cleaned)
                os.remove(DIR+cleaned+".reduced")
        except: pass # no need to worry about extra intermediate files
        return tree

It runs and returns the following code:

"raxml_wrapper.py", line 30, in raxml
        assert p.returncode == 0,"Error raxml"+out[0]
TypeError: Can't convert 'bytes' object to str implicitly

Initially, I tried the following:

 p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
 p = p.decode('utf-8')
 out = p.communicate()
 assert p.returncode == 0,"Error raxml"+out[0]

That didn't fix the issue at all. I have looked at similar questions, but I cannot come up with a solution to this. I would appreciate some help on this.

Thanks!

4
  • I assume Python3? Commented Apr 15, 2018 at 17:10
  • try binascii ... import binascii and then print(binascii.hexlify(bytes_here).decode()) Commented Apr 15, 2018 at 17:14
  • @Mick_ does binascii covert bytes to string Commented Apr 15, 2018 at 18:21
  • binascii can represent your bytes as string of hex values with your data. It's hard to advise on better solution unless you provide some information on how you intend to use the data. Commented Apr 15, 2018 at 18:33

2 Answers 2

1

p, a Popen object, doesn't have a .decode(...) member.

You need to actually decode the output

p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, _ = p.communicate()
out = out.decode('utf-8')
assert p.returncode == 0, 'Error raxml' + out[0]

That said, this code can be improved to use subprocess.check_output:

# does roughly the same thing, you'll get `subprocess.CalledProcessError` instead of `AssertionError`
out = subprocess.check_output(cmd).decode('UTF-8')

Or if you happen to be using python3.6+

out = subprocess.check_output(cmd, encoding='UTF-8')
Sign up to request clarification or add additional context in comments.

8 Comments

Hello @Anthony I'm still getting an assert error message this time: AssertionError: Error raxml
This is what I have added thus far, to find a solution: out, _ = p.communicate() out = subprocess.check_output(cmd).decode('UTF-8') print (out) assert p.returncode == 0,"Error raxml"+ out[0]
ah, you need to use my solutions separately, the three code blocks are options that do roughly the same thing (don't use both of them!)
My mistake @Anthony I just used the first block as it is. I am still getting the assertion error: Error raxml.
probably means your command is exiting nonzero -- what happens when you run it in a terminal separately? (maybe inspect echo $? after running?)
|
0

I do not know exactly what your p.communicate() method does, but it seems that it returns a byte object as a result. And this piece of code, cannot add this byte object to "Error raxml" str object:

assert p.returncode == 0,"Error raxml"+out[0]

Maybe you should try converting it to str as this:

assert p.returncode == 0,"Error raxml"+str(out[0])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.