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
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
  Reading and storing a line of output from pexpect child eagerissac 1 4,144 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Receive Input on Same Line? johnywhy 8 607 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Basic Coding Question: Exit Program Command? RockBlok 3 503 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  problem in using input command akbarza 4 996 Oct-19-2023, 03:27 PM
Last Post: popejose
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Using subprocess to execute complex command with many arguments medatib531 5 1,717 Apr-27-2023, 02:23 PM
Last Post: medatib531
  output provide the filename along with the input file processed. arjunaram 1 903 Apr-13-2023, 08:15 PM
Last Post: menator01
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,033 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  merge two csv files into output.csv using Subprocess mg24 9 1,688 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Command line argument issue space issue mg24 5 1,279 Oct-26-2022, 11:05 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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