Specimen, Set primaryType and derivativeType using R API

LabKey Support Forum (Inactive)
Specimen, Set primaryType and derivativeType using R API Lapalmej  2017-09-14 06:48
Status: Closed
 

Hi,

We are trying to import specimens to Labkey using the R API. When we tried the specimen is import but some fields (primaryType and derivativeType) that we enter are blank. Here is the R code:

toadd = data.frame(patient_id = "ptid" , GlobalUniqueId = "test_1", sequenceNum = 1, drawtimestamp = "20170303", primaryType = "Tissue", derivativeType = "gDNA", stringsAsFactors = FALSE)

labkey.importRows(baseUrl = "https://1.1.1.1/labkey"
                    folderPath = "/X/Y",
                    schemaName = "study",
                    queryName = "SpecimenDetail",
                    toImport = toadd)

after those 2 commands the specimen is import but primaryType and sampleType are blank

I am using Labkey server 16.3 on Centos7. my R version is 3.4.0 and I am using Rlabkey package 2.1.134

Thanks for your help
Joel

 
 
Anthony Corbett responded:  2017-09-15 20:37

I believe PrimaryType and DerivativeType are lookups to study.SpecimenPrimaryType.RowId and study.SpecimenDerivative.RowId respectively. Thus, you need to use the integer RowId of those tables.

I would query those tables to get the RowId and whatever column has your string label and then create a named vector to do the lookups. The following works in 17.1:

library(Rlabkey)
library(magrittr)

primaryTypes <- labkey.selectRows(
  baseUrl="https://localhost:8080/labkey", 
  folderPath="/containerPath", 
  schemaName="study", 
  queryName="SpecimenPrimaryType", 
  colSelect=c("RowId","Description"), 
  colNameOpt = 'rname'
) %$% setNames(rowid, as.character(description))

derivativeTypes <- labkey.selectRows(
  baseUrl="https://localhost:8080/labkey", 
  folderPath="/containerPath", 
  schemaName="study", 
  queryName="SpecimenDerivative", 
  colSelect=c("RowId","Description"), 
  colNameOpt = 'rname'
) %$% setNames(rowid, as.character(description))

Now you can use those for lookups:

toadd = data.frame(patient_id = "ptid" , GlobalUniqueId = "test_1", sequenceNum = 1, drawtimestamp = "20170303", primaryType = primaryTypes[["Tissue"]], derivativeType = derivativeTypes[["gDNA"]], stringsAsFactors = FALSE)

If you have many rows (e.g. imported from flat file or excel) where primaryType and derivativeType columns are your text labels already I would use dplyr::mutate to replace the string value with the rowid using the named vector lookup:

library(dplyr)
toadd <- mutate(toadd, primaryType=primaryTypes[primaryType], derivativeType=derivativeTypes[derivativeType])
 
Lapalmej responded:  2017-09-19 08:05

Thank you Anthony your solution is exactly what I was looking for!

 
Lapalmej responded:  2017-09-19 12:43

One last question:

Can I add new SpecimenPrimaryType from the API. I am able to import specimen with the solution you propose but It is not working if the primaryType is not in the SpecimenPrimaryType dataset.

Is there a way to import specimen with a new primaryType?

Thanks for your help
Joel

 
Jon (LabKey DevOps) responded:  2017-09-21 20:20
Hi Joel,

The SpecimenPrimaryType table appears to not be updatable via the API. The only place where I'm finding the table gets any kind of updating is via the Advanced Specimen import process using a specimen archive file that has a primary_types.tsv file with the values.

https://www.labkey.org/Documentation/wiki-page.view?name=specimenArchiveFileFormat

Regards,

Jon