Python Forum
A question about subprocess taking input from command line and returning output!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A question about subprocess taking input from command line and returning output!
#1
Dear Python forum members,

The script I have written below (the_script.py) works fine with this terminal command at MAC OS Mojave Python 2.7:
Quote:python the_script.py < ABC.pdb > ABC.txt
! However I want my script (the_script.py below) written through terminal to be like this:
Quote:python the_script.py ABC.pdb
!
And not involve < or > and return the output to ABC.txt. The .pdb file is a text file as well.

#!/usr/bin/python2

import sys

import subprocess

process = subprocess.Popen(
		["voronota","get-balls-from-atoms-file","--annotated"],stdin=subprocess.PIPE,
		stdout=subprocess.PIPE)

output=process.communicate(sys.stdin.read())[0];

print output
I will look forward to all the replies.

Sincerely,
Aurimas
Reply
#2
Replace sys.stdin.read() with pathlib.Path('ABC.pdb').read_text() and replace print(output) with pathlib.Path('ABC.txt').write_text(output). Use the argparse module to get the file names from the command line.
Aurimas Wrote:The script I have written below
In 2019, use python 3.
Reply
#3
Don't forget that the built-in print function has a file keyword where to output the text.
In the case where a line has no line-ending/line-seperator, you could use the print funciton.
It adds by default a newline to the end of the string. This could be controlled with the keyword end

In [1]: print?                                                                  
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Thank you guys, but I want to specify what pdb file I choose to use (like 1abc.pdb, 1fgh.pdb or other) and the output I need is
Quote: awk '{sum += $3} END {print sum}'
that prints a numerical value to terminal. Full terminal command in linux/mac would be
Quote:cat HS_1abc.pdb | voronota get-balls-from-atoms-file --annotated | voronota calculate-contacts --annotated | voronota query-contacts --inter-residue --match-first 'R<ARG>' --match-second 'c<solvent>' | awk '{sum += $3} END {print sum}'
I also need the program to say what R in --match-second to use (in this case it is ARG but it can have any one of 20 different values). Also for --match-second I need to define up to fours values like c<A,B,C,D>, c<A,B), c<W,C>, c<solvent>, c<A,C,E> or other letters

How could that be possible with pipes and subprocess and communicate?
Reply
#5
I would try something like this
from subprocess import Popen, PIPE

cat = ['cat', 'HS_1abc.pdb']
get = ['voronota', 'get-balls-from-atoms-file', '--annotated']
calc = ['voronota', 'calculate-contacts', '--annotated']
que = [
    'voronota', 'query-contacts', '--inter-residue',
    '--match-first', 'R<ARG>',
    '--match-second', 'c<solvent>']
awk = ['awk', '{sum += $3} END {print sum}']

cat = Popen(cat, stdout=PIPE)
get = Popen(get, stdin=cat.stdout, stdout=PIPE)
calc = Popen(calc, stdin=get.stdout, stdout=PIPE)
que = Popen(que, stdin=calc.stdout, stdout=PIPE)
awk = Popen(awk, stdin=que.stdout, stdout=PIPE, stderr=PIPE)
out, err = awk.communicate()
print(out)
Reply
#6
Thank you. Should the code be run through python3? I have python 2.7 :(
My main struggle is to make an input being asked before running the script as in your given example I give an input as a string which would require me to write hundreds of different .pdb codes. I would like the script to ask first and then give the numerical value. Otherwise I am better doing it through terminal which I am doing now. Perhaps I need to include dictionary where I could run the calculations :-)
Reply
#7
Aurimas Wrote:I have python 2.7 :(
It is a matter of a few seconds to install a better python.
Reply
#8
Installed Python3 but it works with 2.7 as well. How I could write in terminal: python script 1abc.pdb press enter and get:
XXXXXX - the sum of third columns values
With the written code I get the sum but have to change script all the time to get the correct value, which is wasteful as I have a lot of arguments to change (in ten hundreds or more) :/
Also how I could make 8 line of your proposed code to ask the input for 'R<AAA>' where the script asks to enter AAA as ARG, ASN, ASP, GLN, GLY or any other amino acid three digit code.
Reply
#9
I am tryin to think of how to ask an input to be a text file. It can be any .pdb file like 1abc.pdb, 2abc.pdb, 3drt.pdb, etc.
I am working with upgrading the above script to this:
from subprocess import Popen, PIPE

file = raw_input("> ")
read_file = file.format
open_file = open('read_file', 'r+') 

cat = ['cat', 'open_file']
get = ['voronota', 'get-balls-from-atoms-file', '--annotated']
calc = ['voronota', 'calculate-contacts', '--annotated']
que = [
    'voronota', 'query-contacts', '--inter-residue',
    '--match-first', 'R<ARG>',
    '--match-second', 'c<solvent>']
awk = ['awk', '{sum += $3} END {print sum}']
 
cat = Popen(cat, stdout=PIPE)
get = Popen(get, stdin=cat.stdout, stdout=PIPE)
calc = Popen(calc, stdin=get.stdout, stdout=PIPE)
que = Popen(que, stdin=calc.stdout, stdout=PIPE)
awk = Popen(awk, stdin=que.stdout, stdout=PIPE, stderr=PIPE)
out, err = awk.communicate()
print(out)
but am getting this error:
Error:
Traceback (most recent call last): File "voronota_script.py", line 4, in <module> open_file = open('read_file', 'r+') IOError: [Errno 2] No such file or directory: 'read_file'
How is it possible to ask an input to be a txt file (in my case x.pdb where x has 28 different values)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi-line console input lizze 4 230 Dec-26-2020, 08:10 AM
Last Post: lizze
  Code giving same output no matter the input. Yort 2 196 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 421 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Question on subprocess module. knoxvilles_joker 1 206 Oct-31-2020, 08:52 AM
Last Post: Gribouillis
  Passing List of Objects in Command Line Python usman 7 554 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  EOF error while taking input ShishirModi 1 339 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  single input infinite output problem Chase91 2 337 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Error when running mktorrent subprocess command pythonnewbie138 4 348 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  bad input line 3 how t fix it youssef210 2 368 Aug-27-2020, 04:57 PM
Last Post: nilamo
  How to get program output from subprocess.Popen? glestwid 1 373 Aug-19-2020, 05:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020