跳到正文
cccskills
免费在 GitHub 公开

bio-read-sequences

Read biological sequence files with Biopython Bio.SeqIO and extract sequence IDs, quality scores, and annotations. Process records individually, retrieve sequences by ID from large files, or use lightweight parsers for faster reading.

中文简介正在准备中。当前显示英语简介。

原文查看原文说明

Read biological sequence files (FASTA, FASTQ, GenBank, EMBL, ABI, SFF) using Biopython Bio.SeqIO. Use when parsing sequence files, iterating multi-sequence files, random access to large files, or high-performance parsing.

查看安装方法

适用场景

  • Listing sequence IDs and lengths from FASTA files
  • Reading quality scores from FASTQ or ABI files
  • Extracting annotated coding sequences from GenBank files
  • Retrieving records by ID from large sequence files

包含的文件(11)

  • SKILL.md10.3 KB
  • examples/basic_parsing.py932 B
  • examples/fastq_quality.py939 B
  • examples/genbank_features.py855 B
  • examples/random_access.py1018 B
  • examples/sample.fasta277 B
  • examples/sample.fastq418 B
  • examples/sample.gb1.2 KB
  • examples/single.fasta106 B
  • tests.json2.6 KB
  • usage-guide.md1.8 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Version Compatibility

Reference examples tested with: BioPython 1.83+

Before using code patterns, verify installed versions match. If versions differ:

  • Python: pip show biopython then help(module.function) to check signatures

If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.

Read Sequences

Read biological sequence data from files using Biopython's Bio.SeqIO module.

"Read sequences from a file" → Parse file into a collection of SeqRecord objects with IDs, sequences, and annotations accessible.

  • Python: SeqIO.parse() or SeqIO.read() (BioPython)
  • R: readDNAStringSet() or readAAStringSet() (Biostrings)

Required Import

Core import

from Bio import SeqIO

Core Functions

SeqIO.parse() - Multiple Records

Use for files with one or more sequences. Returns an iterator of SeqRecord objects.

for record in SeqIO.parse('sequences.fasta', 'fasta'):
    print(record.id, len(record.seq))

Important: Always specify the format explicitly as the second argument.

SeqIO.read() - Single Record

Use when file contains exactly one sequence. Raises error if zero or multiple records.

record = SeqIO.read('single.fasta', 'fasta')

SeqIO.to_dict() - Load All Into Memory

Use for random access by record ID. Loads entire file into memory.

records = SeqIO.to_dict(SeqIO.parse('sequences.fasta', 'fasta'))
seq = records['sequence_id'].seq

SeqIO.index() - Large File Random Access

Use for large files when random access is needed without loading everything into memory.

records = SeqIO.index('large.fasta', 'fasta')
seq = records['sequence_id'].seq
records.close()

SeqIO.index_db() - SQLite-Backed Indexing

Use for very large files or multiple files. Creates persistent SQLite index.

# Create index (first time - parses file)
records = SeqIO.index_db('index.sqlite', 'large.fasta', 'fasta')
seq = records['sequence_id'].seq
records.close()

# Reuse existing index (instant load)
records = SeqIO.index_db('index.sqlite')

# Index multiple files together
records = SeqIO.index_db('combined.sqlite', ['file1.fasta', 'file2.fasta'], 'fasta')

Advantages over index():

  • Persistent index survives program restarts
  • Can index multiple files as one database
  • Lower memory for extremely large files
  • SQLite file can be shared across processes

High-Performance Parsing

For maximum throughput on large files, use low-level parsers (3-6x faster than SeqIO.parse):

SimpleFastaParser

Goal: Parse large FASTA files at maximum speed without SeqRecord overhead.

Approach: Use low-level tuple-based parser returning (title, sequence) strings.

Reference (BioPython 1.83+):

from Bio.SeqIO.FastaIO import SimpleFastaParser

with open('large.fasta') as handle:
    for title, sequence in SimpleFastaParser(handle):
        if len(sequence) > 1000:
            print(title.split()[0])  # First word is usually ID

Returns (title, sequence) tuples as strings (no SeqRecord overhead).

FastqGeneralIterator

Goal: Parse large FASTQ files at maximum speed.

Approach: Use low-level tuple-based parser returning (title, sequence, quality_string) strings.

Reference (BioPython 1.83+):

from Bio.SeqIO.QualityIO import FastqGeneralIterator

with open('reads.fastq') as handle:
    for title, sequence, quality in FastqGeneralIterator(handle):
        avg_qual = sum(ord(c) - 33 for c in quality) / len(quality)

Returns (title, sequence, quality_string) tuples.

Common Formats

FormatStringTypical ExtensionNotes
FASTA'fasta'.fasta, .fa, .fna, .faaMost common
FASTA 2-line'fasta-2line'.fastaOne line per sequence (no wrapping)
FASTQ'fastq'.fastq, .fqWith quality scores
FASTQ Solexa'fastq-solexa'.fastqOld Solexa/Illumina (pre-1.3)
FASTQ Illumina'fastq-illumina'.fastqIllumina 1.3-1.7
GenBank'genbank' or 'gb'.gb, .gbkWith features/annotations
EMBL'embl'.emblEuropean format with features
Swiss-Prot'swiss'.datUniProt format

Specialized Formats

FormatStringUse Case
ABI'abi'Sanger sequencing trace files (.ab1)
ABI Trimmed'abi-trim'ABI with low-quality ends trimmed
SFF'sff'454/Ion Torrent flowgram data
SFF Trimmed'sff-trim'SFF with adapter/quality trimming
QUAL'qual'Quality scores file (pairs with FASTA)
PHD'phd'Phred/Phrap/Consed output
ACE'ace'Assembly format (Consed)
PDB SEQRES'pdb-seqres'Protein sequences from PDB files
PDB ATOM'pdb-atom'Sequences from ATOM records in PDB
SnapGene'snapgene'SnapGene .dna files
GCK'gck'Gene Construction Kit files
XDNA'xdna'DNA Strider / SerialCloner files

Reading ABI Trace Files

# Read Sanger sequencing trace with quality
record = SeqIO.read('sample.ab1', 'abi')
print(f'Sequence: {record.seq}')
qualities = record.letter_annotations['phred_quality']

# Auto-trim low quality ends
record_trimmed = SeqIO.read('sample.ab1', 'abi-trim')

Reading 454/Ion Torrent SFF

for record in SeqIO.parse('reads.sff', 'sff'):
    print(record.id, len(record.seq))

# With trimming applied
for record in SeqIO.parse('reads.sff', 'sff-trim'):
    print(record.id, len(record.seq))

Reading PDB Sequences

# Get sequences from SEQRES records
for record in SeqIO.parse('structure.pdb', 'pdb-seqres'):
    print(f'Chain {record.id}: {record.seq}')

# Get sequences from ATOM coordinates
for record in SeqIO.parse('structure.pdb', 'pdb-atom'):
    print(f'Chain {record.id}: {record.seq}')

Alignment Formats (Read-Only)

FormatStringNotes
PHYLIP'phylip'Interleaved phylip
PHYLIP Sequential'phylip-sequential'Sequential phylip
PHYLIP Relaxed'phylip-relaxed'Longer names allowed
Clustal'clustal'ClustalW output
Stockholm'stockholm'Rfam/Pfam alignments
NEXUS'nexus'PAUP/MrBayes format
MAF'maf'Multiple Alignment Format

SeqRecord Object Attributes

After parsing, each record has these key attributes:

record.id          # Sequence identifier (string)
record.name        # Sequence name (string)
record.description # Full description line (string)
record.seq         # Sequence data (Seq object)
record.features    # List of SeqFeature objects (GenBank/EMBL)
record.annotations # Dictionary of annotations
record.letter_annotations  # Per-letter annotations (quality scores)
record.dbxrefs     # Database cross-references

Code Patterns

Collect All Sequences Into a List

records = list(SeqIO.parse('sequences.fasta', 'fasta'))

Count Records Without Loading All

count = sum(1 for _ in SeqIO.parse('sequences.fasta', 'fasta'))

Fast Count (FASTA only)

from Bio.SeqIO.FastaIO import SimpleFastaParser
with open('sequences.fasta') as f:
    count = sum(1 for _ in SimpleFastaParser(f))

Get Sequence IDs Only

ids = [record.id for record in SeqIO.parse('sequences.fasta', 'fasta')]

Read GenBank with Features

for record in SeqIO.parse('sequence.gb', 'genbank'):
    for feature in record.features:
        if feature.type == 'CDS':
            print(feature.qualifiers.get('product', ['Unknown'])[0])
            cds_seq = feature.extract(record.seq)  # Get feature sequence

Access FASTQ Quality Scores

for record in SeqIO.parse('reads.fastq', 'fastq'):
    qualities = record.letter_annotations['phred_quality']
    avg_quality = sum(qualities) / len(qualities)

Read From File Handle

with open('sequences.fasta', 'r') as handle:
    for record in SeqIO.parse(handle, 'fasta'):
        print(record.id)

Custom ID Function for Indexing

def get_accession(identifier):
    return identifier.split('.')[0]  # Remove version

records = SeqIO.index('sequences.fasta', 'fasta', key_function=get_accession)

Common Errors

ErrorCauseSolution
ValueError: More than one recordUsed read() on multi-record fileUse parse() instead
ValueError: No records foundUsed read() on empty fileCheck file exists and has content
ValueError: unknown formatTypo in format stringCheck format string spelling
UnicodeDecodeErrorBinary file or wrong encodingOpen with encoding='latin-1' or check file
sqlite3.OperationalErrorindex_db file lockedClose other connections first

Decision Tree

Need to read sequences?
├── Single record in file?
│   └── Use SeqIO.read()
├── Multiple records?
│   ├── Need all in memory at once?
│   │   └── Use list(SeqIO.parse()) or SeqIO.to_dict()
│   ├── Process one at a time (memory efficient)?
│   │   └── Use SeqIO.parse() iterator
│   ├── Large file, need random access by ID?
│   │   ├── Single session? → Use SeqIO.index()
│   │   └── Persistent/multi-file? → Use SeqIO.index_db()
│   └── Maximum throughput needed?
│       └── Use SimpleFastaParser or FastqGeneralIterator
├── Sanger sequencing trace?
│   └── Use 'abi' or 'abi-trim' format
├── 454/Ion Torrent data?
│   └── Use 'sff' or 'sff-trim' format
└── Protein from structure?
    └── Use 'pdb-seqres' or 'pdb-atom' format

Related Skills

  • write-sequences - Write parsed sequences to new files
  • filter-sequences - Filter sequences by criteria after reading
  • format-conversion - Convert between formats
  • compressed-files - Read gzip/bzip2/BGZF compressed sequence files
  • sequence-manipulation/seq-objects - Work with parsed SeqRecord objects
  • database-access - Fetch sequences from NCBI instead of local files
  • alignment-files - For SAM/BAM/CRAM alignment files, use samtools/pysam

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

adaptyv

免费

Support protein design with cloud laboratory testing for binding, expression, thermal stability, and enzyme activity. Covers sequence optimization, submitting experiments through the Adaptyv API, checking progress, and downloading experimental results.

中文简介正在准备中。当前显示英语简介。

  • Validate designed proteins with laboratory assays
  • Screen and redesign sequences for better solubility
  • Submit protein testing experiments through an API
FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

Time-blind friendly planning, executive function support, and daily structure for ADHD brains. Specializes in realistic time estimation, dopamine-aware task design, and building systems that actually work for neurodivergent minds.

中文简介正在准备中。当前显示原文说明。

FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

aeon

免费

Build Python workflows for data recorded over time, including classification, forecasting, anomaly detection, and pattern search. Using aeon, the skill guides preprocessing, algorithm selection, feature extraction, and evaluation through interfaces compatible with scikit-learn.

中文简介正在准备中。当前显示英语简介。

  • Classify time series by their patterns
  • Forecast future values from historical data
  • Detect anomalies and change points
FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

Use a browser to research topics, extract page information, fill forms, and test websites. Guides agents through identifying page controls, interacting with web apps, saving screenshots or PDFs, and reusing saved login sessions.

中文简介正在准备中。当前显示英语简介。

  • Researching topics and reading web articles
  • Filling and submitting online forms
  • Extracting page text and links
FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

Combine health measurements, exercise, sleep, nutrition, and mental health records to examine trends, unusual changes, and estimated risks. The skill produces personalized suggestions and text or interactive HTML reports using local data without external API calls.

中文简介正在准备中。当前显示英语简介。

  • Review health records together in a comprehensive report
  • Explore links between sleep, mood, exercise, and weight
  • Summarize estimated disease and nutrition risks
FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

alphafold

免费

Predict structures for designed protein sequences and protein complexes with AlphaFold2 or ColabFold. Inspect structure files and confidence scores to assess folding and chain interfaces, with guidance for local runs, Modal execution, and common prediction errors.

中文简介正在准备中。当前显示英语简介。

  • Check predicted folding of a designed protein sequence
  • Predict a binder–target protein complex
  • Review structure and interface confidence scores
FreedomIntelligence/OpenClaw-Medical-Skills30792026年7月21日 更新

查看 FreedomIntelligence 的全部技能

报告此技能的问题