Posts: 7
Threads: 3
Joined: May 2019
May-10-2019, 04:37 PM
(This post was last modified: May-10-2019, 04:37 PM by Aurimas.)
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
Posts: 4,448
Threads: 68
Joined: Jan 2018
May-11-2019, 06:50 AM
(This post was last modified: May-11-2019, 08:30 AM by Gribouillis.)
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.
Posts: 2,009
Threads: 9
Joined: May 2017
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
Posts: 7
Threads: 3
Joined: May 2019
May-15-2019, 07:35 AM
(This post was last modified: May-15-2019, 07:35 AM by Aurimas.)
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?
Posts: 4,448
Threads: 68
Joined: Jan 2018
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)
Posts: 7
Threads: 3
Joined: May 2019
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 :-)
Posts: 4,448
Threads: 68
Joined: Jan 2018
Aurimas Wrote:I have python 2.7 :( It is a matter of a few seconds to install a better python.
Posts: 7
Threads: 3
Joined: May 2019
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.
Posts: 7
Threads: 3
Joined: May 2019
May-15-2019, 04:02 PM
(This post was last modified: May-15-2019, 04:02 PM by Aurimas.)
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)?
|