🔍 Code Extractor

function create_lims_samples

Maturity: 44

Creates a new LIMS_Samples node in a Neo4j graph database with the specified properties and returns the created node.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
400 - 409
Complexity:
simple

Purpose

This function is designed to insert a new LIMS (Laboratory Information Management System) sample record into a Neo4j graph database. It dynamically constructs a Cypher query based on the provided properties dictionary, creates a node with the label 'LIMS_Samples', sets all properties on that node, and returns the created node object. This is useful for tracking laboratory samples in a graph database structure where relationships between samples and other entities can be easily managed.

Source Code

def create_lims_samples(properties):
    """Create a new LIMS_Samples node"""
    props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
    query = f"""
    CREATE (n:LIMS_Samples)
    SET {props_list}
    RETURN n
    """
    result = run_query(query, properties)
    return result[0] if result else None

Parameters

Name Type Default Kind
properties - - positional_or_keyword

Parameter Details

properties: A dictionary containing key-value pairs representing the properties to be set on the LIMS_Samples node. Keys should be strings representing property names, and values can be any type supported by Neo4j (strings, numbers, booleans, dates, etc.). Example: {'sample_id': 'S12345', 'sample_type': 'blood', 'collection_date': '2024-01-15'}. The dictionary should not be empty as it would create a node with no properties.

Return Value

Returns the created LIMS_Samples node object (typically a Neo4j Record or Node object) if the creation was successful, or None if the query returned no results or failed. The returned node object contains all the properties that were set during creation and can be accessed using dot notation or dictionary-style access.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

from neo4j import GraphDatabase

# Assuming run_query function is defined
def run_query(query, params):
    driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
    with driver.session() as session:
        result = session.run(query, params)
        return [record['n'] for record in result]
    driver.close()

# Create a new LIMS sample
sample_properties = {
    'sample_id': 'S12345',
    'sample_type': 'blood',
    'collection_date': '2024-01-15',
    'patient_id': 'P67890',
    'status': 'pending'
}

new_sample = create_lims_samples(sample_properties)
if new_sample:
    print(f'Successfully created sample: {new_sample}')
else:
    print('Failed to create sample')

Best Practices

  • Ensure the properties dictionary is not empty before calling this function to avoid creating nodes with no identifying information
  • Validate and sanitize property values before passing them to prevent injection attacks, although parameterized queries provide some protection
  • Consider adding error handling around the function call to catch potential database connection issues
  • The run_query function must be properly implemented with connection management and error handling
  • Use unique identifiers (like sample_id) in the properties to enable easy retrieval and prevent duplicates
  • Consider adding constraints or indexes on frequently queried properties in the Neo4j database for better performance
  • Be aware that this function does not check for existing nodes with the same properties, which could lead to duplicates

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_lims_sampletestresults 89.0% similar

    Creates a new LIMS_SampleTestResults node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_lims_sampletypes 87.9% similar

    Creates a new LIMS_SampleTypes node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_lims_sampletypeproperties 86.5% similar

    Creates a new LIMS_SampleTypeProperties node in a Neo4j graph database with the provided properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_lims_sampletypetests 86.1% similar

    Creates a new LIMS_SampleTypeTests node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_lims_sampletestresultdetails 82.1% similar

    Creates a new LIMS_SampleTestResultDetails node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
← Back to Browse