
Identify deconvoluted spectra using retention indices and spectral libraries
Source:R/librarySearch.R
librarySearch.RdSearch acquired nominal-mass bins with fast native spectral comparisons and column-specific retention-index filtering. Fitted ion totals are preserved.
Usage
librarySearch(
x,
library,
calibration = NULL,
ri = NULL,
phase = c("DB5", "DB1", "DBWAX"),
ri_tolerance = 30,
n_matches = 5L,
method = "stein_scott",
pseudocount = NULL,
extrapolate = FALSE,
unknown_phase = NULL,
ncores = NULL,
batch_size = 4096L,
reference_count = c("auto", "query", "library"),
ion_weights = NULL,
distinct = FALSE,
min_shared = 0L
)
# S3 method for class 'GCMSmatches'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)
# S3 method for class 'GCMSmatches'
print(x, ...)Arguments
- x
A gcmsDeco fit, or a compound-by-mass matrix with numeric mass names.
- library
A GCMSlibrary returned by readMSP(), readGCMSID() or openLibrary().
- calibration
RIcalibration from calibrateRI(). For gcmsDeco fits, the stored alkane standard and calibrated PSF widths are used automatically when available.
- ri
Optional RI vector, one value per query; overrides profile-based timing.
- phase
RI column class: DB1 (nonpolar), DB5 (semi-standard nonpolar), or DBWAX (polar).
- ri_tolerance
Maximum absolute RI difference, default 30. Inf requests an unconstrained spectral comparison, also allowing references without RI.
- n_matches
Maximum number of matches per spectrum and scoring method.
- method
One or more of stein_scott, cosine, hellinger, jensen_shannon, multinomial, angular, stein_scott_angular, or all. The default is the Stein–Scott weighted dot product.
- pseudocount
Optional total for each query (or one common total), useful when passing L1-normalized spectra. gcmsDeco already retains fitted ion totals.
- extrapolate
Allow linear RI extrapolation beyond the calibrated anchors.
- unknown_phase
Optional explicit column-class assignment for library RI records whose phase is unknown. NULL excludes these from constrained searches.
- ncores
Native search threads; NULL uses the physical core count.
- batch_size
Maximum library spectra loaded per batch; bounds SQLite memory.
- reference_count
Reference count convention for multinomial deviance: auto uses an explicitly supplied reference total when available and otherwise matches the reference total to the query. query always balances to the query; library uses the stored library scale. Original library totals are retained.
- ion_weights
Optional finite nonnegative ion weights. A named vector is matched by nominal mass; unspecified masses retain weight one. An unnamed vector has one value or one per acquired bin. NULL gives uniform weights. libraryIonWeights() estimates reusable inverse-frequency weights.
- distinct
Logical. Rank distinct chemicals by their best eligible record. A checksum-valid CAS identifies a chemical. Remaining records use exact names with normalised case and whitespace. Unnamed records remain separate.
Minimum number of positive-weight ions detected in both spectra. Applied before ranking in both search modes.
- row.names, optional
Additional data-frame arguments.
- ...
Additional method arguments.
Value
A GCMSmatches list with matches (ranked long table), queries (including unassigned spectra), calibration, search settings and elapsed seconds. as.data.frame() extracts the match table. Matches are tentative annotations.
Details
RI filtering precedes spectral scoring. Only the chosen phase class is accepted within ri_tolerance; missing query RI produces no constrained matches. Multiple library RI measurements are compared using the closest. A compound with fewer eligible references returns fewer than n_matches. The same onset-plus-half-narrow-width coordinate is used for standards and fitted profiles. Unknown library column classes require an explicit assignment.
Let a and b be acquired-bin intensities, N=sum(a), p=a/N and q=b/sum(b). The effective reference total M follows reference_count. Raw library totals remain in library_total; reference_total and reference_count identify the effective count and its source. In auto mode an explicit Pseudocount/Ion_total tag supplies the known reference total, adjusted for acquisition coverage.
Cosine and Stein–Scott use weighted inner products sum(w*x*y), with x=a, y=b or the transforms x=a^0.6*mass^3, y=b^0.6*mass^3, respectively. Their distance is 1-C^2. angular and stein_scott_angular return acos(C) in radians using the corresponding transform. Positive weights give a metric on unit-normalized transformed spectra; zero weights omit ions. Angular distances satisfy the triangle inequality and rank identically to their cosine counterparts. A nonzero weighted norm is required.
Let W=max(w). Weighted Hellinger distance is sqrt(sum(w*(sqrt(p)-sqrt(q))^2)/(2*W)). With r=(p+q)/2, weighted JS is J_w=1/2*sum(w*(p*log(p/r)+q*log(q/r))); jensen_shannon returns sqrt(J_w/(W*log(2))). Multinomial deviance is D_w=2*sum(w*(N*p*log(p/u)+M*q*log(q/u))), u=(N*p+M*q)/(N+M). With M=N this simplifies to D_w=4*N*J_w, retaining the query count scale and ranking identically to weighted Jensen–Shannon. At a fixed N this is symmetric in the two spectral shapes. Each ion contributes a nonnegative Jensen gap; nonconstant weights define a composite divergence. Uniform weights with known N,M recover the ordinary multinomial likelihood ratio. Zero contributions use 0*log(0)=0. Disjoint support gives finite distances.
Scores run from 0 to 1000: 1000*C^2 for cosine, 1000*(1-distance^2) for Hellinger/JS, 1000*(1-2*angle/pi) for angular distances, and 1000/(1+deviance) for multinomial. Compare scores within a method and weight convention. Weights change spectral importance while query_total and library_total retain the original count conventions. The default uses no rarity weighting. Build libraryIonWeights() once and reuse the vector.
References are restricted to mass bins actually acquired for the query. Spectra are streamed from SQLite in bounded batches. Native OpenMP workers compare queries independently with deterministic ranking and no nested fitting.
References
Kim, S. et al. (2012). Bioinformatics 28, 1158–1163. doi:10.1093/bioinformatics/bts083.
Stein, S. E. and Scott, D. R. (1994). J. Am. Soc. Mass Spectrom. 5, 859–866. doi:10.1016/1044-0305(94)87009-8.
Examples
f <- tempfile(fileext=".msp")
writeLines(c("Name: compound A", "RI_DB5: 1000", "Num Peaks: 3",
"43 100; 57 80; 71 20;", "Name: compound B", "RI_DB5: 1200",
"Num Peaks: 2", "43 20; 91 100;"), f)
lib <- readMSP(f)
q <- matrix(c(500,400,100,0),1,dimnames=list(NULL,c(43,57,71,91)))
hit <- librarySearch(q,lib,ri=1005,phase="DB5",method="all",ncores=1)
as.data.frame(hit)
#> query library_id shared_ions method distance library_RI
#> 1 1 1 3 stein_scott 0.000000e+00 1000
#> 2 1 1 3 cosine 4.440892e-16 1000
#> 3 1 1 3 hellinger 0.000000e+00 1000
#> 4 1 1 3 jensen_shannon 0.000000e+00 1000
#> 5 1 1 3 multinomial 0.000000e+00 1000
#> 6 1 1 3 angular 2.107342e-08 1000
#> 7 1 1 3 stein_scott_angular 0.000000e+00 1000
#> library_total reference_total reference_count library_mass_coverage query_RI
#> 1 200 1000 query 1 1005
#> 2 200 1000 query 1 1005
#> 3 200 1000 query 1 1005
#> 4 200 1000 query 1 1005
#> 5 200 1000 query 1 1005
#> 6 200 1000 query 1 1005
#> 7 200 1000 query 1 1005
#> delta_RI rank name cas inchikey source source_id origin
#> 1 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 2 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 3 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 4 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 5 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 6 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> 7 -5 1 compound A <NA> <NA> file19c838dc80b1.msp <NA> unknown
#> ri_phase ri_assumed ri_origin ri_source query_total compound score
#> 1 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 2 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 3 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 4 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 5 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 6 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000
#> 7 DB5 FALSE unknown file19c838dc80b1.msp 1000 1 1000