def is_valid(self): self.logger.debug("In is_valid.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) if self._local_file is None: self.logger.error("Must set the local file of the sequence set.") valid = False if self._remote_path is None: self.logger.error("Must set the remote path for the sequence set.") valid = False if "sequenced_from" not in self._links.keys(): self.logger.error("Must have of 'sequenced_from' linkage.") valid = False self.logger.debug("Valid? %s" + str(valid)) return valid
def validate(self): """ Validates the current object's data against the schema in the OSDF instance. Args: None Returns: A list of strings, where each string is a validation error that the OSDF instance identified. """ self.logger.debug("In validate.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) problems = [] if not valid: self.logger.info("Validation did not succeed for " + __name__ + ".") problems.append(error_message) if 'associated_with' not in self._links.keys(): problems.append("Must add an 'associated_with' link to a visit.") self.logger.debug("Number of validation problems: %s." % len(problems)) return problems
def is_valid(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for the specific object. However, unlike validates(), this method does not provide exact error messages, it states if the validation was successful or not. Args: None Returns: True if the data validates, False if the current state of fields in the instance do not validate with the OSDF instance """ self.logger.debug("In is_valid.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) if self._local_file is None: self.logger.error("Must set the local file of the sequence set.") valid = False if 'computed_from' not in self._links.keys(): self.logger.error("Must have of 'computed_from' linkage.") valid = False self.logger.debug("Valid? %s" % str(valid)) return valid
def delete(self): self.logger.debug("In delete.") if self._id is None: self.logger.warn("Attempt to delete a sixteensdnaprep with no ID.") raise Exception("Object does not have an ID.") prep_id = self._id session = iHMPSession.get_session() self.logger.info("Got iHMP session.") # Assume failure success = False try: self.logger.info("Deleting " + __name__ + " with OSDF ID %s." % prep_id) session.get_osdf().delete_node(prep_id) success = True except Exception as e: self.logger.error( "An error occurred when deleting " + __name__ + " %s." + "Reason: %s" % prep_id, e.strerror ) return success
def child_seq_sets(self): """ Return iterator of all sequence sets descended from this prep. """ self.logger.debug("In child_seq_sets.") linkage_query = '"{}"[linkage.sequenced_from]'.format(self.id) from WgsRawSeqSet import WgsRawSeqSet from MicrobTranscriptomicsRawSeqSet import MicrobTranscriptomicsRawSeqSet from ViralSeqSet import ViralSeqSet query = iHMPSession.get_session().get_osdf().oql_query for page_no in count(1): res = query(WgsDnaPrep.namespace, linkage_query, page=page_no) res_count = res['result_count'] for doc in res['results']: if doc['node_type'] == "wgs_raw_seq_set": yield WgsRawSeqSet.load_wgsRawSeqSet(doc) elif doc['node_type'] == "viral_seq_set": yield ViralSeqSet.load_viral_seq_set(doc) elif doc['node_type'] == "microb_transcriptomics_raw_seq_set": yield MicrobTranscriptomicsRawSeqSet.load_microb_transcriptomics_raw_set_set(doc) res_count -= len(res['results']) if res_count < 1: break
def load(subject_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: subject_id (str): The OSDF ID for the document to load. Returns: A Subject object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % subject_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") subject_data = session.get_osdf().get_node(subject_id) module_logger.info("Creating a template Subject.") subject = Subject() module_logger.debug("Filling in Subject details.") subject._set_id(subject_data['id']) subject._links = subject_data['linkage'] subject._version = subject_data['ver'] subject._gender = subject_data['meta']['gender'] subject._rand_subject_id = subject_data['meta']['rand_subject_id'] subject._tags = subject_data['meta']['tags'] if 'race' in subject_data['meta']: subject._race = subject_data['meta']['race'] module_logger.debug("Returning loaded Subject.") return subject
def load(project_id): module_logger.debug("In load. Specified ID: %s" % project_id) # use the OSDF get_node() to load the data session = iHMPSession.get_session() module_logger.info("Got iHMP session.") project_data = session.get_osdf().get_node(project_id) module_logger.info("Creating a template project.") project = Project() module_logger.debug("Filling in project details.") project._set_id(project_data['id']) # For version, the key to use is simply 'ver' project._links = project_data['linkage'] project._version = project_data['ver'] project._tags = project_data['meta']['tags'] project._mixs = project_data['meta']['mixs'] project._description = project_data['meta']['description'] project._name = project_data['meta']['name'] module_logger.debug("Returning loaded project.") return project
def load(visit_node_id): module_logger.debug("In load. Specified ID: %s" % visit_node_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") visit_data = session.get_osdf().get_node(visit_node_id) module_logger.info("Creating a template Visit.") visit = Visit() module_logger.debug("Filling in Visit details.") # The attributes commmon to all iHMP nodes visit._set_id(visit_data['id']) visit._version = visit_data['ver'] visit._links = visit_data['linkage'] # The attributes that are particular to Visit objects visit._visit_id = visit_data['meta']['visit_id'] visit._visit_number = visit_data['meta']['visit_number'] visit._date = visit_data['meta']['date'] visit._interval = visit_data['meta']['interval'] if 'clinic_id' in visit_data['meta']: module_logger.info("Visit data has 'clinic_id' present.") visit._clinic_id = visit_data['meta']['clinic_id'] if 'tags' in visit_data['meta']: module_logger.info("Visit data has 'tags' present.") visit._tags= visit_data['meta']['tags'] module_logger.debug("Returning loaded Visit.") return visit
def validate(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for that specific object. All required fields for that specific object must be present. Args: None Returns: A list of strings, where each string is the error that the validation raised during OSDF validation """ self.logger.debug("In validate.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) problems = [] if not valid: self.logger.info("Validation did not succeed.") problems.append(error_message) self.logger.debug("Number of validation problems: %s." % len(problems)) return problems
def load(study_id): module_logger.debug("In load. Specified ID: %s" % study_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") study_data = session.get_osdf().get_node(study_id) module_logger.info("Creating a template Study.") study = Study() module_logger.debug("Filling in Study details.") study._set_id(study_data['id']) # For version, the key to use is simply 'ver' study._version = study_data['ver'] study._links = study_data['linkage'] # The attributes that are particular to Study objects study._name = study_data['meta']['name'] study._description = study_data['meta']['description'] study._center = study_data['meta']['center'] study._contact = study_data['meta']['contact'] study._tags = study_data['meta']['tags'] if 'srp_id' in study_data['meta']: study._srp_id = study_data['meta']['srp_id'] module_logger.debug("Returning loaded Study.") return study
def validate(self): self.logger.debug("In validate.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) problems = [] if not valid: self.logger.info("Validation did not succeed for " + __name__ + ".") problems.append(error_message) if self._local_file is None: problems.append("Local file is not yet set.") if self._remote_path is None: problems.append("Remote path is not yet set.") if "sequenced_from" not in self._links.keys(): problems.append("Must add a 'sequenced_from' link to a 16s_dna_prep.") self.logger.debug("Number of validation problems: %s." % len(problems)) return problems
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False if self._id is None: # The document has not been saved before visit_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(visit_data) self.logger.info("Save for visit %s successful." % node_id) self.logger.debug("Setting ID for visit %s." % node_id) self._set_id(node_id) self.version = 1 success = True except Exception as e: self.logger.error( "An error occurred while inserting visit %s." + "Reason: %s" % e) else: visit_data = self._get_raw_doc() try: self.logger.info("Attempting to update visit with ID: %s." % self._id) session.get_osdf().edit_node(visit_data) self.logger.info("Update for visit %s successful." % self._id) success = True except Exception as e: self.logger.error( "An error occurred while updating visit %s." + "Reason: %s" % self._id, e) return success
def abundance_matrices(self): """ Returns an iterator of all AbundanceMatrix nodes connected to this object. """ self.logger.debug("In abundance_matrices().") linkage_query = '"{}"[linkage.computed_from]'.format(self.id) query = iHMPSession.get_session().get_osdf().oql_query from AbundanceMatrix import AbundanceMatrix for page_no in count(1): res = query(WgsRawSeqSetPrivate.namespace, linkage_query, page=page_no) res_count = res['result_count'] for doc in res['results']: yield AbundanceMatrix.load_abundance_matrix(doc) res_count -= len(res['results']) if res_count < 1: break
def load(sample_id): module_logger.debug("In load. Specified ID: %s" % sample_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") sample_data = session.get_osdf().get_node(sample_id) module_logger.info("Creating a template Sample.") sample = Sample() module_logger.debug("Filling in Sample details.") sample._set_id(sample_data['id']) # ver, not version for the key sample._version = sample_data['ver'] sample._tags = sample_data['meta']['tags'] sample._mixs = sample_data['meta']['mixs'] sample._fma_body_site = sample_data['meta']['fma_body_site'] if 'body_site' in sample_data['meta']: sample._body_site = sample_data['meta']['body_site'] if 'supersite' in sample_data['meta']: sample._supersite= sample_data['meta']['supersite'] module_logger.debug("Returning loaded Sample.") return sample
def load(seq_set_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: seq_set_id (str): The OSDF ID for the document to load. Returns: A WgsAssembledSeqSet object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % seq_set_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") seq_set_data = session.get_osdf().get_node(seq_set_id) module_logger.info("Creating a template " + __name__ + ".") seq_set = WgsAssembledSeqSet.load_wgsAssembledSeqSet(seq_set_data) module_logger.debug("Returning loaded " + __name__) return seq_set
def delete(self): """ Deletes the current object (self) from the OSDF instance. If the object has not been saved previously (node ID is not set), then an error message will be logged stating the object was not deleted. If the ID is set, and exists in the OSDF instance, then the object will be deleted from the OSDF instance, and this object must be re-saved in order to use it again. Args: None Returns: True upon successful deletion, False otherwise. """ self.logger.debug("In delete.") if self._id is None: self.logger.warn("Attempt to delete a project with no ID.") raise Exception("project does not have an ID.") project_id = self._id session = iHMPSession.get_session() self.logger.info("Got iHMP session.") # Assume failure success = False try: self.logger.info("Deleting project with ID %s." % project_id) session.get_osdf().delete_node(project_id) success = True except Exception as e: self.logger.error("An error occurred when deleting project %s. " + "Reason: %s" % project_id, e) return success
def annotations(self): """ Returns an iterator of all Annotation nodes connected to this object. """ self.logger.debug("In annotations().") linkage_query = '"annotation"[node_type] && "{}"[linkage.computed_from]'.format(self.id) query = iHMPSession.get_session().get_osdf().oql_query from Annotation import Annotation for page_no in count(1): res = query(WgsAssembledSeqSet.namespace, linkage_query, page=page_no) res_count = res['result_count'] for doc in res['results']: yield Annotation.load_annotation(doc) res_count -= len(res['results']) if res_count < 1: break
def validate(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for that specific object. All required fields for that specific object must be present. Args: None Returns: A list of strings, where each string is the error that the validation raised during OSDF validation """ self.logger.debug("In validate.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) problems = [] if not valid: self.logger.info("Validation did not succeed for Sample.") problems.append(error_message) if 'collected_during' not in self._links.keys(): problems.append("Must add a 'collected_during' key-value pair in the links") self.logger.debug("Number of validation problems: %s." % len(problems)) return problems
def is_valid(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for the specific object. However, unlike validates(), this method does not provide exact error messages, it states if the validation was successful or not. Args: None Returns: True if the data validates, False if the current state of fields in the instance do not validate with the OSDF instance """ self.logger.debug("In is_valid.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) if 'collected_during' not in self._links.keys(): valid = False self.logger.debug("Valid? %s" % str(valid)) return valid
def load(seq_set_id): module_logger.debug("In load. Specified ID: %s" % seq_set_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") seq_set_data = session.get_osdf().get_node(seq_set_id) module_logger.info("Creating a template " + __name__ + ".") seq_set = SixteenSTrimmedSeqSet() module_logger.debug("Filling in " + __name__ + " details.") # The attributes commmon to all iHMP nodes seq_set._set_id(seq_set_data['id']) seq_set._version = seq_set_data['ver'] seq_set._links = seq_set_data['linkage'] # The attributes that are particular to SixteenSTrimmedSeqSet documents seq_set._checksums = seq_set_data['meta']['checksums'] seq_set._comment = seq_set_data['meta']['comment'] seq_set._format = seq_set_data['meta']['format'] seq_set._format_doc = seq_set_data['meta']['format_doc'] seq_set._size = seq_set_data['meta']['size'] seq_set._urls = seq_set_data['meta']['urls'] seq_set._tags = seq_set_data['meta']['tags'] if 'sequence_type' in seq_set_data['meta']: module_logger.info(__name__ + " data has 'sequence_type' present.") seq_set._sequence_type = seq_set_data['meta']['sequence_type'] module_logger.debug("Returning loaded " + __name__) return seq_set
def is_valid(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for the specific object. However, unlike validates(), this method does not provide exact error messages, it states if the validation was successful or not. Args: None Returns: True if the data validates, False if the current state of fields in the instance do not validate with the OSDF instance """ self.logger.debug("In is_valid.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) self.logger.debug("Valid? %s" + str(valid)) return valid
def save(self): self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False study = self._study remote_path = "/".join(["/" + study, "wgs_raw_seq_set", os.path.basename(self._local_file)]) self.logger.debug("Remote path for this file will be %s." % remote_path) # Upload the file to the iHMP aspera server upload_result = aspera.upload_file(WgsRawSeqSet.aspera_server, session.username, session.password, self._local_file, remote_path) if not upload_result: self.logger.error("Experienced an error uploading the sequence set. Aborting save.") return False if self.id is None: # The document has not yet been save seq_set_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(seq_set_data) self.logger.info("Save for " + __name__ + " %s successful." % node_id) self.logger.info("Setting ID for " + __name__ + " %s." % node_id) self._set_id(node_id) self._urls = [ "fasp://" + WgsRawSeqSet.aspera_server + remote_path ] self._version = 1 success = True except Exception as e: self.logger.error("An error occurred while saving " + __name__ + ". " + "Reason: %s" % e) else: seq_set_data = self._get_raw_doc() try: self.logger.info("Attempting to update " + __name__ + " with ID: %s." % self._id) session.get_osdf().edit_node(seq_set_data) self.logger.info("Update for " + __name__ + " %s successful." % self._d) success = True except Exception as e: self.logger.error("An error occurred while updating " + __name__ + " %s. Reason: %s" % self._d, e) self.logger.debug("Returning " + str(success)) return success
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False if self.id is None: # The document has not yet been save prep_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(prep_data) self.logger.info("Save for sixteensdnaprep %s successful." % node_id) self.logger.info("Setting ID for sixteensdnaprep %s." % node_id) self._set_id(node_id) self._version = 1 success = True except Exception as e: self.logger.exception(e) self.logger.error("An error occurred when inserting %s.", self) else: prep_data = self._get_raw_doc() try: self.logger.info("Attempting to update " + __name__ + " with ID: %s." % self._id) session.get_osdf().edit_node(prep_data) self.logger.info("Update for " + __name__ + " %s successful." % self._id) success = True except Exception as e: self.logger.error("An error occurred while updating " + __name__ + " %s. Reason: %s" % self._id, e) return success
def save(self): """ Saves the data to OSDF. The JSON form of the object is not valid, then the data is not saved. If the instance was saved previously, then the node ID is assigned the alphanumeric assigned by the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful save, will be defined as the alphanumeric ID from OSDF. In addition, the document's version is updated when a successful save operation is completed. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False if self._id is None: # The document has not yet been saved sample_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(sample_data) self.logger.info("Save for Sample %s successful." % node_id) self.logger.info("Setting ID for Sample %s." % node_id) self._set_id(node_id) self.version = 1 success = True except Exception as e: self.logger.error("An error occurred while saving Sample. " + "Reason: %s" % e) else: sample_data = self._get_raw_doc() try: self.logger.info("Attempting to update Sample with ID: %s." % self.id) session.get_osdf().edit_node(sample_data) self.logger.info("Update for Sample %s successful." % self.id) success = True except Exception as e: msg = "An error occurred while updating " + \ "Sample %s. Reason: %s" % (self.id, e) self.logger.error(msg) return success
def save(self): # Use the create_osdf_node if the node has ID = -1 # if saving the first time, must also use create_osdf_node # if node previously saved, use edit_node instead since ID is given # (an update in a way) can also use get_node to check if the # node already exists if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False # Before save, make sure that linkage is non-empty, the key should # be collected-during self.logger.debug("In save.") session = iHMPSession.get_session() self.logger.info("Got iHMP session.") osdf = session.get_osdf() success = False if self.id is None: # The document has not been saved before project_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = osdf.insert_node(project_data) self.logger.info("Save for Project %s successful." % node_id) self.logger.debug("Setting ID for Project %s." % node_id) self._set_id(node_id) self.version = 1 success = True except Exception as e: self.logger.error("An error occurred while inserting Project %s." + "Reason: %s" % self._name, e) else: project_data = self._get_raw_doc() try: self.logger.info("Attempting to update Project with ID: %s." % self._id) osdf.edit_node(project_data) self.logger.info("Update for Project %s successful." % self._id) updated_data = osdf.get_node(self._id) latest_version = updated_data['ver'] self.logger.debug("The new version of this Project is now: %s" % str(latest_version)) self.version = latest_version success = True except Exception as e: self.logger.error("An error occurred while updating Project %s. " + "Reason: %s" % (self.id, e)) return success
def load(annot_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: annot_id (str): The OSDF ID for the document to load. Returns: A Annotation object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % annot_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") annot_data = session.get_osdf().get_node(annot_id) module_logger.info("Creating a template Annotation.") annot = Annotation() module_logger.debug("Filling in Annotation details.") # Node required fields annot._set_id(annot_data['id']) annot._links = annot_data['linkage'] annot._version = annot_data['ver'] # Required fields annot._annotation_pipeline = annot_data['meta']['annotation_pipeline'] annot._checksums = annot_data['meta']['checksums'] annot._format = annot_data['meta']['format'] annot._format_doc = annot_data['meta']['format_doc'] annot._orf_process = annot_data['meta']['orf_process'] annot._study = annot_data['meta']['study'] annot._tags = annot_data['meta']['tags'] annot._urls = annot_data['meta']['urls'] # Handle Annotation optional properties if 'comment' in annot_data['meta']: annot._comment = annot_data['meta']['comment'] if 'date' in annot_data['meta']: annot._date = annot_data['meta']['date'] if 'sop' in annot_data['meta']: annot._sop = annot_data['meta']['sop'] if 'annotation_source' in annot_data['meta']: annot._annotation_source = annot_data['meta']['annotation_source'] module_logger.debug("Returning loaded Annotation.") return annot
def load(seq_set_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: seq_set_id (str): The OSDF ID for the document to load. Returns: A ClusteredSeqSet object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % seq_set_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") css_data = session.get_osdf().get_node(seq_set_id) module_logger.info("Creating a template ClusteredSeqSet.") css = ClusteredSeqSet() module_logger.debug("Filling in ClusteredSeqSet details.") # Node required fields css._set_id(css_data['id']) css._links = css_data['linkage'] css._version = css_data['ver'] # Required fields css._checksums = css_data['meta']['checksums'] css._comment = css_data['meta']['comment'] css._format = css_data['meta']['format'] css._clustering_process = css_data['meta']['clustering_process'] css._sequence_type = css_data['meta']['sequence_type'] css._size = css_data['meta']['size'] css._study = css_data['meta']['study'] css._subtype = css_data['meta']['subtype'] css._tags = css_data['meta']['tags'] css._urls = css_data['meta']['urls'] # Handle ClusteredSeqSet optional properties if 'date' in css_data['meta']: css._date = css_data['meta']['date'] if 'format_doc' in css_data['meta']: css._format_doc = css_data['meta']['format_doc'] if 'sop' in css_data['meta']: css._sop = css_data['meta']['sop'] module_logger.debug("Returning loaded ClusteredSeqSet.") return css
def load(prep_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: prep_id (str): The OSDF ID for the document to load. Returns: A WgsDnaPrep object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % prep_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") prep_data = session.get_osdf().get_node(prep_id) module_logger.info("Creating a template " + __name__ + ".") prep = WgsDnaPrep() module_logger.debug("Filling in " + __name__ + " details.") # The attributes commmon to all iHMP nodes prep._set_id(prep_data['id']) prep._version = prep_data['ver'] prep._links = prep_data['linkage'] # The attributes that are particular to WgsDnaPrep documents prep._comment = prep_data['meta']['comment'] prep._lib_layout = prep_data['meta']['lib_layout'] prep._lib_selection = prep_data['meta']['lib_selection'] prep._mims = prep_data['meta']['mims'] prep._ncbi_taxon_id = prep_data['meta']['ncbi_taxon_id'] prep._prep_id = prep_data['meta']['prep_id'] prep._sequencing_center = prep_data['meta']['sequencing_center'] prep._sequencing_contact = prep_data['meta']['sequencing_contact'] prep._storage_duration = prep_data['meta']['storage_duration'] prep._tags = prep_data['meta']['tags'] if 'frag_size' in prep_data['meta']: module_logger.info(__name__ + " data has 'frag_size' present.") prep._frag_size = prep_data['meta']['frag_size'] if 'srs_id' in prep_data['meta']: module_logger.info(__name__ + " data has 'srs_id' present.") prep._srs_id = prep_data['meta']['srs_id'] module_logger.debug("Returning loaded " + __name__) return prep
def load(prep_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: prep_id (str): The OSDF ID for the document to load. Returns: A SixteenSDnaPrep object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % prep_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") prep_data = session.get_osdf().get_node(prep_id) module_logger.info("Creating a template " + __name__ + ".") prep = SixteenSDnaPrep() module_logger.debug("Filling in " + __name__ + " details.") # The attributes commmon to all iHMP nodes prep._set_id(prep_data['id']) prep._version = prep_data['ver'] prep._links = prep_data['linkage'] # The attributes that are particular to SixteenSDnaPrep documents prep._comment = prep_data['meta']['comment'] prep._lib_layout = prep_data['meta']['lib_layout'] prep._lib_selection = prep_data['meta']['lib_selection'] prep._mimarks = prep_data['meta']['mimarks'] prep._ncbi_taxon_id = prep_data['meta']['ncbi_taxon_id'] prep._prep_id = prep_data['meta']['prep_id'] prep._sequencing_center = prep_data['meta']['sequencing_center'] prep._sequencing_contact = prep_data['meta']['sequencing_contact'] prep._storage_duration = prep_data['meta']['storage_duration'] prep._tags = prep_data['meta']['tags'] if 'frag_size' in prep_data['meta']: module_logger.info(__name__ + " data has 'frag_size' present.") prep._frag_size = prep_data['meta']['frag_size'] if 'srs_id' in prep_data['meta']: module_logger.info(__name__ + " data has 'srs_id' present.") prep._srs_id = prep_data['meta']['srs_id'] module_logger.debug("Returning loaded " + __name__) return prep
def search(self, query): """ Searches the OSDF instance using the specified input parameters Args: Returns: """ self.logger.debug("In search.") session = iHMPSession.get_session() self.logger.info("Got iHMP session.")
def is_valid(self): self.logger.debug("In is_valid.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) self.logger.debug("Valid? %s" + str(valid)) return valid
def _sample_attr_docs(self): linkage_query = '"{}"[linkage.associated_with]'.format(self.id) query = iHMPSession.get_session().get_osdf().oql_query for page_no in count(1): res = query(Sample.namespace, linkage_query, page=page_no) res_count = res['result_count'] for doc in res['results']: yield doc res_count -= len(res['results']) if res_count < 1: break
def load(seq_set_id): """ Loads the data for the specified input ID from OSDF to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: seq_set_id (str): The OSDF ID for the document to load. Returns: A MicrobTranscriptomicsRawSeqSet object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % seq_set_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") seq_set_data = session.get_osdf().get_node(seq_set_id) module_logger.info("Creating a template " + __name__ + ".") seq_set = MicrobTranscriptomicsRawSeqSet() module_logger.debug("Filling in " + __name__ + " details.") # The attributes commmon to all iHMP nodes seq_set._set_id(seq_set_data['id']) seq_set._version = seq_set_data['ver'] seq_set._links = seq_set_data['linkage'] # The attributes that are required seq_set._checksums = seq_set_data['meta']['checksums'] seq_set._comment = seq_set_data['meta']['comment'] seq_set._exp_length = seq_set_data['meta']['exp_length'] seq_set._format = seq_set_data['meta']['format'] seq_set._format_doc = seq_set_data['meta']['format_doc'] seq_set._seq_model = seq_set_data['meta']['seq_model'] seq_set._size = seq_set_data['meta']['size'] seq_set._urls = seq_set_data['meta']['urls'] seq_set._tags = seq_set_data['meta']['tags'] seq_set._study = seq_set_data['meta']['study'] # Optional attributes. if 'sequence_type' in seq_set_data['meta']: module_logger.info(__name__ + " data has 'sequence_type' present.") seq_set._sequence_type = seq_set_data['meta']['sequence_type'] module_logger.debug("Returning loaded " + __name__) return seq_set
def load(node_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: node_id (str): The OSDF ID for the document to load. Returns: A ViralSeqSet object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % node_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") node_data = session.get_osdf().get_node(node_id) module_logger.info("Creating a template ViralSeqSet.") node = ViralSeqSet() module_logger.debug("Filling in ViralSeqSet details.") # Node required fields node._set_id(node_data["id"]) node._links = node_data["linkage"] node._version = node_data["ver"] # Required fields node._checksums = node_data["meta"]["checksums"] node._format = node_data["meta"]["format"] node._format_doc = node_data["meta"]["format_doc"] node._study = node_data["meta"]["study"] node._tags = node_data["meta"]["tags"] node._urls = node_data["meta"]["urls"] # Handle ViralSeqSet optional properties if "comment" in node_data["meta"]: node._comment = node_data["meta"]["comment"] if "format" in node_data["meta"]: node._format = node_data["meta"]["format"] if "format_doc" in node_data["meta"]: node._format_doc = node_data["meta"]["format_doc"] module_logger.debug("Returning loaded ViralSeqSet.") return node
def save(self): self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False # Upload the file to the iHMP aspera server upload_result = aspera.upload_file( "aspera.ihmpdcc.org", session.username, session.password, self._local_file, self._remote_path ) if not upload_result: self.logger.error("Experienced an error uploading the sequence set. Aborting save.") raise Exception("Unable to upload file to aspera server.") return success if self.id is None: # The document has not yet been save seq_set_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(seq_set_data) self.logger.info("Save for " + __name__ + " %s successful." % node_id) self.logger.info("Setting ID for " + __name__ + " %s." % node_id) self._set_id(node_id) self._version = 1 success = True except Exception as e: self.logger.error("An error occurred while saving " + __name__ + ". " + "Reason: %s" % e) else: seq_set_data = self._get_raw_doc() try: self.logger.info("Attempting to update " + __name__ + " with ID: %s." % self._id) session.get_osdf().edit_node(seq_set_data) self.logger.info("Update for " + __name__ + " %s successful." % self._d) success = True except Exception as e: self.logger.error("An error occurred while updating " + __name__ + " %s. Reason: %s" % self._d, e) return success
def search(self, query): """ Searches the OSDF instance using the specified input parameters Args: Returns: """ self.logger.debug("In search.") #searching without any parameters will return all different results session = iHMPSession.get_session() self.logger.info("Got iHMP session.")
def load(seq_set_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: seq_set_id (str): The OSDF ID for the document to load. Returns: A WgsRawSeqSet object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % seq_set_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") seq_set_data = session.get_osdf().get_node(seq_set_id) module_logger.info("Creating a template " + __name__ + ".") seq_set = WgsRawSeqSet() module_logger.debug("Filling in " + __name__ + " details.") # The attributes commmon to all iHMP nodes seq_set._set_id(seq_set_data['id']) seq_set._version = seq_set_data['ver'] seq_set._links = seq_set_data['linkage'] # The attributes that are particular to WgsRawSeqSet documents seq_set._checksums = seq_set_data['meta']['checksums'] seq_set._comment = seq_set_data['meta']['comment'] seq_set._exp_length = seq_set_data['meta']['exp_length'] seq_set._format = seq_set_data['meta']['format'] seq_set._format_doc = seq_set_data['meta']['format_doc'] seq_set._seq_model = seq_set_data['meta']['seq_model'] seq_set._size = seq_set_data['meta']['size'] seq_set._urls = seq_set_data['meta']['urls'] seq_set._tags = seq_set_data['meta']['tags'] if 'sequence_type' in seq_set_data['meta']: module_logger.info(__name__ + " data has 'sequence_type' present.") seq_set._sequence_type = seq_set_data['meta']['sequence_type'] module_logger.debug("Returning loaded " + __name__) return seq_set
def load(visit_node_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: visit_node_id (str): The OSDF ID for the document to load. Returns: A Visit object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % visit_node_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") visit_data = session.get_osdf().get_node(visit_node_id) module_logger.info("Creating a template Visit.") visit = Visit() module_logger.debug("Filling in Visit details.") # The attributes commmon to all iHMP nodes visit._set_id(visit_data['id']) visit._version = visit_data['ver'] visit._links = visit_data['linkage'] # The attributes that are particular to Visit objects visit._visit_id = visit_data['meta']['visit_id'] visit._visit_number = visit_data['meta']['visit_number'] visit._date = visit_data['meta']['date'] visit._interval = visit_data['meta']['interval'] if 'clinic_id' in visit_data['meta']: module_logger.info("Visit data has 'clinic_id' present.") visit._clinic_id = visit_data['meta']['clinic_id'] if 'tags' in visit_data['meta']: module_logger.info("Visit data has 'tags' present.") visit._tags = visit_data['meta']['tags'] module_logger.debug("Returning loaded Visit.") return visit
def load(study_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: study_id (str): The OSDF ID for the document to load. Returns: A Study object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % study_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") study_data = session.get_osdf().get_node(study_id) module_logger.info("Creating a template Study.") study = Study() module_logger.debug("Filling in Study details.") study._set_id(study_data['id']) # For version, the key to use is simply 'ver' study._version = study_data['ver'] study._links = study_data['linkage'] # The attributes that are particular to Study objects study._name = study_data['meta']['name'] study._description = study_data['meta']['description'] study._center = study_data['meta']['center'] study._contact = study_data['meta']['contact'] study._tags = study_data['meta']['tags'] if 'srp_id' in study_data['meta']: study._srp_id = study_data['meta']['srp_id'] module_logger.debug("Returning loaded Study.") return study
def search(query="\"16s_trimmed_seq_set\"[node_type]"): """ Searches the OSDF database through all SixteenSTrimmedSeqSet node types. Any criteria the user wishes to add is provided by the user in the query language specifications provided in the OSDF documentation. A general format is (including the quotes and brackets): "search criteria"[field to search] If there are any results, they are returned as a SixteenSTrimmedSeqSet instance, otherwise an empty list will be returned. Args: query (str): The query for the OSDF framework. Defaults to the SixteenSTrimmedSeqSet node type. Returns: Returns an array of SixteenSTrimmedSeqSet objects. It returns an empty list if there are no results. """ module_logger.debug("In search.") #searching without any parameters will return all different results session = iHMPSession.get_session() module_logger.info("Got iHMP session.") if query != "\"16s_trimmed_seq_set\"[node_type]": query = query + " && \"16s_trimmed_seq_set\"[node_type]" sixteenSTrimmedSeqSet_data = session.get_osdf().oql_query( "ihmp", query) all_results = sixteenSTrimmedSeqSet_data['results'] result_list = list() if len(all_results) > 0: for i in all_results: sixteenSTrimmedSeqSet_result = SixteenSTrimmedSeqSet.load_sixteenSTrimmedSeqSet( i) result_list.append(sixteenSTrimmedSeqSet_result) return result_list
def validate(self): """ Validates the current object's data/JSON against the current schema in the OSDF instance for that specific object. All required fields for that specific object must be present. Args: None Returns: A list of strings, where each string is the error that the validation raised during OSDF validation """ self.logger.debug("In validate.") document = self._get_raw_doc() session = iHMPSession.get_session() self.logger.info("Got iHMP session.") (valid, error_message) = session.get_osdf().validate_node(document) problems = [] if not valid: self.logger.info("Validation did not succeed for " + __name__ + ".") problems.append(error_message) if self._local_file is None: problems.append("Local file is not yet set.") elif not os.path.isfile(self._local_file): problems.append("Local file does not point to an actual file.") if 'sequenced_from' not in self._links.keys(): problems.append( "Must add a 'sequenced_from' link to a wgs_dna_prep.") self.logger.debug("Number of validation problems: %s." % len(problems)) return problems
def search(query="\"sample\"[node_type]"): module_logger.debug("In search.") #searching without any parameters will return all different results session = iHMPSession.get_session() module_logger.info("Got iHMP session.") if query != "\"sample\"[node_type]": query = query + " && \"sample\"[node_type]" sample_data = session.get_osdf().oql_query("ihmp", query) all_results = sample_data['results'] result_list = list() if len(all_results) > 0: for i in all_results: sample_result = Sample.load_sample(i) result_list.append(sample_result) return result_list
def load(sample_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: sample_id (str): The OSDF ID for the document to load. Returns: A Sample object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % sample_id) session = iHMPSession.get_session() module_logger.info("Got iHMP session.") sample_data = session.get_osdf().get_node(sample_id) module_logger.info("Creating a template Sample.") sample = Sample() module_logger.debug("Filling in Sample details.") sample._set_id(sample_data['id']) # ver, not version for the key sample._version = sample_data['ver'] sample._tags = sample_data['meta']['tags'] sample._mixs = sample_data['meta']['mixs'] sample._fma_body_site = sample_data['meta']['fma_body_site'] if 'body_site' in sample_data['meta']: sample._body_site = sample_data['meta']['body_site'] if 'supersite' in sample_data['meta']: sample._supersite = sample_data['meta']['supersite'] module_logger.debug("Returning loaded Sample.") return sample
def load(project_id): """ Loads the data for the specified input ID from the OSDF instance to this object. If the provided ID does not exist, then an error message is provided stating the project does not exist. Args: project_id (str): The OSDF ID for the document to load. Returns: A Project object with all the available OSDF data loaded into it. """ module_logger.debug("In load. Specified ID: %s" % project_id) # use the OSDF get_node() to load the data session = iHMPSession.get_session() module_logger.info("Got iHMP session.") project_data = session.get_osdf().get_node(project_id) module_logger.info("Creating a template project.") project = Project() module_logger.debug("Filling in project details.") project._set_id(project_data['id']) # For version, the key to use is simply 'ver' project._links = project_data['linkage'] project._version = project_data['ver'] project._tags = project_data['meta']['tags'] project._mixs = project_data['meta']['mixs'] project._description = project_data['meta']['description'] project._name = project_data['meta']['name'] module_logger.debug("Returning loaded project.") return project
def delete(self): """ Deletes the current object. The object must already have been saved/present in the OSDF instance, so an ID for the object must have been already set. Args: None Returns: True if the object was successfully deleted, False otherwise Exceptions: Exception: If the instance does not have an ID set (was never saved in OSDF) """ self.logger.debug("In delete.") if self._id is None: self.logger.warn("Attempt to delete a node with no ID.") raise Exception("Node does not have an ID.") visit_node_id = self._id session = iHMPSession.get_session() self.logger.info("Got iHMP session.") # Assume failure success = False try: self.logger.info("Deleting node with OSDF ID %s." % visit_node_id) session.get_osdf().delete_node(visit_node_id) success = True except Exception as e: self.logger.error( "An error occurred when deleting node %s." + "Reason: %s" % visit_node_id, e.strerror) return success
def delete(self): """ Deletes the current object (self) from the OSDF instance. If the object has not been saved previously (node ID is not set), then an error message will be logged stating the object was not deleted. If the ID is set, and exists in the OSDF instance, then the object will be deleted from the OSDF instance, and this object must be re-saved in order to use it again. Args: None Returns: True upon successful deletion, False otherwise. """ self.logger.debug("In delete.") if self._id is None: self.logger.warn("Attempt to delete a Study with no ID.") raise Exception("Study does not have an ID.") study_id = self._id session = iHMPSession.get_session() self.logger.info("Got iHMP session.") # Assume failure success = False try: self.logger.info("Deleting Study with ID %s." % study_id) session.get_osdf().delete_node(study_id) success = True except Exception as e: self.logger.error( "An error occurred when deleting Study %s." + "Reason: %s" % study_id, e.strerror) return success
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False study = self._study remote_path = "/".join([ "/" + study, "wgs_raw_seq_set", os.path.basename(self._local_file) ]) self.logger.debug("Remote path for this file will be %s." % remote_path) # Upload the file to the iHMP aspera server upload_result = aspera.upload_file(WgsRawSeqSet.aspera_server, session.username, session.password, self._local_file, remote_path) if not upload_result: self.logger.error( "Experienced an error uploading the sequence set. Aborting save." ) return False if self.id is None: # The document has not yet been save seq_set_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(seq_set_data) self.logger.info("Save for " + __name__ + " %s successful." % node_id) self.logger.info("Setting ID for " + __name__ + " %s." % node_id) self._set_id(node_id) self._urls = [ "fasp://" + WgsRawSeqSet.aspera_server + remote_path ] self._version = 1 success = True except Exception as e: self.logger.error("An error occurred while saving " + __name__ + ". " + "Reason: %s" % e) else: seq_set_data = self._get_raw_doc() try: self.logger.info("Attempting to update " + __name__ + " with ID: %s." % self._id) session.get_osdf().edit_node(seq_set_data) self.logger.info("Update for " + __name__ + " %s successful." % self._d) success = True except Exception as e: self.logger.error( "An error occurred while updating " + __name__ + " %s. Reason: %s" % self._d, e) self.logger.debug("Returning " + str(success)) return success
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") # If node previously saved, use edit_node instead since ID # is given (an update in a way) # can also use get_node to check if the node already exists if not self.is_valid(): self.logger.error("Cannot save, data is invalid.") return False # Before save, make sure that linkage is non-empty, the key should be collected-during session = iHMPSession.get_session() self.logger.info("Got iHMP session.") success = False if self._id is None: # The document has not yet been saved study_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = session.get_osdf().insert_node(study_data) self.logger.info("Save for Study %s successful." % node_id) self.logger.debug("Setting ID for Study %s." % node_id) self._set_id(node_id) self._version = 1 success = True except Exception as e: self.logger.error("An error occurred while inserting Study. " + "Reason: %s" % e) else: study_data = self._get_raw_doc() try: self.logger.info("Attempting to update Study with ID: %s." % self._id) session.get_osdf().edit_node(study_data) self.logger.info("Update for Study %s successful." % self._id) success = True except Exception as e: self.logger.error( "An error occurred while updating Study %s. " + "Reason: %s" % self._id, e) return success
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ # Use the create_osdf_node if the node has ID = -1 # if saving the first time, must also use create_osdf_node # if node previously saved, use edit_node instead since ID is given # (an update in a way) can also use get_node to check if the # node already exists if not self.is_valid(): self.logger.error("Cannot save, data is invalid") return False # Before save, make sure that linkage is non-empty, the key should # be collected-during self.logger.debug("In save.") session = iHMPSession.get_session() self.logger.info("Got iHMP session.") osdf = session.get_osdf() success = False if self.id is None: # The document has not been saved before project_data = self._get_raw_doc() self.logger.info("Got the raw JSON document.") try: self.logger.info("Attempting to save a new node.") node_id = osdf.insert_node(project_data) self.logger.info("Save for Project %s successful." % node_id) self.logger.debug("Setting ID for Project %s." % node_id) self._set_id(node_id) self.version = 1 success = True except Exception as e: self.logger.error("An error occurred while inserting Project %s." + "Reason: %s" % self._name, e) else: project_data = self._get_raw_doc() try: self.logger.info("Attempting to update Project with ID: %s." % self._id) osdf.edit_node(project_data) self.logger.info("Update for Project %s successful." % self._id) updated_data = osdf.get_node(self._id) latest_version = updated_data['ver'] self.logger.debug("The new version of this Project is now: %s" % str(latest_version)) self.version = latest_version success = True except Exception as e: self.logger.error("An error occurred while updating Project %s. " + "Reason: %s" % (self.id, e)) return success
def save(self): """ Saves the data in the current instance. The JSON form of the current data for the instance is validated in the save function. If the data is not valid, then the data will not be saved. If the instance was saved previously, then the node ID is assigned the alpha numeric found in the OSDF instance. If not saved previously, then the node ID is 'None', and upon a successful, will be assigned to the alpha numeric ID found in the OSDF instance. Also, the version is updated as the data is saved in the OSDF instance. Args: None Returns; True if successful, False otherwise. """ self.logger.debug("In save.") # If node previously saved, use edit_node instead since ID # is given (an update in a way) # can also use get_node to check if the node already exists if not self.is_valid(): self.logger.error("Cannot save, data is invalid.") return False session = iHMPSession.get_session() self.logger.info("Got iHMP session.") osdf = session.get_osdf() success = False if self._id is None: self.logger.info("About to insert a new " + __name__ + " OSDF node.") # Get the JSON form of the data and load it self.logger.debug("Converting Subject to parsed JSON form.") data = json.loads(self.to_json()) try: node_id = osdf.insert_node(data) self._set_id(node_id) self._version = 1 success = True except Exception as e: self.logger.error("Unable to save " + __name__ + ". Reason: %s" % e.strerror) else: self.logger.info( "Subject already has an ID, so we do an update (not an insert)." ) try: subject_data = self._get_raw_doc() self.logger.info( "Subject already has an ID, so we do an update (not an insert)." ) subject_id = self._id self.logger.debug("Subject OSDF ID to update: %s." % subject_id) osdf.edit_node(subject_data) subject_data = osdf.get_node(subject_id) latest_version = subject_data['ver'] self.logger.debug("The version of this Subject is now: %s" % str(latest_version)) self._version = latest_version success = True except Exception as e: self.logger.error("Unable to update " + __name__ + ". Reason: %s" % e.strerror) return success