Page MenuHomec4science

add_columns.py
No OneTemporary

File Metadata

Created
Sat, Jan 18, 09:36

add_columns.py

from DM_communication import get_docs_VM, update_one_VM
#code to add the gram type features
#dictionary of bacteria and their Gram classification
bacteria_gram_dict = {
'bs134': 1, # Gram-positive
'li142': 1,
'ys134': 0, # Gram-negative
'pp46': 0,
'coli': 0,
'ns139': 0,
'pp6': 0,
'se26': 1,
'se9': 1,
}
#let's add a shape column
bacteria_shape_dict={
'bs134': 0, # Baccilus
'li142': 0,
'ys134': 0,
'pp46': 0,
'coli': 0,
'ns139': 1, #Coccus
'pp6': 0,
'se26': 1,
'se9': 1,
}
def add_shape_to_docs(db_name, coll_name, bacteria_shape_dict):
docs = get_docs_VM(db_name, coll_name, query={})
for doc in docs:
bacteria_name = doc.get('bacteria', None)
if bacteria_name is None:
print(f"Document {doc['_id']} does not have a 'bacteria' field.")
continue
shape_type = bacteria_shape_dict.get(bacteria_name, None)
if shape_type is not None:
filt = {'_id': doc['_id']}
update_one_VM(db_name, coll_name, filt, {"$set": {"shape_type": shape_type}})
else:
print(f"Bacteria name {bacteria_name} not found in dictionary.")
print("Documents updated with shape_type.")
def print_shape_types(db_name, coll_name):
#lets test our new function
#fetch all documents to print gram types
docs = get_docs_VM(db_name, coll_name, query={})
for doc in docs:
bacteria_name = doc.get('bacteria', None)
shape_type = doc.get('shape_type', None)
print(f"Bacteria: {bacteria_name}, shape type: {shape_type}")
#loop through the dictionnary to add a key gram_type from known list of bacteria
def add_gram_type_to_docs(db_name, coll_name, bacteria_gram_dict):
docs = get_docs_VM(db_name, coll_name, query={})
for doc in docs:
bacteria_name = doc.get('bacteria', None)
if bacteria_name is None:
print(f"Document {doc['_id']} does not have a 'bacteria' field.")
continue
gram_type = bacteria_gram_dict.get(bacteria_name, None)
if gram_type is not None:
filt = {'_id': doc['_id']}
update_one_VM(db_name, coll_name, filt, {"$set": {"gram_type": gram_type}})
else:
print(f"Bacteria name {bacteria_name} not found in dictionary.")
print("Documents updated with gram_type.")
def print_gram_types(db_name, coll_name):
#lets test our new function
#fetch all documents to print gram types
docs = get_docs_VM(db_name, coll_name, query={})
for doc in docs:
bacteria_name = doc.get('bacteria', None)
gram_type = doc.get('gram_type', None)
print(f"Bacteria: {bacteria_name}, Gram type: {gram_type}")
if __name__ == '__main__':
#add_gram_type_to_docs('Optical_Trapping_ML_DB', 'tseries_Gram_analysis', bacteria_gram_dict)
#print_gram_types('Optical_Trapping_ML_DB', 'tseries_Gram_analysis')
add_shape_to_docs('Optical_Trapping_ML_DB', 'tseries_Gram_analysis', bacteria_shape_dict)
print_shape_types('Optical_Trapping_ML_DB', 'tseries_Gram_analysis')

Event Timeline