Create custom samples
CreateAndModifyACustomSample.py
from LOGS import LOGS
from LOGS.Entities import CustomFieldDataType, CustomTypeEntityType, CustomTypeSection
# Connect to LOGS
logs = LOGS()
# Create custom field referencing a samples
ref = logs.newCustomField()
ref.name = "Derived from"
ref.description = "A example custom field that references another sample from which this one is derived"
ref.dataType = CustomFieldDataType.Sample
logs.create(ref)
# Create custom field for origin string with validation
origin = logs.newCustomField()
origin.name = "Origin"
origin.description = "A example custom field describing the origin of this sample"
origin.dataType = CustomFieldDataType.String
logs.create(origin)
# Create custom field storing the alphanumeric serial number with validation
serialNumber = logs.newCustomField()
serialNumber.name = "Serial Number"
serialNumber.description = "A example custom field describing the serialNumber of this sample following the rule that is must contain ony 4 alphanumeric characters"
serialNumber.dataType = CustomFieldDataType.String
serialNumber.validationRegexp = r"^\w{4}$"
serialNumber.validationMessage = (
"The serial number must be exactly 4 alphanumeric characters"
)
logs.create(serialNumber)
# Create a section with the above custom fields.
# Sections are a only an organizational unit within a custom type and cannot be created as an entity alone.
# Thus, we create a section that we use in a the custom type below.
section = CustomTypeSection()
section.name = "Origin Information"
section.customFields = [ref, origin, serialNumber]
# Create a custom type for samples including the section
originatedSampleType = logs.newCustomType()
originatedSampleType.name = "Sample with origin"
originatedSampleType.entityType = CustomTypeEntityType.Sample
originatedSampleType.sections = [section]
logs.create(originatedSampleType)
# Using the first project found in the LOGS instance for this example purposes only
project = logs.projects().first()
# Now create a sample of the new custom type and set the custom fields
sample1 = logs.newSample(entityOrCustomTypeOrId=originatedSampleType)
sample1.customType = originatedSampleType
sample1.name = "Test Sample 1"
sample1.projects = [project]
sample1.customValues.Origin_Information.Origin = "Forest Stream"
sample1.customValues.Origin_Information.Serial_Number = "A1B2"
logs.create(sample1)
sample2 = logs.newSample(entityOrCustomTypeOrId=originatedSampleType)
sample2.customType = originatedSampleType
sample2.name = "Test Sample 2 [derived from Sample 1]"
sample2.projects = [project]
# The specific custom field can be set using the section and field names as attributes
sample2.customValues.Origin_Information.Origin = "Meadow Stream"
sample2.customValues.Origin_Information.Derived_from = sample1
# Alternatively, the custom field can be set using its id
sample2.customValues.customField(serialNumber.id).value = "C3D4"
logs.create(sample2)
sample2.printContent()
# Cleanup created entities
logs.delete(sample2, permanently=True)
logs.delete(sample1, permanently=True)
logs.delete(originatedSampleType, permanently=True)
logs.delete(ref, permanently=True)
logs.delete(origin, permanently=True)