0

I am using the call() function from a Python script to preform a command from an external software. I want to store the output of the function into a variable and work with the variable. But, the output value is not getting stored into the variable at all. When I give the command to print variable, it prints 0 which is the default value. The code is:

from subprocess inport call
print "\n Summarizing mutations"
summary_mutation = call(["halSummarizeMutations", hal_output])
print summary_mutation

The hal_output is the input file given to command halSummarizeMutations. The output I am getting is:

GenomeName, ParentName, BranchLength, GenomeLength, ParentLength, Subtitutions, Transitions, Transversions, Matches, GapInsertions, GapInsertedBases, GapDeletions, GapDeletedBases, Insertions, InsertionBases, Deletions, DeletionBases, Inversions, InvertedBases, Duplications, DuplicatedBases, Transpositions, TranspositionBases, Other
ancestral_sequences, homo_sapiens, 1, 225206, 248956422, 149218, 49494, 99724, 49433, 10, 698, 0, 0, 17, 20539, 10, 4345, 1343, 134907, 672, 57699, 0, 0, 0
gorilla_gorilla, homo_sapiens, 1, 229507203, 248956422, 44226, 13434, 26644, 15224, 68, 593, 120, 6319, 15, 229443572, 6, 857, 0, 0, 0, 0, 0, 0, 10
papio_anubis, homo_sapiens, 1, 220367699, 248956422, 98187, 32113, 64759, 32284, 6, 1418, 11, 245, 36, 220231921, 30, 7448, 791, 109496, 0, 0, 0, 0, 0
Total, ,3, 450100108, 746869266, 291631, 95041, 191127, 96941, 84, 2709, 131, 6564, 68, 449696032, 46, 12650, 2134, 244403, 672, 57699, 0, 0, 0
Average, ,1, 150033369, 248956422, 97210, 31680, 63709, 32313, 28, 903, 43, 2188, 22, 149898677, 15, 4216, 711, 81467, 224, 19233, 0, 0, 0

0

where 0 is the value of summary_mutation variable. Please help me out. Thanks in advance.

rchang
  • 5,150
  • 1
  • 15
  • 25
  • Is that `call` method the one from the `subprocess` module (i.e. does `from subprocess import call` appear in your code)? The signature looks like it but I wanted to make sure. – rchang Jan 12 '15 at 11:32
  • Yes it is from the subprocess module and I have done an import command but didn't show it here. Sorry! – Varshith Chakrapani Jan 12 '15 at 11:33

1 Answers1

1

Assuming you are working with the subprocess module, it is probably best to use Popen objects if you want to interact with the output.

from subprocess import Popen, PIPE

proc = Popen(["halSummarizeMutations", hal_output], stdout=PIPE)
summary_mutation = proc.communicate()[0]

Now summary_mutation should be a string representation of the contents of the subprocess' standard output stream.

rchang
  • 5,150
  • 1
  • 15
  • 25
  • @VarshithChakrapani I'm glad if it helps. If you haven't yet looked through it, the documentation for the Python 2.x `subprocess` module is here (https://docs.python.org/2/library/subprocess.html) at this link. – rchang Jan 12 '15 at 11:40
  • @VarshithChakrapani: you could also use `summary_mutation = subprocess.check_output(["halSummarizeMutations", hal_output])` – jfs Jan 13 '15 at 12:23