npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@biolibs/searchio

v1.1.0

Published

Sequence search results in one model, verified against Biopython: QueryResult, Hit, HSP and HSPFragment, and readers and writers for the tools that produce them (Bio.SearchIO)

Readme

@biolibs/searchio

Sequence search results from any tool, in one model: a port of Biopython's Bio.SearchIO.

BLAST, HMMER, BLAT, Exonerate and the rest each write what they found in a format of their own, and each means much the same thing by it: a query, the database sequences it resembled, and for each of those the regions that matched. Bio.SearchIO reads them all into four classes, so that code written for one tool's output works on another's.

The four classes are here, and so are parse, read, write, convert and toDict over every format Biopython reads: eighteen formats under nineteen names, seven of them written as well — and index, for a file too big to want all of at once. index_db, which keeps its index in an SQLite file, is not ported.

Reading and writing

import { TextSink, convert, parse, read, toDict, write } from "@biolibs/searchio";

const tab = "q1\th1\t95.00\t20\t1\t0\t1\t20\t101\t120\t1e-05\t38.2\n" +
            "q2\th1\t100.00\t12\t0\t0\t5\t16\t30\t19\t0.002\t24.3\n";

for (const qresult of parse(tab, "blast-tab")) {
  qresult.id;                            // "q1", then "q2"
  qresult.get(0).get(0).evalue;          // 1e-5, then 0.002
}
const byId = toDict(parse(tab, "blast-tab"));
byId.get("q2")!.get(0).get(0).hitStrand; // -1: the hit runs from 30 down to 19

const out = new TextSink();
write(byId.get("q1")!, out, "blast-tab", { comments: true });   // [1, 1, 1, 1]: results, hits, HSPs, fragments
out.getvalue();                          // "# <UNKNOWN PROGRAM> <unknown version>\n# Query: q1 <unknown description>\n..."

read(tab, "blast-tab");                  // ValueError: More than one query result found in handle
const copy = new TextSink();
convert(tab, "blast-tab", copy, "blast-tab");   // [2, 2, 2, 2], and copy.getvalue() === tab

A file is given as its contents — a string or bytes — and written to anything with a write(text); TextSink is one that keeps what it is given. Nothing here opens a file.

| Format | Read | Write | Options | |---|---|---|---| | blast-tab (-outfmt 6, and 7 with comments) | yes | yes | comments, fields | | blast-xml (-outfmt 5) | yes | yes | useRawQueryIds, useRawHitIds | | blat-psl (PSL, and PSLX with pslx) | yes | yes | pslx; header when writing | | hmmer3-tab (HMMER 3 --tblout) | yes | yes | | | hmmscan3-domtab, hmmsearch3-domtab, phmmer3-domtab (--domtblout) | yes | yes | | | hmmer3-text (what hmmscan, hmmsearch and phmmer print) | yes | | | | hmmer2-text (what HMMER 2's hmmpfam and hmmsearch print) | yes | | | | exonerate-text (the alignments Exonerate prints) | yes | | | | exonerate-vulgar, exonerate-cigar (its vulgar: and cigar: lines) | yes | | | | fasta-m10 (the FASTA programs' -m 10 output) | yes | | | | hhsuite2-text, hhsuite3-text (what hhsearch and hhblits print) | yes | | | | infernal-tab (Infernal --tblout, --fmt 1, 2 or 3) | yes | | fmt, when the file has no header | | infernal-text (what cmscan and cmsearch print) | yes | | | | interproscan-xml (InterProScan 5's XML) | yes | | |

  • fields are BLAST's own column names, as a list or as the space-separated string BLAST takes, std included. A commented file names its own columns; a plain one is assumed to have the default twelve.
  • Coordinates are turned from BLAST's one-based, either-way-round pairs into zero-based starts and ends with a strand, and turned back on writing.
  • PSL is blocks. Each line of PSL is an HSP and each of its ungapped blocks a fragment, so a BLAT HSP usually has several: hsp.queryRangeAll, hsp.hitSpanAll. Minus-strand block starts, which PSL counts from the far end, are turned round. A protein query against translated DNA is recognised as BLAT recognises it — its target blocks are three times as long — and gets a query strand of 0. The reader works out BLAT's score and percent identity as the BLAT web page does (hsp.score, hsp.identPct).
  • HMMER's profile is the hit or the query. hmmscan searches profiles with a sequence; hmmsearch and phmmer search sequences with a profile. A report says which program wrote it. A domain table does not, so there are three names for the one format, and the name decides which coordinates are the hit's. The table of hits has no coordinates at all: each hit gets one HSP, for its best domain's scores. Only the reports have the alignments, with HMMER's annotation lines in fragment.alnAnnotation (similarity, PP, CS).
  • Exonerate aligns across introns. An alignment is one HSP with a fragment for each exon, and for each stretch between frameshifts; hsp.hitInterRanges are the introns. All three formats can be in one file, as Exonerate writes them when asked, and each reader takes its own lines. The text reader has the sequences: where a protein is aligned to DNA, Exonerate prints it in three-letter codes, and the reader turns those back into residues and keeps the codons in fragment.alnAnnotation. Codons an intron splits are hsp.querySplitCodons and hsp.hitSplitCodons. The vulgar: reader works the same blocks out from the line's steps (kept as hsp.vulgarComp); the cigar: reader keeps the steps as text and makes one fragment of the whole alignment.
  • FASTA shows more than it aligned. -m 10 prints each sequence with some of what lies either side of the alignment, and the positions that say how much; the reader trims it. Percent identity and similarity are fractions in the file and percentages on the HSP (identPct, posPct).
  • HHsuite has one query to a file, and each numbered match is an HSP; matches to the same template are one hit. The probability HHsuite gives is hsp.prob.
  • InterProScan's hits are signatures. Each match of a protein to a member database's signature is a hit, its locations the HSPs; the InterPro entry and its GO terms and pathways are hit.dbxrefs, and the kind of match and the database are in hit.attributes. Every fragment's query is the whole protein sequence, as in Biopython.
  • Infernal's table says which table it is. There are three layouts with different columns, and the reader tells them apart by the headings, so a table with its header cut off has to come with { fmt }. The report's alignments may leave stretches out (*[12]*); each stretch shown is a fragment, and the rows of structure and confidence are in fragment.alnAnnotation (CS, PP, NC). Infernal lists matches by score, so a hit's HSPs need not have been together in the file. The table's coordinates are kept as the table has them, one-based; the report's too.
  • XML needs more than a table has. Results read from tabular output lack the search's parameters and BLAST's own ids, and the XML writer refuses them, naming the first thing it could not find.
  • A format that is no format gets Biopython's ValueError, with Biopython's list of the ones there are.

This is BLAST output read into Bio.SearchIO's model. @biolibs/blast reads the same XML into Bio.Blast's, which is a different one.

The model

QueryResult     one query's results
  Hit           one database sequence
    HSP         one match: the tool's statistics for it
      HSPFragment   one unbroken stretch of alignment: the sequences and the coordinates
import { HSP, HSPFragment, Hit, QueryResult } from "@biolibs/searchio";

const fragment = new HSPFragment("gi|123|", "query1", "ACGT-ACGT", "ACGTTACGT");
fragment.hitStart = 10;
fragment.hitEnd = 18;

const hsp = new HSP([fragment]);
hsp.evalue = 1.5e-22;                    // a statistic, attached as a reader would attach it
hsp.bitscore = 98.4;

const qresult = new QueryResult([new Hit([hsp])]);

qresult.id;                              // "query1" — taken from the fragment, through the HSP and the hit
qresult.get(0).id;                       // "gi|123|"
qresult.get("gi|123|").get(0).alnSpan;   // 9
qresult.toString();                      // what Python's print() gives
  • Kept in step. A hit knows its id and the query's, and so does every HSP and fragment under it. Set one at the top and it is set all the way down, sequences' records included.
  • A list and a dictionary. qresult.get(0) is the first hit; qresult.get("gi|123|") is the hit with that id, or with that among its other ids.
  • One fragment or several. hsp.hit, hsp.hitStrand and the rest answer for an HSP's only fragment and refuse when there are several; hsp.hitAll, hsp.hitStrandAll answer for all of them. hsp.hitStart and hsp.hitEnd are the smallest start and the largest end.
  • Whatever the tool said. A reader attaches what its format has — evalue, bitscore, seqLen — as plain properties, and they are carried along when an object is sliced, filtered or sorted.

Access by query

import { index } from "@biolibs/searchio";

const idx = index(tab, "blast-tab");      // walks the file once; reads nothing into objects
[...idx.keys()];                          // ["q1", "q2"]
idx.size;                                 // 2
idx.get("q2").get(0).get(0).evalue;       // 0.002: that query's results, read now
idx.getRaw("q2");                         // its bytes, as they are in the file
idx.has("q9");                            // false

index takes a file's contents — bytes, or text, which is encoded as UTF-8 so that offsets are byte offsets — and returns something to ask for a query by key. keyFunction turns a query's id into the key to file it under; two queries with one key are a ValueError. Fifteen of the format names can be indexed, the ones Biopython can: all but HHsuite's and InterProScan's.

From Python

Names are the same, in camel case. What Python does with brackets and built-ins is done with methods.

| Python | TypeScript | |---|---| | hsp.hit_start, hit.query_id, qresult.hit_keys, hsp.aln_span | hsp.hitStart, hit.queryId, qresult.hitKeys, hsp.alnSpan | | x[0], x["id"], x[1:3] | x.get(0), x.get("id"), x.get({ start: 1, stop: 3 }) | | x[0] = y, del x[0] | x.set(0, y), x.delete(0) | | len(x), y in x, for y in x | x.length, x.has(y), for (const y of x) | | str(x), repr(x) | x.toString(), x.repr() | | qresult.hit_filter(f), hsp_filter, hit_map, hsp_map | qresult.hitFilter(f), hspFilter, hitMap, hspMap | | qresult.sort(key=f, reverse=True, in_place=False) | qresult.sort(f, true, false) | | qresult.pop("id", default) | qresult.pop("id", fallback) | | hsp.evalue on an HSP that has none: AttributeError | undefined | | SearchIO.parse(handle, "blast-tab", comments=True) | parse(text, "blast-tab", { comments: true }) | | SearchIO.write(qresults, handle, "blast-xml", use_raw_hit_ids=False) | write(qresults, sink, "blast-xml", { useRawHitIds: false }) | | SearchIO.to_dict(qresults, key_function=f) | toDict(qresults, f), a Map | | SearchIO.index("out.tab", "blast-tab", key_function=f) | index(contents, "blast-tab", { keyFunction: f }) | | idx["q1"], "q1" in idx, len(idx), idx.get_raw("q1") | idx.get("q1"), idx.has("q1"), idx.size, idx.getRaw("q1") |

What is verified

Two generators run the real Biopython and record what it did; the tests replay both.

| Upstream module | Items at parity | Checklist | |---|---|---| | Bio.SearchIO | 143 / 143 | Bio.SearchIO.md |

The model: 453 programs, 31,962 steps (reference/golden/gen_searchio_model_cases.py). Each is a short program — make these objects, do these things to them — with what Biopython answered after every step: a value, or an exception with its message, and any warning. Fifty-three are written by hand, one for each thing the classes do; four hundred are random, because the rules interact.

The functions and the BLAST formats: 4,486 cases (reference/golden/gen_searchio_blast_cases.py). The files are made by the generator — random gapped alignments with the coordinates and counts worked out from them, for seven BLAST programs — and then spoiled: a line removed, a value replaced, the file cut short. 1,366 cases read a file and compare every attribute of every object and what each prints, or the exception and what was read before it; 2,998 write, and compare the text to the character, including what had been written when a writer gave up. Most of those write results that were read and then altered — a frame removed, an e-value set to each side of each threshold BLAST formats at.

BLAT's PSL: 2,626 cases (reference/golden/gen_searchio_blat_cases.py). Each alignment is a random run of blocks on a random strand, for DNA, translated and protein searches, with PSL's counts worked out from it; 836 cases read, 1,667 write — as PSL, PSLX and both BLAST formats, and BLAST results as PSL.

HMMER: 3,449 cases (reference/golden/gen_searchio_hmmer_cases.py). One random search — queries, hits, domains, alignments — is laid out as each program lays it out: both HMMER 3 tables, the HMMER 3 report for hmmscan, hmmsearch and phmmer with and without alignments and at three line widths, and the HMMER 2 report for hmmpfam and hmmsearch. 1,247 cases read; 1,989 write the tables, from every HMMER format and from BLAST's.

Exonerate: 2,398 cases (reference/golden/gen_searchio_exonerate_cases.py). One alignment is made and printed all three ways — for DNA against DNA, across introns (est2genome), with stretches left unaligned (ner), for a protein over DNA with frameshifts and split codons (protein2dna, protein2genome) and for two coding sequences (coding2coding) — and each reader is given the file whole and in parts. 1,966 cases read.

FASTA, HHsuite and InterProScan: 1,136, 547 and 674 cases (gen_searchio_fasta_cases.py, gen_searchio_hhsuite_cases.py, gen_searchio_interproscan_cases.py), made and spoiled the same way.

index: 4,122 cases, recorded by the same generators: each format's files indexed as they are, keyed three other ways, with each line gone in turn, and (1,589 of them) as BGZF in blocks small enough that queries and lines run across them — the keys, the bytes each key stands for (by length and digest), and what is read from them.

Infernal: 1,067 cases (gen_searchio_infernal_cases.py): one random search as the table in each of its three layouts — read by its header, and told each layout rightly and wrongly — and as the report of cmscan and cmsearch, with alignments in one screenful or several, whole or with stretches left out, and without alignments at all.

Where the claim stops:

  • index_db is not ported. It keeps its index in an SQLite file, and nothing here touches a file system; the checklist marks it as it marks Bio.SeqIO.index_db. index does read BGZF-compressed files, as Biopython's does, through @biolibs/bgzf; plain gzip is refused.
  • A query read back from an index is read from text with its line ends made plain, where Biopython reads the bytes as they are. Only a file with carriage returns could show the difference, and the cases with them do not.
  • One number type. Python writes 7 and 7.0 differently and JavaScript has one number for both. The XML writer writes a whole number as a float where the reader would have made it one (evalue, bitscore, the statistics); a float set by hand on an attribute that is normally an integer is written as an integer. Integers beyond 2⁵³ lose their last digits.
  • Text, not files. Given a path, Biopython turns a TypeError raised while reading into a RuntimeError about its own file handling. The cases give it an open file, and the port takes text.
  • Four tabular files Biopython never finishes reading — commented, with a # Fields: line and no rows under it — are recorded as that and not run. The port stops at the end of the file.
  • Eighteen HMMER 3 reports that stop short. Biopython looks for the next line it knows by reading until it finds it, and at the end of a truncated report reads nothing for ever. The port raises ValueError ("Unexpected end of file") instead, and the tests check that it does.
  • Twenty-four FASTA files and nineteen Infernal reports that stop short are the same story as HMMER's: Biopython reads the end for ever, the port raises ValueError.
  • InterProScan's namespace is used as a pattern. Biopython builds a regular expression from the document's namespace to name the kind of each match. A namespace with characters that mean something in a pattern is not covered by the cases.
  • A hundred and one Exonerate files with nothing in them for the reader asked — vulgar: lines given to the cigar: reader, say. Biopython goes on looking for ever. The port yields nothing, and the tests check that it does.
  • Every file the readers are measured on was made by the generators, laid out from knowledge of each tool's output and not copied from it — nothing was downloaded. What is verified is that the port reads those files exactly as Biopython does, faults included; a layout of a real tool's that the generators do not make is not covered.
  • QueryResult.absorb is not run on a hit the result already holds. In Biopython that never returns: it appends the hit's HSPs to the list it is walking. The port does the same, and neither is tested.

Oddities that are Biopython's and are kept:

  • A slice of a QueryResult shares the original's table of alternative ids, so has can say yes to an id whose hit the slice does not hold.
  • Replacing a fragment in an HSP with one for another hit raises AttributeError, not ValueError: the message it means to give refers to an attribute an HSP does not have.
  • A hit with no HSPs in a BLAST XML file is dropped, and a blank line in a tabular file ends it.
  • Writing XML from no query results at all throws StopIteration, which is what Biopython lets out.
  • The XML writer, told not to use BLAST's own hit ids, joins a hit's description to its alternative ids with nothing between.
  • The tabular writer given fields as a string takes it a character at a time. The reader splits it.

Install

npm install @biolibs/searchio

ESM only, Node 20 or later, TypeScript declarations included.

License

MIT — see LICENSE.