Connect with us

Bioinformatics Programming

Nested SQL queries and aggregate functions for complex information retrieval from a database

Dr. Muniba Faiza

Published

on

SQL queries for complex information retrieval

In one of our previous articles, we have mentioned a few basic SQL queries to search in a database. In this article, we have described a few nested SQL queries and aggregate functions for information retrieval from a biological database.

1. To retrieve a limited number of records from a database.

>SELECT TOP number * FROM table_name;

For example, you have a table named sequences in your database having different columns such as OrgName, Sequence, OrgNumber, and so on. Since a single species may consist of multiple sequences and you want to retrieve only the top 3 sequences of that species, then use the following query.

>SELECT TOP 3 * FROM sequences;

In MySQL, the LIMIT clause does the same thing,

>SELECT column_names * FROM table_name

WHERE condition LIMIT number;

For example,

>SELECT * FROM sequences

WHERE OrgName=’Arabidopsis thaliana’

LIMIT 3;

2. To retrieve the minimum and maximum values from the selected column

To find the minimum value

>SELECT MIN(column_name) FROM table_name

WHERE condition;

For example,

>SELECT MIN(strains) AS MinimumStrains

FROM sequences;

To find the maximum value

>SELECT MAX(column_name) FROM table_name

WHERE condition;

For example,

>SELECT MAX(strains) AS MinimumStrains

FROM sequences

WHERE OrgName=’Arabidopsis thaliana’;

3. Aggregate functions in SQL

To find the number of rows matching a condition

>SELECT COUNT(column_name)

FROM table_name

WHERE condition;

For example,

>SELECT COUNT(OrgNumber)

FROM sequences;

To find the average value of a numeric column in a table

>SELECT AVG(column_name)

FROM table_name

WHERE condition;

For example,

>SELECT AVG(TotSeqs)

FROM sequences;

To find the sum of a numeric column

>SELECT SUM(column_name)

FROM table_name

WHERE condition;

For example,

>SELECT SUM(TotSeqs)

FROM sequences;

4. To search for a specific pattern in a column

>SELECT column1, column2, …

FROM table_name

WHERE column_name LIKE pattern;

There are several other wildcards that can be used with LIKE operator such as ‘*’, ‘[]’, ‘^’, etc. They can be used in combination as well. There are two wildcards mostly used with LIKE operator: ‘%’ and ‘_’.

% represents zero, one, or multiple characters, whereas, _ represents a single character. These wildcards are used before and/or after the specific characters or a pattern you are looking for.

For example, if you wish to search for sequences of a species using a few characters, then use the following query.

>SELECT Sequence, OrgName

FROM sequences

WHERE OrgName LIKE ‘%dopsis%’;

>SELECT Sequence, OrgName

FROM sequences

WHERE OrgName LIKE ‘%thaliana’;

>SELECT Sequence, OrgName

FROM sequences

WHERE OrgName LIKE ‘_ _abidop%’;

5. To specify multiple values in a WHERE clause

>SELECT column1, column2, …

FROM table_name

WHERE column_name IN (value1, value2, …);

For example,

>SELECT * FROM sequences

WHERE OrgName IN (‘Arabidopsis thaliana’, ‘Agrocybe aegerita’, ‘Homo sapiens’);

Or instead of entering values, SELECT statement can also be used as shown below,

>SELECT * FROM sequences

WHERE OrgName IN (SELECT statement);

If you wish to select records that are not present in the given values, then use the following query.

>SELECT * FROM sequences

WHERE OrgName NOT IN (‘Arabidopsis thaliana’, ‘Agrocybe aegerita’, ‘Homo sapiens’);

6. To select records within a given range in a table

>SELECT column1, column2, …

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

For example,

>SELECT * FROM sequences

WHERE OrgNumber BETWEEN 102 AND 130;

Other SQL queries for complex information retrieval from a database will be described in upcoming articles.

Dr. Muniba is a Bioinformatician based in New Delhi, India. She has completed her PhD in Bioinformatics from South China University of Technology, Guangzhou, China. She has cutting edge knowledge of bioinformatics tools, algorithms, and drug designing. When she is not reading she is found enjoying with the family. Know more about Muniba

Advertisement
Click to comment

You must be logged in to post a comment Login

Leave a Reply

Bioinformatics Programming

How to create a pie chart using Python?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to make swarm boxplot?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to obtain ligand structures in PDB format from PDB ligand IDs?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to obtain SMILES of ligands using PDB ligand IDs?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to get secondary structure of multiple PDB files using DSSP in Python?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

vs_analysis_compound.py: Python script to search for binding affinities based on compound names.

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to download files from an FTP server using Python?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to convert the PDB file to PSF format?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

smitostr.py: Python script to convert SMILES to structures.

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to preprocess data for clustering in MATLAB?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to calculate drug-likeness using RDKit?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

sdftosmi.py: Convert multiple ligands/compounds in SDF format to SMILES.

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

tanimoto_similarities_one_vs_all.py – Python script to calculate Tanimoto Similarities of multiple compounds

Dr. Muniba Faiza

Published

on

tanimoto_similarities_one_vs_all.py – Python script to calculate Tanimoto Similarities of a compound with 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…)

Continue Reading

Bioinformatics Programming

tanimoto_similarities.py: A Python script to calculate Tanimoto similarities of multiple compounds using RDKit.

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to commit changes to GitHub repository using vs code?

Published

on

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.

(more…)

Continue Reading

Bioinformatics Programming

Extracting first and last residue from helix file in DSSP format.

Dr. Muniba Faiza

Published

on

Extracting first and last residue from helix file in DSSP format.

Previously, we have provided a tutorial on using dssp_parser to extract all helices including long and short separately. Now, we have provided a new python script to find the first and last residue in each helix file. (more…)

Continue Reading

Bioinformatics Programming

How to extract x,y,z coordinates of atoms from PDB file?

Published

on

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…)

Continue Reading

Bioinformatics Programming

dssp_parser: A new Python package to extract helices from DSSP files.

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to calculate center of mass of a protein structure using Python script?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

Bioinformatics Programming

How to sort binding affinities based on a cutoff using vs_analysis.py script?

Dr. Muniba Faiza

Published

on

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…)

Continue Reading

LATEST ISSUE

ADVERT