##################################################### ### Transformation script that registers samples ### ### if they are not already in LabKey. ### ### Created by: Hannah Brakke ### ##################################################### #load needed packages (assuming they are already installed on your server) library(Rlabkey); library(readxl) ################################################ # Read in the run properties and results data. # ################################################ run.props = labkey.transform.readRunPropertiesFile("${runInfo}"); # save the important run.props as separate variables run.data.file = labkey.transform.getRunPropertyValue(run.props, "runDataFile"); run.output.file = run.props$val3[run.props$name == "runDataFile"]; error.file = labkey.transform.getRunPropertyValue(run.props, "errorsFile"); # read in the results data file content run.data = read.delim(run.data.file, header=TRUE, sep="\t", stringsAsFactors = FALSE); ########################################################### # Now that the file is loaded, perform the transformation # ########################################################### ### If you want to transform any of your assay data, do it here. ### Now register samples that don't exist in LabKey but are in your assay file ### Include lineage of these samples to a parent sample type ### Configure API key, base URL, and folder path in the lines below: myApiKey = "YOUR_API_KEY_HERE" ### Paste your API key between the quotes labkey.setDefaults(apiKey=myApiKey) ## pull the sample type information myBaseUrl = "http://localhost:8080/labkey" ### Define your baseUrl myFolderPath = "/Tutorials/AssayTransform" ### Define your folder path mySchemaName = "samples" myChildQueryName = "childSamples" #define the child sample type name myParentQueryName = "parentSamples" #define the parent sample type name #load the child sample data childData= labkey.selectRows( baseUrl= myBaseUrl, folderPath= myFolderPath, schemaName= mySchemaName, queryName= myChildQueryName, colSelect="Name" #we only care about the name to register samples ) #load the parent sample data parentData= labkey.selectRows( baseUrl= myBaseUrl, folderPath= myFolderPath, schemaName= mySchemaName, queryName= myParentQueryName, colSelect="Name" #we only care about the name to register the lineage ) ### Determine if the sample ID in your assay matches with your sample type (this assumes you have a column called sampleID) #match the child sample type IDs with my assay IDs childmatches = match(run.data$sampleID, childData$Name, nomatch=NA) parentmatches = match(run.data$parentID, parentData$Name, nomatch=NA) #create an empty data frame to load sample IDs and the sample parent rowsToInsert = setNames(data.frame(matrix(ncol = 2, nrow = 0)), c("Name","MaterialInputs/parentSamples")) #change the parentSamples to your parent sample name for(i in 1:nrow(run.data)){ if(is.na(childmatches[i]) == TRUE && is.na(parentmatches[i]) == TRUE){ #if there is no sampleID registered or a parent ID registered, error out stop("If the sampleId isn't registered, the parent sample must be registered. Please register a sample Id or a parent sample.") }else if(is.na(childmatches[i]) == TRUE && is.na(parentmatches[i]) == FALSE){ rowsToInsert[i,] = c(run.data$sampleID[i],run.data$parentID[i]) } } #remove rows with NA rowsToInsert =na.omit(rowsToInsert) #insert new samples labkey.insertRows( baseUrl= myBaseUrl, folderPath=myFolderPath, schemaName= mySchemaName, queryName= myChildQueryName, toInsert = rowsToInsert) ########################################################### # Write the transformed data to the output file location. # ########################################################### # write the new set of run data out to an output file write.table(run.data, file=run.output.file, sep="\t", na="", row.names=FALSE, quote=FALSE) # print the ending time for the transform script writeLines(paste("nProcessing end time:",Sys.time(),sep=" "))