Workspace Setup
To setup a Workspace on Labstep:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import labstep
# Login
user = labstep.login('myaccount@labstep.com', 'mypassword')
# Create a new Workspace
workspace = user.newWorkspace(name='The Synthesis of Aspirin')
user.setWorkspace(workspace.id)
# Create an Experiment
my_experiment = user.newExperiment(name='Trial 1')
# Upload the reaction scheme
user.newFile('./aspirin_reaction_scheme.png')
# Create a Protocol
my_protocol = user.newProtocol('Aspirin Synthesis')
# Create Resource Category
chemicalCategory = user.newResourceCategory('Chemical')
chemicalCategory.addMetadata(fieldName='Molar mass')
chemicalCategory.addMetadata(fieldName='Density')
chemicalCategory.addMetadata(fieldName='Melting point')
# Add Resources
salicylic_acid = user.newResource('Salicylic Acid')
salicylic_acid.addComment('Here is the chemical structure',
'./salicylic_acid.png')
salicylic_acid.addMetadata(fieldName='Formula', value='C7H6O3')
salicylic_acid.addMetadata(fieldName='Hazards', value='Corrosive, irritant')
# etc...
Importing Resources
To import a list of resources into Labstep from a resource_import.csv file:
resource_import.csv
import labstep
import pandas as pd
from labstep.service.helpers import linearToCartesianCoordinates
## Authenticate user
user = labstep.authenticate()
## Set to the ID of the workspace to import into.
user.setWorkspace(WORKSPACE_ID) ## Replace with ID of target workspace.
## Create templates for resources and items
resourceCategory = user.newResourceCategory('Plasmid Preps')
resourceTemplate = resourceCategory.getResourceTemplate()
itemTemplate = resourceCategory.getItemTemplate()
resourceTemplate.addMetadata(fieldName="Plasmid Backbone", fieldType="default")
itemTemplate.addMetadata(fieldName="Concentration", fieldType="numeric", unit="ng / µL")
itemTemplate.addMetadata(fieldName="Prep Date", fieldType="date")
## Import data from xlsx file to create locations, resources and items.
df = pd.read_excel('inventory.xlsx')
df_locations = df['Location'].unique()
locations = {}
for location in df_locations:
newLocation = user.newResourceLocation(name=location)
locations[newLocation.name] = newLocation.guid
for index, row in df.iterrows():
newResource = user.newResource(name=row['Name'], resource_category_id=resourceCategory.id)
newResource.addMetadata(fieldName='Plasmid Backbone', fieldType='default', value=row['Plasmid backbone'])
newItem = newResource.newItem()
newItem.addMetadata(fieldName='Concentration', fieldType="numeric", number=row['Concentration (ng / µl)'])
newItem.addMetadata(fieldName='Prep Date', fieldType="date", date=str(row['Prep Date']))
mapPosition = linearToCartesianCoordinates(position=row['Box Position'], number_of_columns=10)
newItem.setLocation(locations[row['Location']], position=mapPosition)
Deleting Multiple Entities
You can use labstepPy to easily delete multiple different Entities on Labstep (a list of Experiments, Protocols, or Tags, etc.), either from within a specific Workspace or by performing a global delete.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import labstep
# Login
user = labstep.login('myaccount@labstep.com', 'mypassword')
# Choose a Workspace
my_workspace = user.getWorkspaces(name='Structure of Protein A')[0]
user.setWorkspace(my_workspace.id)
# Delete a list of Tags for 'crystallisation'
# That are only in the Resource entity type
tags_to_delete = user.getTags(search_query='crystallisation', type='resource')
for i in range(len(tags_to_delete)):
print('TAGS TO DELETE =', tags_to_delete[i].name)
tags_to_delete[i].delete()
# Get the difference of two lists using set()
def diff(list1, list2):
return (list(set(list1) - set(list2)))
# Only keep experiments that investigate the protein structure by 'NMR'
# And store the IDs in a list
keep_experiments = user.getExperiments(search_query='NMR')
keep_exp_ids = []
for i in range(len(keep_experiments)):
print('EXPERIMENTS TO KEEP =', keep_experiments[i].name)
print('EXPERIMENT IDS TO KEEP =', keep_experiments[i].id)
keep_exp_ids.append(keep_experiments[i].id)
# Get all Experiment IDs
all_experiments = user.getExperiments()
all_exp_ids = []
for i in range(len(all_experiments)):
all_exp_ids.append(all_experiments[i].id)
# Find the IDs of Experiments to delete, and delete them
for i in diff(all_exp_ids, keep_exp_ids):
print('EXPERIMENT IDS TO DELETE =', i)
exp_to_delete = user.getExperiment(i)
exp_to_delete.delete()
Downloading Files
You can use labstepPy to download files uploaded directly to Labstep or attached to different Labstep entities
import labstep
user = labstep.login('myaccount@labstep.com', 'mypassword')
# A List of the authenticated user's files can be accessed
# via the getFiles method
my_files = user.getFiles()
my_file = my_files[0]
# Alternatively retrieve a list of files from a workspace
my_workspace = user.getWorkspaces()[0]
workspace_files = my_workspace.getFiles()
# Access the data via the getData method
rawData = my_file.getData()
# Or save directly as a new file
my_file.save()
# If you come across a file_id attached to another labstep entity
# you can retrieve the file using the User getFile method
my_file = user.getFile(123)
my_file.save()