Skip to content

Extract fingerprint using R #291

@ghost

Description

Bug description

There are error when I try to extract fingerprint in R.

Here is my ms2 file, Pkease change it to .msp and then upload to sirius
neg_short2_intersected.txt

I did some exploration and generate my own instruction
https://yingxiaoyan.gitlab.io/postdoctoral_research/Sirius_api.html

Expected behavior

A clear and concise description of what you expected to happen.

There should available fingerprint if a spectra is valid (with more than 2 peaks)

Steps To Reproduce

See https://yingxiaoyan.gitlab.io/postdoctoral_research/Sirius_api.html
Obtain the api after you run the msp file on sitius and then use the port then run following code


port_neg<-

library(RSirius) 
sdk <- SiriusSDK$new()
api <- sdk$connect(paste0("http://localhost:",port_neg))  ### change yout local host after sirius


project_id =sapply(api$projects_api$GetProjects(),function(x)x$projectId)
project_id


compounds_id=api$compounds_api$GetCompounds(project_id)
compounds_id=sapply(compounds_id, function(x)x$compoundId)
compounds_id

aligned_feature_id<-api$features_api$GetAlignedFeatures(project_id)
aligned_feature_id=sapply(aligned_feature_id, function(x)x$alignedFeatureId)
aligned_feature_id


formula_id_list<-list()
siriusScoreNormalized_list<-list()  

failed_idx <- integer(0)  # will store i values where the call fails

for(i in 1:length(aligned_feature_id)){
  tryCatch({
  formula_id_list[[i]]=api$features_api$GetFormulaCandidates(project_id,
                                               aligned_feature_id[i])
  siriusScoreNormalized_list[[i]]=sapply(formula_id_list[[i]], 
                                         function(x)x$siriusScoreNormalized)
 
  formula_id_list[[i]]=sapply(formula_id_list[[i]], function(x)x$formulaId)
 
  },error=function(e) {
      failed_idx <<- c(failed_idx, i)   # record failing i
      # Optionally print the error message
      message(sprintf("Error at i=%d: %s", i, conditionMessage(e)))
      # return a placeholder so the loop continues
      return(NULL)
    }
  )
  cat(i," ")
}

siriusScoreNormalized_best<-lapply(siriusScoreNormalized_list,which.max)

formula_id_list[[1]]
formula_id_list[[2]]
formula_id_list[[3]]


for (i in seq_len(nrow(X))) {
  # Attempt the API call and flatten the payload
  v <- tryCatch(
    {
      resp <- api$features_api$GetFingerprintPrediction(
        project_id         = project_id,
        aligned_feature_id = aligned_feature_id[i],
        formula_id         = formula_id_best[i]
      )
      unlist(resp, use.names = FALSE)
    },
    error = function(e) {
      failed_idx <<- c(failed_idx, i)   # record failing i
      # Optionally print the error message
      message(sprintf("Error at i=%d: %s", i, conditionMessage(e)))
      # return a placeholder so the loop continues
      return(NULL)
    },
    warning = function(w) {
      # Treat warnings as failures too (optional)
      failed_idx <<- c(failed_idx, i)
      message(sprintf("Warning at i=%d: %s", i, conditionMessage(w)))
      return(NULL)
    }
  )

  # Only assign if we received a non-null vector
  if (!is.null(v)) {
    # If needed, coerce to numeric
    v <- suppressWarnings(as.numeric(v))
    # Pad/trim to match X's columns
    if (length(v) < ncol(X)) {
      v <- c(v, rep(NA_real_, ncol(X) - length(v)))
    } else if (length(v) > ncol(X)) {
      v <- v[seq_len(ncol(X))]
    }
    X[i, ] <- v
  } else {
    # Fill row with NAs on failure
    X[i, ] <- NA_real_
  }

  cat(i, " ")
}

# After the loop:
cat("\nFailed indices:", paste(failed_idx, collapse = ", "), "\n")

Screenshots and Log Files

Please upload screenshots of any errors and include log output or console output. Log can be copied from the graphical user interface using the log button.

Example data

Include any additional information that might help the development team to reproduce the bug, in particular relevant project files or datasets, that demonstrate the problem you are reporting.

api$features_api$GetFingerprintPrediction(    project_id         = project_id,
                                                         aligned_feature_id = aligned_feature_id[179],
                                                           formula_id         = formula_id_best[179])

I got error like this for lot of features, however for the features with these errors, the spectra are quite well matched (lots of blue peaks instead of black)
Error in is.null(local_var_resp$response) || local_var_resp$response == :
'length = 200' in coercion to 'logical(1)'

Metadata

Metadata

Assignees

No one assigned

    Labels

    probable bugMarks bug reports where the reported issue has not yet been confirmed/reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions