API insertRows to add samples fails with error 'Name expression required'

LabKey Support Forum
API insertRows to add samples fails with error 'Name expression required' olnerdybastid  2018-03-16 16:00
Status: Closed
 

I am trying to use the LabKey API to programatically insert new samples, but keep getting an error stating 'Name expression required' (though I am providing a value for 'Name'). I've verified that my API calls to select rows are good in the same table I want to insert into, so it seems to me like the problem is with the structure of the data I'm trying to pass to LabKey, and probably stems from a misunderstanding on my part of how the sample schemas are structured. I've reproduced a minimal example below using R and the HIV Study tutorial (I get the same error using JS):


base = 'http://localhost:8080/labkey'
folder = '/Tutorials/HIV Study'
schema = 'samples'
query = 'Study Specimens'

session <- getSession(baseUrl=base, folderPath=folder)

# Get samples from the tutorial HIV study -- this works, returns 496 rows
samples = labkey.selectRows(baseUrl=base, folderPath=folder,schemaName=schema, queryName=query)

# Now take the first row, change the name, and try to upload this as a 'new' sample 
sampleToUpload = samples[1,]
sampleToUpload[1,'Name'] = 'thisIsMySample'

# Doesn't work, returns 'HTTP request was unsuccessful. Status code = 400, Error message = Name expression required'
labkey.insertRows(baseUrl=base, folderPath=folder,schemaName=schema, queryName=query, sampleToUpload)
 
 
kevink responded:  2018-03-19 17:07

Sorry you are having issues inserting samples. Unfortunately, I haven't been able to reproduce this issue. When you go to the SampleSet details page, does the SampleSet list the Id Column(s) or the Name Expression?

 
olnerdybastid responded:  2018-03-20 04:19

Thanks for having a look. Are you referring to the contents of the web part titled (by default) 'Sample Set Contents'? If so, when I add that web part I see two columns displayed, 'Name' and 'Flag' in the grid view. Looking over the available columns that are listed when I customize the grid view, I see both 'Name' and 'Id' but not 'Name Expression'. Let me know if I'm not looking in the correct place for Sample Set details.

 
Jon (LabKey DevOps) responded:  2018-03-30 15:09
Hello,

"Name Expression" can be found in the Edit Set area of the Sample Set. You should be able to use "Name" though.

Just curious, have you tried to see if you can insert in a single row via a data.frame? So rather than using the selectRows call and putting that in the insertRows, you try to insert a single row instead?

Regards,

Jon
 
olnerdybastid responded:  2018-04-02 07:50

Ah, I see. I took a look at the Edit Set area of the Sample Set I'm trying to insert into ('Study Specimens') and the Name Expression field is blank. I tried specifying some different name expressions in the Edit Set interface including ${Name} and ${randomID} and then trying to to insert samples again with the same procedure as before, but with no luck. Same error message occurs--'Name expression required'.

After setting the 'Name Expression' field back to its default value (which is empty in the HIV tutorial), I tried two different things while I was messing around with this over the weekend. First, per your suggestion, I made a data frame with a single row consisting of a single column ('Name') and tried to insert that. Same error.

folder = '/Tutorials/HIV Study'
schema = 'samples'
query = 'Study Specimens'

session <- getSession(baseUrl=base, folderPath=folder)

# Make a data frame that specifies a sample name

sampleToUpload = data.frame(matrix(c('P666666'), nrow=1))
colnames(sampleToUpload)=c('Name')

# Doesn't work, returns 'HTTP request was unsuccessful. Status code = 400, Error message = Name expression required'
labkey.insertRows(baseUrl=base, folderPath=folder,schemaName=schema, queryName=query, sampleToUpload)

Secondly, I navigated to the 'Upload Specimens' page, downloaded the template workbook, and entered data for a dummy sample (see attached Excel sheet). When I paste that sheet into the 'Upload Specimens' section I can add the sample just fine. When I read it into an R data frame first and try to insert it via API however I still get the same Name Expression error:

#Using template provided from the 'Upload Specimens' page
library(gdata)

sampleToUpload = read.xls('myHIVspecimen')
# Doesn't work, returns 'HTTP request was unsuccessful. Status code = 400, Error message = Name expression required'
labkey.insertRows(baseUrl=base, folderPath=folder,schemaName=schema, queryName=query, sampleToUpload)

You mentioned that the error didn't replicate for you. Can I ask what columns you passed in the data frame when you made the insertRows call so I can make sure I'm structuring my data frame correctly?

 
Jon (LabKey DevOps) responded:  2018-04-08 00:14
Hello,

Upon further examination, I realized I was working in standard Sample Sets, not specifically the Sample Set that gets created in tandem with a standard specimen repository in a Study.

It does appear that we do not allow for this action by design.

Have you tried to just create your own Sample Set instead of trying to use the one tied to the Specimen Repository?

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

Regards,

Jon