Iam not quite sure how to define Users in this case - python-3.x

iam a total noob in python; i have a background in chemistry and iam doing my master in computational chemistry. Iam trying to learn computer science as fast as i can.
I currently dont know how to solve this error. I have googled the question but the answers dont actually satisfy.
I would really appreciate if you guys give me hints on how to fix this error.
Thanks,
Thanh Le
In order for the program to work, it uses codes from this file containing:
from RunRMSD import RunRMSD
RunRMSD()
from SumRMSD import SumRMSD
SumRMSD()
then it uses codes from a file (RunRMSD) containing:
run calcRMSD.py to get raw output from pymol
def RunRMSD():
# get output directory from a threefiles.txt
with open('./threefiles.txt') as fi:
fline = fi.readline()
flist = fline.split('\t')
path_output = flist[1]
import os
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
os.system(command)
Not sure if my path is correct though.
thanhs-MacBook-Pro-2:untitled folder thanhle$ python Director_RMSD.py
Traceback (most recent call last):
File "Director_RMSD.py", line 5, in <module>
RunRMSD()
File "/Users/thanhle/Desktop/ftdock-2-dev2/untitled folder/RunRMSD.py", line 11, in RunRMSD
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
NameError: global name 'Users' is not defined

The "command" variable is not well written:
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
The error is thrown because the path /Users/thanhle/Desktop/output/ is not concatenated and also you are missing a apostrophe. If you don't want to parse any variable to the command it should be written:
command = '/opt/local/bin/pymol -cqr ./CalcRMSD.py > /Users/thanhle/Desktop/output/RMSD.out'

Related

porting numpy fromfile to python3 when reading from standard input

I'm porting some code to Python 3 and stumbled upon an invocation of numpy.fromfile(src) which works just fine when src is a handle of a real file but fails when src is sys.stdin. I boiled down the issue to the following two commands that will let you see how the problem only exists in Python 3 but not in Python 2:
$ echo 1 2 3 | python -c 'import numpy,sys; print(numpy.fromfile(sys.stdin, dtype=int, sep=" "))'
[1 2 3]
$ echo 1 2 3 | python3 -c 'import numpy,sys; print(numpy.fromfile(sys.stdin, dtype=int, sep=" "))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
OSError: obtaining file position failed
When I look at /usr/lib/python2.7/dist-packages/numpy/core/records.py then it seems that even in the Python2 version of numpy it says for fromfile:
The file object must support random access (i.e. it must have tell and seek methods).
So I guess the fact that the above works with Python2 was never meant to be supported. Still the code I want to port to Python3 relied on this functionality.
I tried reading sys.stdin into an io.BytesIO but this will error out with io.UnsupportedOperation: fileno.
In my hopes that others also stumbled across this problem, I wanted to ask for an easy drop-in replacement of numpy.fromfile() which allows reading from stdin even on Python3. Certainly other projects already had to implement a solution to this? I wasn't able to find one.
You could try reading bytes from stdin like this:
data = sys.stdin.buffer.read()
And then using numpy.frombuffer to read it. (You should also provide the datatype)
arr = np.frombuffer(data, dtype=np.float)
If you are reading text data, you can do this:
data = sys.stdin.read()
arr = numpy.fromstring(data, sep=" ")
When you don't provide sep it acts in some legacy mode.

Unable to run Porter5: generating `.flatpsi` file instead of `.psi`

I am trying to use Porter5 to run protein secondary structure prediction on a FASTA file containing a bunch of protein sequences. I am using a Linux machine.
For starters, I decided to try using the example file that gets downloaded along with Porter5, called 2FLGA.fasta. The command I used was the one I found on the GitHub page for Porter5 (https://github.com/mircare/Porter5/)
$ python3 Porter5.py -i example/2FLGA.fasta --cpu 4
I got the following error message:
sh: 1: /home/user/ncbi-blast-2.8.1+/bin/psiblast: not found
PSI-BLAST executed in 0.01s
wc: example/2FLGA.fasta.psi: No such file or directory
awk: cannot open example/2FLGA.fasta.psi (No such file or directory)
HHblits executed in 0.01s
Traceback (most recent call last):
File "/home/user/Porter5/scripts/process-alignment.py", line 37, in <module>
sequences = lines[0] = len(lines) - 1
IndexError: list assignment index out of range
Traceback (most recent call last):
File "Porter5.py", line 80, in <module>
flatpsi_ann = open(filename+".flatpsi.ann", "r").readlines()
FileNotFoundError: [Errno 2] No such file or directory: 'example/2FLGA.fasta.flatpsi.ann'
After PSI-BLAST, the Porter5 script is expecting an output file called 2FLGA.fasta.psi. I checked the example directory and it contains an output file called 2FLGA.fasta.flatpsi.
I'm not sure what to do here. I don't want to try modifying any of the Porter5 scripts to look for .flatpsi files instead of .psi files because I am a beginner at programming, and I don't want all hell to break loose by tampering with the code.
Could someone please help me with this? Any help is appreciated.
(There are a bunch of errors to negotiate with later but I'll see about those after dealing with the first one.)
I am the author of Porter5 and I generally recommend to open an issue straight on GitHub since I don't get any notification otherwise.
It looks like the path of psiblast is wrong (first line of your error message). You can check that with the following command:
$ ls /home/user/ncbi-blast-2.8.1+/bin/psiblast
Also, the path for the executable or the database of HHblits is wrong, or maybe both. You can check that as follow (within Porter5/):
$ cat scripts/config.ini
You can either edit scripts/config.ini or run the following command until Porter5 runs succesfully:
$ python3 Porter5.py -i example/2FLGA.fasta --cpu 4 --setup
(The .flatpsi is an intermediate file, it doesn't contain a valid representation if HHblits doesn't run succesfully)

How to catch error and stop using IF and ELSE statement?

Hello i am new to python programming. I am self learning by doing a project.
the code below is working fine, but i want to catch the exception if the condition is FALSE. i already have a statement to print if the directory is not there; and continue. I know i can make this code work by nesting the if statement.
but i dont want to do. i want to proceed to next line of code. when i run this as it is, i will get an error if the directory is missing.
import os
goToDir = open("URPsetup.dat","r")
goToDirPath = goToDir.read()
if (os.path.exists(goToDirPath))== True:
os.chdir(goToDirPath)
else: print("directory not found, please check file URPsetup.dat")
goToConfig = open("utility.dat", "r")
print(goToConfig.read())
i get this error message when the directory is not there. What i mean here is that, the directory i supplied in "URPsetup.dat" was incorrect. I did this on purpose to see if the code will stop at the else statement above. it will print the else statement, and continue on to show the error message below. how do i catch this error and stop?
directory not found, please check file URPsetup.dat
Traceback (most recent call last):
File "C:/Users/R82436/PycharmProjects/URP2017/prototype.py", line 9, in <module>
goToConfig = open("utility.dat", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'utility.dat'
directory not found, please check file URPsetup.dat
Process finished with exit code 1
I GUESS you're looking for this:
import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
sys.exit()
.
.
OR this will do just fine too:
# No need to import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
raise SystemExit(0)
.
.
That will do the trick ;)
And try to explain your problem properly next time, You aren't completely clear on what is it exactly that you want to do.

Python3.4 -Nmap Requires root privileges

Running on Mac Os 10.10.5
Running this script to scan for hosts on the network:
import nmap
nm = nmap.PortScanner()
nm.scan('192.168.5.1/24', arguments='-O')
for h in nm.all_hosts():
if 'mac' in nm[h]['addresses']:
print(nm[h]['addresses'], nm[h]['vendor'])
When running it its printing:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 290, in analyse_nmap_xml_scan
dom = ET.fromstring(self._nmap_last_output)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py", line 1326, in XML
return parser.close()
File "<string>", line None
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/*/Documents/*.py", line 3, in <module>
nm.scan('192.168.0.0/24', arguments='-O')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 235, in scan
nmap_err_keep_trace = nmap_err_keep_trace)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nmap/nmap.py", line 293, in analyse_nmap_xml_scan
raise PortScannerError(nmap_err)
nmap.nmap.PortScannerError: 'TCP/IP fingerprinting (for OS scan) requires root privileges.\nQUITTING!\n'
I tried going to that directory and running this command in the terminal:
sudo python *.py
({'mac': '02:62:31:41:6D:84', 'ipv4': '192.168.5.1'}, {})
Any suggestions to run the script from the python IDLE?
Running IDLE as root might work, but it might not be a great idea. sudo idle
Option 1 (recommended):
Put the code requiring elevated privileges in a python file which you run with sudo. I assume you want to play with the results, so you could have the script save the results to a file, which you then read in IDLE.
The following code works in python 2.7 and 3.4
import nmap
import json
nm = nmap.PortScanner()
nm.scan('192.168.5.1/24',arguments='-O') #Note that I tested with -sP to save time
output = []
with open('output.txt', 'a') as outfile:
for h in nm.all_hosts():
if 'mac' in nm[h]['addresses']:
item = nm[h]['addresses']
if nm[h]['vendor'].values():
item['vendor'] = list(nm[h]['vendor'].values())[0]
output.append(item)
json.dump(output, outfile)
Run sudo python nmaproot.py
Since the file is written by root, you need to change ownership back to yourself.
sudo chown -r myusername output.txt
In IDLE:
import json
input = open('output.txt','r'):
json_data = json.load(input)
json_data[0] # first host
Option 2 (not recommended at all):
Use subprocess to run the file with the elevated code as root and return the output. It gets kind of messy and requires you to hardcode your password...but it's possible.
from subprocess import Popen, PIPE
cmd = ['sudo', '-S', 'python', 'nmaproot.py']
sudopass = 'mypassword'
p = Popen(cmd, stdin=PIPE, stderr=PIPE,universal_newlines=True, stdout=PIPE)
output = p.communicate(sudopass + '\n')
I'm unsure of how you can run a given portion of your python code as root without saving it to a file and running it separately. I recommend you go with option 1 as option 2 isn't very good (but it was fun to figure out).
Copy the idle desktop shortcut and name it rootidle then right and change properties. Goto desktop entry and add gksu before /usr/bin/idle3. Then load and run the program
maybe this might help someone here. Found this from one site
scanner.scan(ip_addr, '1-1024', '-v -sS', sudo=True)
use
sudo = True

Testing python programs without using python shell

I would like to easily test my python programs without constantly using the python shell since each time the program is modified you have to quit, re-enter the python shell and import the program again. I am using a 2012 Macbook pro with OSX. I have the following code:
import sys
def read_strings(filename):
with open(filename) as file:
return file.read().split('>')[1:0]
file1 = sys.argv[1]
filename = read_strings(file1)
Essentially I would like to read into and split a txt file containing:
id1>id2>id3>id4
I am entering this into my command line:
pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt
However when I try the sys.argv approach on the command line my program returns nothing. Is this a good approach to testing code, could anyone point me in the correct direction?
This is what I would like to happen:
pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt
['id1', 'id2', 'id3', 'id4']
Let's take this a piece at a time:
However when I try the sys.argv approach on the command line my
program returns nothing
The final result of your program is that it writes a string into the variable filename. It's a little strange to have a program "return" a value. Generally, you want a program to print it's something out or save something to a file. I'm guessing it would ease your debugging if you modified your program by adding,
print (filename)
at the end: you'd be able to see the result of your program.
could anyone point me in the correct direction?
One other debugging note: It can be useful to write your .py files so that they can be run both independently at the command line or in a python shell. How you've currently structured your code, this will work semi-poorly. (Starting a shell and then importing your file will cause an error because sys.argv[1] isn't defined.)
A solution to this is to change your the bottom section of your code as follows:
if __name__ == '__main__':
file1 = sys.argv[1]
filename = read_strings(file1)
The if guard at the top says, "If running as a standalone script, then run what's below me. If you imported me from some place else, then do not execute what's below me."
Feel free to follow up below if I misinterpreted your question.
You never do anything with the result of read_strings. Try:
print(read_strings(file1))

Resources