Biopython NcbiblastpCommandline not working: "No such file or directory: 'test.xml" - biopython

Trying to do a local blast using NCBIBLAST+, already downloaded in the mentioned path. I am using Win10 and Spyder.
from Bio.Blast.Applications import NcbiblastpCommandline
blastp=r"C:\NCBI\blast-BLAST_VERSION+\bin\blastp.exe"
blastp_cline= NcbiblastpCommandline(blastp,query="test.fasta",db='Bos_transcript_and_prot\\bos_protein.fasta',outfmt=5,evalue=0.001, out="test.xml")
blastp_cline
from Bio.Blast import NCBIXML
with open("test.xml") as result_handle:
E_VALUE_THRESH=0.01
blast_records = NCBIXML.parse(result_handle)
blast_record = NCBIXML.read(result_handle)
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
if hsp.expect < E_VALUE_THRESH:
print("****Alignment****")
print("sequence:", alignment.title)
print("length:", alignment.length)
print("e value:", hsp.expect)
print(hsp.query[0:75] + "...")
print(hsp.match[0:75] + "...")
print(hsp.sbjct[0:75] + "...")
I have my database created as well as the fasta files contain only protein sequences. Still .XML file is not getting created and also the blastp_cline variable is not getting created.
Thanks in advance

For execution of the command use:
stdout, stderr = blastp_cline()

try:
change with open("test.xml") as result_handle:
with :
with open("test.xml", 'w') as result_handle:
and let me know
dont remember how 'w' differes from 'w+' , try to google it

Related

Using io.tmpfile() with shell command, ran via io.popen, in Lua?

I'm using Lua in Scite on Windows, but hopefully this is a general Lua question.
Let's say I want to write a temporary string content to a temporary file in Lua - which I want to be eventually read by another program, - and I tried using io.tmpfile():
mytmpfile = assert( io.tmpfile() )
mytmpfile:write( MYTMPTEXT )
mytmpfile:seek("set", 0) -- back to start
print("mytmpfile" .. mytmpfile .. "<<<")
mytmpfile:close()
I like io.tmpfile() because it is noted in https://www.lua.org/pil/21.3.html :
The tmpfile function returns a handle for a temporary file, open in read/write mode. That file is automatically removed (deleted) when your program ends.
However, when I try to print mytmpfile, I get:
C:\Users\ME/sciteLuaFunctions.lua:956: attempt to concatenate a FILE* value (global 'mytmpfile')
>Lua: error occurred while processing command
I got the explanation for that here Re: path for io.tmpfile() ?:
how do I get the path used to generate the temp file created by io.tmpfile()
You can't. The whole point of tmpfile is to give you a file handle without
giving you the file name to avoid race conditions.
And indeed, on some OSes, the file has no name.
So, it will not be possible for me to use the filename of the tmpfile in a command line that should be ran by the OS, as in:
f = io.popen("python myprog.py " .. mytmpfile)
So my questions are:
Would it be somehow possible to specify this tmpfile file handle as the input argument for the externally ran program/script, say in io.popen - instead of using the (non-existing) tmpfile filename?
If above is not possible, what is the next best option (in terms of not having to maintain it, i.e. not having to remember to delete the file) for opening a temporary file in Lua?
You can get a temp filename with os.tmpname.
local n = os.tmpname()
local f = io.open(n, 'w+b')
f:write(....)
f:close()
os.remove(n)
If your purpose is sending some data to a python script, you can also use 'w' mode in popen.
--lua
local f = io.popen(prog, 'w')
f:write(....)
#python
import sys
data = sys.stdin.readline()

Cannot extract the contents of a TGZ file using F# and SharpZipLib

I am learning F# and Deedle. I am trying to extract the contents of this TGZ File using SharpZipLib. I downloaded the TGZ to my local drive. I think I am close because out1 works, but out2 errs. I am sure the code could be written better with pipe forwarding or composition, but it first needs to work. Does anyone have any ideas?
open ICSharpCode.SharpZipLib.GZip
open ICSharpCode.SharpZipLib.Tar
let extract path filename =
let fullpath = Path.Combine(path, filename)
let inSt = File.OpenRead(fullpath)
let gSt = new GZipInputStream(inSt)
let tar = TarArchive.CreateOutputTarArchive(gSt)
let out1 = tar.ListContents
let out2 = tar.ExtractContents(path)
out2
extract "C:/Downloads/" "dragontail-12.4.1.tgz"
This is the error:
Error: System.NullReferenceException: Object reference not set to an instance of an object.
at ICSharpCode.SharpZipLib.Tar.TarArchive.ExtractContents(String destinationDirectory, Boolean allowParentTraversal) in /_/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs:line 620
at ICSharpCode.SharpZipLib.Tar.TarArchive.ExtractContents(String destinationDirectory) in /_/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs:line 600
at FSI_0104.extract(String path, String filename)
at <StartupCode$FSI_0104>.$FSI_0104.main#()
Does this help?
https://stackoverflow.com/a/52200001/1594263
Looks like you should be using CreateInputTarArchive(). I modified your example to use CreateInputTarArchive(), and it worked for me.
BTW you're just assigning a function to out1, you're not actually calling ListContents().
I'm not a SharpZipLib expert, but this works for me:
use tar = TarArchive.CreateInputTarArchive(gSt, System.Text.Encoding.UTF8)
tar.ExtractContents(path, true)
I think you have to explicitly allow for path traversal to unzip into an absolute path. See SharpZipLib unit tests in this file.

How to copy latest file from a directory using lua

I am trying to copy only latest file from a directory using lua file.
Latest file means : depends on modified time/created time.
How can i do this?
Referring to this question: How can I get last modified timestamp in Lua
You might be able to leverage the io.popen function to execute a shell command to get the name of the file. It seems like there's no builtin function that exposes filesystem metadata or stats. Something like this might work:
local name_handle = io.popen("ls -t1 | head -n 1")
local filename = name_handle:read()
I'm not familiar with Lua, but perhaps this helps. I imagine that once you have the name of the newest file you can use the other IO functions to do the copying.
local function get_last_file_name(directory)
local command = 'dir /a-d /o-d /tw /b "'..directory..'" 2>nul:'
-- /tw for last modified file
-- /tc for last created file
local pipe = io.popen(command)
local file_name = pipe:read()
pipe:close()
return file_name
end
local directory = [[C:\path\to\your\directory\]]
local file_name = get_last_file_name(directory)
if file_name then
print(file_name)
-- read the last file
local file = io.open(directory..file_name)
local content = file:read"*a"
file:close()
-- print content of the last file
print(content)
else
print"Directory is empty"
end

Writing a file to a specific path in ruby taking the file name from excel

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.
I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem
You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

python xlrd: convert xls to csv using tempfiles. Tempfile is empty

I am downloading an xls file from the internet. It is in .xls format but I need 'Sheet1' to be in csv format. I use xlrd to make the conversion but seem to have run into an issue where the file I write to is empty?
import urllib2
import tempfile
import csv
import xlrd
url_2_fetch = ____
u = urllib2.urlopen(url_2_fetch)
wb = xlrd.open_workbook(file_contents=u.read())
sh = wb.sheet_by_name('Sheet1')
csv_temp_file = tempfile.TemporaryFile()
with open('csv_temp_file', 'wb') as f:
writer = csv.writer(f)
for rownum in xrange(sh.nrows):
writer.writerow(sh.row_values(rownum))
That seemed to have worked. But now I want to inspect the values by doing the following:
with open('csv_temp_file', 'rb') as z:
reader = csv.reader(z)
for row in reader:
print row
But I get nothing:
>>> with open('csv_temp_file', 'rb') as z:
... reader = csv.reader(z)
... for row in reader:
... print row
...
>>>
I am using a tempfile because I want to do more parsing of the content and then use SQLAlchemy to store the contents of the csv post more parsing to a mySQL database.
I appreciate the help. Thank you.
This is completely wrong:
csv_temp_file = tempfile.TemporaryFile()
with open('csv_temp_file', 'wb') as f:
writer = csv.writer(f)
The tempfile.TemporaryFile() call returns "a file-like object that can be used as a temporary storage area. The file will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected)."
So your variable csv_temp_file contains a file object, already open, that you can read and write to, and will be deleted as soon as you call .close() on it, overwrite the variable, or cleanly exit the program.
So far so good. But then you proceed to open another file with open('csv_temp_file', 'wb') that is not a temporary file, is created in the script's current directory with the fixed name 'csv_temp_file', is overwritten every time this script is run, can cause security holes, strange bugs and race conditions, and is not related to the variable csv_temp_file in any way.
You should trash the with open statement and use the csv_temp_file variable you already have. You can try to .seek(0) on it before using it again with the csv reader, it should work. Call .close() on it when you are done with it and the temporary file will be deleted.

Resources