subprocess.call using cygwin instead of cmd on Windows

2024/11/17 23:32:27

I'm programming on Windows 7 and in one of my Python projects I need to call bedtools, which only works with Cygwin on Windows. I'm new to Cygwin, installed the default version + everything needed for bedtools and then used Cygwin to install bedtools by using make as described in the installation instructions.

$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make

When I use the Cygwin terminal to call it manually like below, it works without problem and the output file contains the correct result.

bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file

But when I use subprocess.call in my program it seems to use Windows cmd instead of Cygwin, which doesn't work.

arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)

Results in no output file and a return code of 3221225781.


arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)

Results in an empty output file and a return code of 3221225781.


cygwin_bash_path = 'D:/Cygwin/bin/bash.exe'
arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)

Results in no output file, a return code of 126 and

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file

arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)

Results in an empty output file, a return code of 126 and

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file

Any ideas how I can get it to work?

Answer

Imagine you want to run a Linux command from Windows. You could install Linux into a VM and run commands via ssh (Putty/plink on Windows):

#!/usr/bin/env python
import subprocesscmd = [r'C:\path\to\plink.exe', '-ssh', 'user@vm_host', '/path/to/bedtools']
with open('output', 'wb', 0) as file:subprocess.check_call(cmd, stdout=file)

Cygwin provides run command that allows to run commands directly:

cmd = [r'C:\cygwin\path\to\run.exe', '-p', '/path/to/', 'bedtools', '-wait', 'arg1', 'arg2']

Note: Python script is run from Windows in both cases. bedtools is Linux or Cygwin (non-Windows) command here and therefore you should provide POSIX paths.

https://en.xdnf.cn/q/71124.html

Related Q&A

Django Celery Received unregistered task of type appname.tasks.add

Following the documentation and the Demo Django project here https://github.com/celery/celery/tree/3.1/examples/djangoProject Structurepiesup2|piesup2| |__init__.py| |celery.py| |settings.py| |urls…

Documenting and detailing a single script based on the comments inside

I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like: # -*- coding: …

Using Ansible variables in testinfra

Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running teststest.pyimport pytest def test_zabbix_agent_package(host):package = host.pa…

How to create a dictionary of dictionaries of dictionaries in Python

So I am taking a natural language processing class and I need to create a trigram language model to generate random text that looks "realistic" to a certain degree based off of some sample da…

How to separate Master Slave (DB read / writes) in Flask Sqlalchemy

Im trying to separate the Read and write DB operations via Flask Sqlalchemy. Im using binds to connect to the mysql databases. I would want to perform the write operation in Master and Reads from slave…

Why import class from another file will call __init__ function?

The structure of the project is:project - main.py - session.py - spider.pyThere is a class in session.py:import requestsclass Session:def __init__(self):self.session = requests.Session()print(Session c…

Flask: login session times out too soon

While editing a record, if there is a long wait of let say a few minutes (getting coffee) and then coming back to press the save (POST), I get redirected to the main page to login instead and the data …

Activate virtual environement and start jupyter notebook all in batch file

I created the following batch file: jupyter_nn.bat. Inside file I have:cd "C:\My_favorite_path" activate neuralnets jupyter notebookSo the goal is to activate conda virtual environment and s…

several contour plots in the same figures

I have several 3d functions. I would like two plot the contour plots of them in the same figure to see the difference between them. I expect to see some crossings between contours of two functions. Her…

how to detect all the rectangular boxes in the given image

I tried to detect all the rectangles in image using threshold, canny edge and applied contour detection but it was not able to detect all the rectangles. Finally, I thought of detect the same using hou…