How can I save command outputs in PyMOL to a txt file? - python

I'm new to PyMOL, and I'm trying to write a python script that will generate a .txt file and save a PyMOL command output to it. Let's say it has an array containing names of pdb files and a for loop that aligns each one to some specific protein:
pdb = ["191L", "192L", "193L", "194L"]
cmd.fetch("190L")
for i in pdb:
cmd.fetch(i)
cmd.align(i, "190L")
PyMOL will calculate the RMSD for each alignment. How can I write my script so that it will take each RMSD and save it to a text file?
Here's what I have so far:
def get_rmsd():
cmd.fetch("190L")
for i in pdb:
cmd.fetch(i)
output = open("rmsd.txt", "w")
data = cmd.align(i, "190L")
data = str(data)
output.write(data)
stored.f.close()
When I call the function on PyMOL, it fetches and aligns the file like it's supposed to, but no text file is created.

Figured out the answer to my own question, the solution was embarrassingly easy lol.
Assign variables to each desired output, e.g.:
output = cmd.align("190L", "191L")
Run the script in PyMOL as is, then feed the python code into it line by line, beginning with "python" and closing with "python end." In my case, something like:
python
with open("rmsd.txt", "w") as f:
f.write(output)
f.close()
python end
A really basic example but that's the gist of it.

I wouldn't use the pymol.Scratch_Storage class. I would keep it simple and do something like:
Create a new file "test.py"
Copy the following:
from pymol import cmd
import os
def get_rmsd(pdbs, align_to):
# a bit more reusable
cmd.fetch(align_to)
with open("rmsd.txt", "w") as w:
# using context management will close the rmsd.txt automatically
for pdb in pdbs:
cmd.fetch(pdb)
line = cmd.align(pdb, align_to)
w.write(f"{line}\n")
print(f"outfile: {os.path.join(os.getcwd(), 'rmsd.txt')}")
get_rmsd(["191L", "192L", "193L", "194L"], "190l")
Run script from PyMOL terminal with: run test.py

for i in pdb:
cmd.fetch(i)
output = open("rmsd.txt", "w")
data = cmd.align(i, "190L")
data = str(data)
output.write(data)
stored.f.close()
try:
for i in pdb:
cmd.fetch(i)
output = open("rmsd.txt", "a")
data = cmd.align(i, "190L")
output.write("%s\n" % data)
output.close()
should do the job

Related

Jupiter Notebook in vs code: python command not showing/ reading file from memory

my code is not showing any output after running specific commands for reading a file from storage location. I'm still a beginner so i'm little confused. Dropping my code here below
You are not reading the file your code, hence it does not show any output.
Try this
path = <path of you file>
file = open(path, mode="r")
file.read()
file.close()
You can also simplify it further
with open(path, mode="r") as file:
file.read()
If you want to display the output of an executed cell, you should execute the variable you want to inspect in a different cell (or at the end of a cell). It wont work if it is inside the context manager (with clause).
[1] with open(path, mode="r") as file:
content = file.read()
[2] content

Is there a way to pipe the output of tensorflow to a .txt file

So after running this line of code
python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=tf_files/106906.jpg
my console throws an output like this:
Evaluation time (1-image): 0.294s
ClassA (score=0.97202)
ClassB (score=0.02572)
ClassC (score=0.00226)
Is there a way I can put the output into a .txt or CSV? I've tried adding a simple >output.txt to the end of the previous command but that only created an empty .txt file.
The original code looks like this:
for i in top_k:
print(template.format(labels[i], results[i]))
Which I modified to this:
for i in top_k:
outputFile = open('output.txt', 'w')
print(template.format(labels[i], results[i]), file = outputFile)
outputFile.close()
Unfortunately this only prints the last thing. Basically it prints the first message and then it puts the second one in place of the first etc. Is there a way to keep all the lines in that output.txt file?
You could either print the output to a file with print (3.x).
Example (from article above):
# Code for printing to a file
sample = open('samplefile.txt', 'w')
print('GeeksForGeeks', file = sample)
sample.close()
Or, if you are operating on bash, redirect sys.stdout to file. Or for Windows.
As an alternative you could extract it by attributes, e.g. with list comprehensions, and talk advantage of python's csv lib to save it to file.

How to read input() from a text file in Python

My Python program requests some user input:
n = int(input())
for i in range(n):
s = input()
# ...
For debugging purposes, I would like to create a test input.txt file like this:
3
string1
string2
string3
so that I need not to manually enter data from the keyboard every time I launch it for debug.
Is there any way to get input() from text file, not from keyboard (without changing program code).
I am using Wing Personal as a dev tool.
Update
I know for sure, that there exist bots which are able to test python programs passing various inputs and comparing output with known answers. I guess these bots do not hit the keyboard with iron fingers. Maybe they use command-line tools to pass input.txt to the Python so that it reads input() from that file, not from keyboard. I would like to learn how to do it.
Input reads from Standard input so if you use bash you can redirect stdin to a file
without changing your code
in bash you would run something like
cat textfile | programm.py
or
< textfile programm.py
I've noticed that none of these solutions redirects the stdin from Python. To do that, you can use this snippet:
import sys
sys.stdin = open('input.txt', 'r')
Hopefully, that helps you!
You can read the file into a list like this:
with open('filename.txt', 'r') as file:
input_lines = [line.strip() for line in file]
s would be data in index i.
n = int(input())
for i in range(n):
s = input_lines[i]
# ...
Make sure your py file is in the same dir as your txt file.
You can create a test.py file which is used to run your test. In the test.py, you will read the content of input.txt and import the programming.
For example, in test.py file.
import main_code
with open('input.txt', 'r') as file:
lines = file.readlines()
for line in lines:
s = line
"""
working with the input.
"""

Writing general script using open('file', 'r')

I am writing a script where I first read a file using
with open('file', 'r') as file,
do some operation, and then write it to a new file using
with open('newfile', 'w') as newfile.
My question is, what do I need to change in the script to make it general for a number of files, so that I can just call the script with the file name from the terminal like python3 script.py file.ext? Also, is there a way to write the output back into the original file using this method?
One way of doing this is:
import sys
file=open(sys.argv[1], "r")
newfile=open("example.txt", "w")
content=file.read()
#do stuff to content here such as content=content.upper()
newfile.write(content)
If you inputted into the command line:
python script.py aaaaaa.txt
Then the file you set as the output would have the edited content of aaaaaa.txt
Hope this helps :)

Python:How do I export the output of my code to it's own text file?

How do I export the output of my code to it's own text file? When I run my code I get a large set of data from it. How do I export this so that I can read all lines of data in it's own text file.
You can write file in python like
with open("out.txt", "w") as f:
f.write("OUTPUT")
Or you can use io redirection to redirect output to a file
$ python code.py > out.txt
https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
https://en.wikipedia.org/wiki/Redirection_(computing)
Assuming you will read the results in another application, you can use redirections, usually something like this:
./myprogram >results.txt
You may want to take a look at file objects, which would allow you to write all the data you want to a file.
For example :
file = open('output.txt', 'w')
file.write('Here is some data')
file.close()
One way to do it is this:
import csv
with open("name_of_file_to_be_created") as f:
writer = csv.writer(f)
for i in range(n):
writer.writerow('stuff to write')
# writes a single line in each iteration, e.g. assuming you are computing something inside the loop
Another way to do this is:
with open("name_of_file_to_be_created") as f:
print("here you can type freely", file = f)
# or
f.write('whatever it is that you have to write')

Categories

Resources