Bioinformatics Programming
How to execute Unix/shell commands in a Perl script?
BioPerl is a collection of Perl modules that are used to write Perl scripts applied in bioinformatics [1]. It is used in bioinformatics programmings such as in developing source codes, standalone software/tools, and algorithms. It’s easy to install and provide various modules which make it easier to execute different functions. However, Python is mostly preferred over the Perl language, still, some of the bioinformatics software is based on Perl such as the standalone version of I-TASSER. Sometimes, it’s a big trouble to execute some Unix/shell commands in Perl script for the beginners, it’s difficult to decide which function would be specific to a condition. Therefore, this article is a guide for the execution of Unix/shell commands in Perl script.
Perl offers different functions and operators to execute external commands (described as follows), which are special in their own ways. They are slightly different from each other, which sometimes makes it hard to choose for the beginners in Perl.
1. exec””
syntax: exec "command";
It is a Perl function (perlfunc) which executes a command but never returns, as same as the return
statement in a function. It keeps continuing processing the script while executing the command and doesn’t wait for it to finish first, it returns false when the command is not found but never returns true.
2. system()
syntax: system( "command" );
It is also a Perl function (perlfunc) that executes a command and waits for it to get finished first and then resume the Perl script. The return value is the exit status of the command. It can be called in two ways:
system( "command arg1 arg2" );
OR
system( "command", "arg1", "arg2" );
3. Backticks “ or qx//
syntax: `command`;
syntax: qx/command/;
It is a Perl operator (perlop) very similar to system()
, execute a command, and then resumes the Perl script after the execution has finished but the return value is STDOUT of the command.
4. IPC::Open2
syntax: $output = open2(\*CHLD_OUT, \*CHLD_IN, 'command arg1 arg2'
);
It runs a process for both reading and writing and creates a pipe to both STDIN and STDOUT. It can be used in both ways:
$output = open2(my $out, my $in, 'command arg1 arg2');
OR without using the shell
$output = open2(my $out, my $in, 'command', 'arg1', 'arg2');
You can read about it more in the documentation: IPC::Open2.
5. IPC::Open3
syntax: $output = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, 'command arg1 arg2', 'optarg',...);
It is very similar to
IPC::Open2
with the capability to capture all three file handles of the process, i.e., STDIN, STDOUT, and STDERR. It can also be used with or without the shell. You can read about it more in the documentation: IPC::Open3.
$output = open3(my $out, my $in, 'command arg1 arg2');
OR without using the shell
$output = open3(my $out, my $in, 'command', 'arg1', 'arg2');
a2p
syntax: a2p [options] [awkscript]
There is a Perl utility known as a2p
which translates awk
command to Perl. It takes awk script as input and generates a comparable Perl script as the output. Suppose, you want to execute the following awk
statement
awk '{$1 = ""; $2 = ""; print}' f1.txt
This statement gives error sometimes even after escaping the variables (\$1, \$2) but by using a2p
it can be easily converted to Perl script:
#!/usr/local/bin/perl
eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator
while (<>) {
chomp; # strip record separator
@Fld = split(' ', $_, -1);
if ($awk) {
$Fld[(1)-1] = '';
$Fld[(2)-1] = '';
print join($,,@Fld);
}
print join($,,@Fld) if $f1 . . $txt;
}
For further information, you can read it’s documentation: a2p
References
- Stajich, J. E.; Block, D.; Boulez, K.; Brenner, S.; Chervitz, S.; Dagdigian, C.; Fuellen, G.; Gilbert, J.; Korf, I.; Lapp, H.; Lehväslaiho, H.; Matsalla, C.; Mungall, C. J.; Osborne, B. I.; Pocock, M. R.; Schattner, P.; Senger, M.; Stein, L. D.; Stupka, E.; Wilkinson, M. D.; Birney, E. (2002). “The BioPerl Toolkit: Perl Modules for the Life Sciences”. Genome Research. 12(10): 1611–1618.
Bioinformatics Programming
How to create a pie chart using Python?
In this article. we are creating a pie chart of the docking score of five different compounds docked with the same protein. (more…)
Bioinformatics Programming
How to make swarm boxplot?
With the new year, we are going to start with a very simple yet complicated topic (for beginners) in bioinformatics. In this tutorial, we provide a simple code to plot swarm boxplot using matplotlib and seaborn. (more…)
Bioinformatics Programming
How to obtain ligand structures in PDB format from PDB ligand IDs?
Previously, we provided a similar script to download ligand SMILES from PDB ligand IDs. In this article, we are downloading PDB ligand structures from their corresponding IDs. (more…)
Bioinformatics Programming
How to obtain SMILES of ligands using PDB ligand IDs?
Fetching SMILE strings for a given number of SDF files of chemical compounds is not such a trivial task. We can quickly obtain them using RDKit or OpenBabel. But what if you don’t have SDF files of ligands in the first place? All you have is Ligand IDs from PDB. If they are a few then you can think of downloading SDF files manually but still, it seems time-consuming, especially when you have multiple compounds to work with. Therefore, we provide a Python script that will read all Ligand IDs and fetch their SDF files, and will finally convert them into SMILE strings. (more…)
Bioinformatics Programming
How to get secondary structure of multiple PDB files using DSSP in Python?
In this article, we will obtain the secondary structure of multiple PDB files present in a directory using DSSP [1]. You need to have DSSP installed on your system. (more…)
Bioinformatics Programming
vs_analysis_compound.py: Python script to search for binding affinities based on compound names.
Previously, we have provided the vs_analysis.py script to analyze virtual screening (VS) results obtained from Autodock Vina. In this article, we have provided another script to search for binding affinity associated with a compound. (more…)
Bioinformatics Programming
How to download files from an FTP server using Python?
In this article, we provide a simple Python script to download files from an FTP server using Python. (more…)
Bioinformatics Programming
How to convert the PDB file to PSF format?
VMD allows converting PDB to PSF format but sometimes it gives multiple errors. Therefore, in this article, we are going to convert PDB into PSF format using a different method. (more…)
Bioinformatics Programming
smitostr.py: Python script to convert SMILES to structures.
As mentioned in some of our previous articles, RDKit provides a wide range of functions. In this article, we are using RDKit [1] to draw a molecular structure using SMILES. (more…)
Bioinformatics Programming
How to preprocess data for clustering in MATLAB?
Data preprocessing is a foremost and essential step in clustering based on machine learning methods. It removes noise and provides better results. In this article, we are going to discuss the steps involved in data preprocessing using MATLAB [1]. (more…)
Bioinformatics Programming
How to calculate drug-likeness using RDKit?
RDKit [1] allows performing multiple functions on chemical compounds. One is the quantitative estimation of drug-likeness also known as QED properties. These properties include molecular weight (MW), octanol-water partition coefficient (ALOGP), number of hydrogen bond donors (HBD), number of hydrogen bond acceptors (HBA), polar surface area (PSA), number of rotatable bonds (ROTB), number of aromatic rings (AROM), structural alerts (ALERTS). (more…)
Bioinformatics Programming
sdftosmi.py: Convert multiple ligands/compounds in SDF format to SMILES.
You can obtain SMILES of multiple compounds or ligands in an SDF file in one go. Here, we provide a simple Python script to do that. (more…)
Bioinformatics Programming
tanimoto_similarities_one_vs_all.py – Python script to calculate Tanimoto Similarities of multiple compounds
We previously provided a Python script to calculate the Tanimoto similarities of multiple compounds against each other. In this article, we are providing another Python script to calculate the Tanimoto similarities of one compound with multiple compounds. (more…)
Bioinformatics Programming
tanimoto_similarities.py: A Python script to calculate Tanimoto similarities of multiple compounds using RDKit.
RDKit [1] is a very nice cheminformatics software. It allows us to perform a wide range of operations on chemical compounds/ ligands. We have provided a Python script to perform fingerprinting using Tanimoto similarity on multiple compounds using RDKit. (more…)
Bioinformatics Programming
How to commit changes to GitHub repository using vs code?
In this article, we are providing a few commands that are used to commit changes to GitHub repositories using VS code terminal.
Bioinformatics Programming
Extracting first and last residue from helix file in DSSP format.
Bioinformatics Programming
How to extract x,y,z coordinates of atoms from PDB file?
The x, y, and z coordinates of atoms are provided in the PDB file. One way to extract them is by using the Biopython package [1]. In this article, we will extract coordinates of C-alpha atoms for each residue from the PDB file using Biopython. (more…)
Bioinformatics Programming
dssp_parser: A new Python package to extract helices from DSSP files.
A new Python package named ‘dssp_parser‘ is developed to parse DSSP files. This package fetches all helices including long and short ones from DSSP files. (more…)
Bioinformatics Programming
How to calculate center of mass of a protein structure using Python script?
Here is a Python script that helps you calculate the center of mass of a protein using the Pymol [1]. (more…)
Bioinformatics Programming
How to sort binding affinities based on a cutoff using vs_analysis.py script?
Previously, we have provided a Python script (vs_analysis.py) to analyze the virtual screening (VS) results of Autodock Vina. Now, we have updated this script to sort binding affinities based on user inputted cutoff value. (more…)
[email protected]
November 10, 2021 at 2:24 am
I have been using qq to run linux programs inside perl.
And it works well.
system qq($adduser –home $home –gecos “$fullname” $username);
system qq($edquota -p john $username);
system qq($chage -E \$(date -d +180days +%Y-%m-%d) $username);
system qq($chage -l $username);
system qq($quota -s $username);