- Data serialization is the process of converting information into a structured format so that it can be stored, transferred, shared, and processed by computer programs. In bioinformatics, biological information is collected from many databases, research projects, laboratory experiments, and computational analyses. This information may include DNA sequences, protein records, gene annotations, genetic variants, sample descriptions, gene expression measurements, and molecular structures. Serialization makes it possible to represent this information in standard formats that different software systems can understand.
- Biological databases and bioinformatics applications use several data serialization formats. Three important groups are JSON (JavaScript Object Notation), XML (Extensible Markup Language), and tabular formats such as CSV (Comma-Separated Values), TSV (Tab-Separated Values), and other structured text files. These formats are widely used for exchanging biological records, retrieving information through APIs, storing experimental results, and transferring data between computational tools.
- JSON is a lightweight data format used to represent information as objects, arrays, key-value pairs, numbers, strings, and Boolean values. It is particularly common in modern web services and REST APIs because it is relatively compact and easy for programming languages to process. Many biological databases return JSON responses when researchers retrieve information through their APIs.
- A JSON object contains information organized into key-value pairs. A key identifies a particular field, while its value contains the corresponding information. For example, a biological record may contain a gene identifier, gene name, organism, chromosome, and genomic coordinates. These fields can be represented in a single JSON object.
- The following example shows a simple JSON record for a gene:
{
“gene_id”: “GENE001”,
“gene_name”: “BRCA1”,
“organism”: “Homo sapiens”,
“chromosome”: “17”,
“start”: 43044295,
“end”: 43170245
}
- This example illustrates how different properties of a gene can be stored in a structured format. The values can be accessed individually by using their corresponding keys. A collection of several gene records can be represented using a JSON array.
{
“genes”: [
{
“gene_id”: “GENE001”,
“gene_name”: “BRCA1”,
“organism”: “Homo sapiens”
},
{
“gene_id”: “GENE002”,
“gene_name”: “TP53”,
“organism”: “Homo sapiens”
}
]
}
- JSON supports nested structures, which means that one object can contain other objects or arrays. This is useful in bioinformatics because biological information often has hierarchical relationships. A gene may contain information about transcripts, transcripts may contain exon records, and proteins may contain domains and functional annotations. JSON can represent these relationships within a single structured response.
- Python provides built-in support for reading and writing JSON through its json module. The json.load() function reads JSON data from a file, while json.dump() writes Python objects into a JSON file. The json.loads() and json.dumps() functions are used when working with JSON data stored as strings.
- The following Python example reads a JSON file:
import json
with open(“gene_data.json”, “r”) as file:
data = json.load(file)
print(data[“gene_name”])
print(data[“organism”])
- The following example creates and saves a JSON record:
import json
gene = {
“gene_id”: “GENE001”,
“gene_name”: “BRCA1”,
“organism”: “Homo sapiens”,
“chromosome”: “17”
}
with open(“gene_data.json”, “w”) as file:
json.dump(gene, file, indent=4)
print(“JSON file created successfully.”)
- The indent=4 option makes the resulting file easier to read by adding indentation. Python libraries such as requests can also retrieve JSON responses directly from biological APIs, while pandas can convert suitable JSON records into tabular data for analysis.
- R can also read and write JSON using packages such as jsonlite. The fromJSON() function converts JSON data into R objects, while toJSON() converts R objects into JSON representations.
- The following R example reads a JSON file:
library(jsonlite)
data <- fromJSON(“gene_data.json”)
print(datagenename)print(dataorganism)
- The following example creates a JSON file from an R list:
library(jsonlite)
gene <- list(
gene_id = “GENE001”,
gene_name = “BRCA1”,
organism = “Homo sapiens”,
chromosome = “17”
)
write_json(
gene,
“gene_data.json”,
pretty = TRUE,
auto_unbox = TRUE
)
print(“JSON file created successfully.”)
- XML is another important format for storing and exchanging structured biological information. XML stands for Extensible Markup Language. It uses tags and nested elements to describe the organization and meaning of data. Unlike JSON, which primarily uses objects and arrays, XML represents information through opening and closing tags, attributes, and hierarchical elements.
- XML has been widely used in biological databases, scientific data exchange, web services, and structured biological record formats. For example, NCBI provides sequence records in XML through its Entrez utilities. XML can represent complex biological records containing sequence information, organism details, references, features, annotations, and links to other database records.
- The following example illustrates a simple XML representation of a gene record:
<?xml version=”1.0″ encoding=”UTF-8″?>
<gene>
<gene_id>GENE001</gene_id>
<gene_name>BRCA1</gene_name>
<organism>Homo sapiens</organism>
<chromosome>17</chromosome>
<start>43044295</start>
<end>43170245</end>
</gene>
- In this example, each piece of information is enclosed within an XML element. The gene element is the parent element, while gene_id, gene_name, organism, and other elements are its children. XML supports nested elements, allowing complex biological information to be organized into multiple levels.
- XML elements can also contain attributes. Attributes provide additional information about an element and are written inside its opening tag. For example:
<gene id=”GENE001″ organism=”Homo sapiens”>
<gene_name>BRCA1</gene_name>
<chromosome>17</chromosome>
</gene>
- XML is useful when data have complex structures or when a database provides a defined XML schema. An XML schema specifies the permitted elements, attributes, data types, and relationships within a document. Document Type Definitions (DTDs) and XML Schema Definition (XSD) files are examples of mechanisms used to describe or validate XML structures.
- Python provides several tools for processing XML files. The built-in xml.etree.ElementTree module can read XML documents, search for elements, extract values, and create XML files. The following example reads a simple XML gene record:
import xml.etree.ElementTree as ET
tree = ET.parse(“gene_data.xml”)
root = tree.getroot()
gene_name = root.find(“gene_name”).text
organism = root.find(“organism”).text
print(gene_name)
print(organism)
The following Python example creates an XML file:
import xml.etree.ElementTree as ET
root = ET.Element(“gene”)
ET.SubElement(root, “gene_id”).text = “GENE001”
ET.SubElement(root, “gene_name”).text = “BRCA1”
ET.SubElement(root, “organism”).text = “Homo sapiens”
ET.SubElement(root, “chromosome”).text = “17”
tree = ET.ElementTree(root)
tree.write(
“gene_data.xml”,
encoding=”utf-8″,
xml_declaration=True
)
print(“XML file created successfully.”)
- R also supports XML processing through packages such as xml2. The read_xml() function reads an XML document, while functions such as xml_find_first() and xml_text() can locate elements and extract their values.
- The following example reads an XML file in R:
library(xml2)
doc <- read_xml(“gene_data.xml”)
gene_name <- xml_text(
xml_find_first(doc, “.//gene_name”)
)
organism <- xml_text(
xml_find_first(doc, “.//organism”)
)
print(gene_name)
print(organism)
- Tabular formats are among the simplest and most widely used ways of storing biological data. They organize information into rows and columns. Each row generally represents one biological record, while each column represents a particular variable or characteristic. Common tabular formats include CSV and TSV.
- CSV stands for Comma-Separated Values. In this format, values in each row are separated by commas. It is commonly used for gene expression measurements, sample information, experimental results, and statistical data. The following example illustrates a CSV file containing gene information:
gene_id,gene_name,organism,chromosome
GENE001,BRCA1,Homo sapiens,17
GENE002,TP53,Homo sapiens,17
GENE003,EGFR,Homo sapiens,7
- TSV stands for Tab-Separated Values. It has a similar structure to CSV, but values are separated by tab characters instead of commas. TSV is frequently used in bioinformatics because biological annotations may contain commas within text fields. Tab separation can make certain datasets easier to process.
- The following example illustrates a TSV file:
gene_id gene_name organism chromosome
GENE001 BRCA1 Homo sapiens 17
GENE002 TP53 Homo sapiens 17
GENE003 EGFR Homo sapiens 7
- Tabular files are particularly useful for representing large datasets in which each record has a similar structure. For example, a gene expression matrix may contain genes in rows and samples in columns. A variant annotation table may contain chromosome, position, reference allele, alternative allele, gene name, and predicted consequence. A protein annotation table may contain accession numbers, protein names, organisms, and functional descriptions.
- Python provides several ways to read and write tabular files. The built-in csv module can process CSV files, while pandas provides powerful tools for importing, filtering, organizing, and analyzing tabular datasets. The read_csv() function can read both CSV and TSV files by specifying the appropriate separator.
- The following example reads a CSV file using pandas:
import pandas as pd
data = pd.read_csv(“gene_data.csv”)
print(data.head())
print(data[“gene_name”])
To read a TSV file, the separator can be specified using sep:
import pandas as pd
data = pd.read_csv(
“gene_data.tsv”,
sep=”\t”
)
print(data.head())
A Python program can also create a CSV file from a data frame:
import pandas as pd
data = pd.DataFrame({
“gene_id”: [“GENE001”, “GENE002”],
“gene_name”: [“BRCA1”, “TP53”],
“organism”: [“Homo sapiens”, “Homo sapiens”]
})
data.to_csv(
“gene_data.csv”,
index=False
)
print(“CSV file created successfully.”)
- R provides built-in functions for reading and writing tabular files. The read.csv() function reads CSV files, while read.delim() reads tab-separated files. The write.csv() and write.table() functions can be used to save data in corresponding formats.
- The following example reads a CSV file in R:
data <- read.csv(“gene_data.csv”)
print(head(data))
print(data$gene_name)
To read a TSV file, researchers can use:
data <- read.delim(“gene_data.tsv”)
print(head(data))
The following example writes an R data frame to a CSV file:
data <- data.frame(
gene_id = c(“GENE001”, “GENE002”),
gene_name = c(“BRCA1”, “TP53”),
organism = c(“Homo sapiens”, “Homo sapiens”)
)
write.csv(
data,
“gene_data.csv”,
row.names = FALSE
)
print(“CSV file created successfully.”)
- Different serialization formats are suitable for different types of biological information. JSON is useful for API communication and nested data structures. XML is suitable for structured documents with complex hierarchies and defined schemas. CSV and TSV are convenient for tables, statistical analysis, and exchanging datasets between different software applications. The choice depends on the structure of the information, the requirements of the database, and the tools used for analysis.
- Bioinformatics researchers frequently need to convert data between these formats. For example, an API may return a gene annotation in JSON, which can then be converted into a CSV file for statistical analysis. An XML sequence record may be parsed to extract nucleotide sequences and annotations. A TSV file containing gene identifiers may be converted into JSON to support communication with another software application. Such conversions make it easier to combine information from different biological resources.
- Data serialization also requires attention to data types and missing values. Gene identifiers and accession numbers should generally be treated as text rather than numbers because they may contain letters, punctuation, or leading zeros. Missing values should be represented consistently, and researchers should avoid confusing an empty value with a true biological value. Numerical fields such as genomic coordinates and expression measurements should be stored using suitable numeric types. Dates, Boolean values, and nested structures should also follow clearly defined conventions.
- Large biological datasets may require more efficient storage formats than ordinary JSON, XML, or CSV files. Formats such as Parquet and HDF5 can support efficient storage and access for large structured datasets. HDF5 is useful for hierarchical scientific data, including large matrices and multi-dimensional biological measurements. Parquet is a column-oriented format that can provide efficient storage and retrieval for large tabular datasets. Other specialized formats, such as BAM, CRAM, VCF, and FASTQ, are designed for particular types of biological information and should not be confused with general-purpose serialization formats.
- Data validation is essential when exchanging serialized information. A JSON or XML document may be syntactically valid but still contain incorrect biological information. Similarly, a CSV file may have missing columns, inconsistent identifiers, or incorrectly formatted values. Researchers should validate the structure of retrieved data and check whether required fields are present. JSON Schema can describe expected JSON structures, while XML Schema can be used to validate XML documents. Tabular datasets can be checked for column names, data types, missing values, and duplicate records.
- Reproducibility also depends on documenting the format and structure of biological data. Researchers should record the source database, retrieval date, file format, data version, genome assembly, and any conversion or filtering steps. When converting between formats, they should preserve important identifiers, annotations, and relationships. Some formats may not support all the information contained in the original data, so conversion can result in information loss if the process is not carefully designed.
- Data serialization in JSON, XML, and tabular formats is fundamental to biological data exchange and computational analysis. JSON provides a flexible structure for API responses, XML supports hierarchical biological records, and CSV and TSV provide simple formats for storing and analyzing tabular information. By understanding these formats and using Python and R libraries to read, write, validate, and convert them, bioinformatics researchers can integrate biological data from different databases and develop reliable, reproducible computational workflows.