Exemplo n.º 1
0
def deserialize_contact(data, persistent=False):
    """
    Transforms data (as JSON string or a dict) to a contact object
    """
    if not data:
        return None

    if not persistent:
        pass # TODO if necessary

    else:
        from repository.models import Contact
        from vocabulary.models import CountryCode

        if isinstance(data, basestring):
            data = DictKeyAttribute(simplejson.loads(data))
        elif isinstance(data, dict):
            data = DictKeyAttribute(data)

        try:
            obj = Contact.objects.get(pk=data.pk)
        except Contact.DoesNotExist:
            obj = Contact()
            obj.id = data.pk
            obj.firstname = data.firstname
            obj.middlename = data.middlename
            obj.lastname = data.lastname
            obj.email = data.email
            obj.affiliation = deserialize_institution(data.affiliation, True)
            obj.address = data.address
            obj.city = data.city
            obj.country = deserialize_vocabulary(data.country, True, CountryCode)
            obj.zip = data.zip
            obj.telephone = data.telephone
            obj.creator = deserialize_user(data.creator, True)
            obj.save()

        return obj
Exemplo n.º 2
0
def post_save_submission(xml_file_path):
    clinical_trial_xpath = {
        'trial_id':'',
        'date_registration':'',
        'scientific_title':'trial_identification/scientific_title',
        'scientific_acronym':'trial_identification/scientific_acronym',
        'public_title':'trial_identification/public_title',
        'acronym':'trial_identification/acronym',
        'hc_freetext':'health_conditions/freetext',
        'i_freetext':'interventions/freetext',
        'inclusion_criteria':'recruitment/inclusion_criteria',
        'gender':'recruitment/gender/@value',
        'agemin_value':'recruitment/agemin',
        'agemin_unit':'recruitment/agemin/@unit',
        'agemax_value':'recruitment/agemax',
        'agemax_unit':'recruitment/agemax/@unit',
        'exclusion_criteria':'recruitment/exclusion_criteria',
        'study_design':'study_type/study_design',
        'expanded_access_program':'study_type/study_design/@expanded_access_program',
        'number_of_arms':'study_type/study_design/@number_of_arms',
        'date_enrollment_anticipated':'recruitment/date_enrolment_anticipated',
        'date_enrollment_actual':'recruitment/date_enrolment_actual',
        'target_sample_size':'recruitment/target_size',
        'created':'',
        'updated':'',
        'exported':'',
        'status':'',
        'staff_note':''
    }

    contact_types_map = {
        'public_contact':PublicContact,
        'scientific_contact':ScientificContact,
        'site_contact':SiteContact
    }

    study_design_map = {
        'allocation': StudyAllocation,
        'intervention_assignment':InterventionAssigment,
        'masking': StudyMasking,
        'purpose':  StudyPurpose
    }

    xml = open(xml_file_path)
    tree = ElementTree()
    root = tree.parse(xml)

    ct = ClinicalTrial()
    ct.save()

    # Non-relational Fields from Clinical Trial
    for field,xpath in clinical_trial_xpath.items():
        if xpath != '':
            resultEl = root.xpath(xpath)
            if len(resultEl) > 0:
                if hasattr(resultEl[0],'text'):
                    setattr(ct, field, resultEl[0].text)
                else:
                    setattr(ct, field, resultEl[0])

    # Add Sponsors
    for sponsorNode in root.xpath('sponsors_and_support/*'):
        sponsor = Institution()
        sponsor.name = sponsorNode.find('name').text
        sponsor.address = sponsorNode.find('address').text
        sponsor.country = CountryCode.objects.get(label=sponsorNode.attrib['country_code'])
        sponsor.save()
        if sponsorNode.tag == 'primary_sponsor':
            ct.primary_sponsor = sponsor
        elif sponsorNode.tag == 'secondary_sponsor':
            TrialSecondarySponsor.objects.create(trial=ct,institution=sponsor)
        elif sponsorNode.tag == 'source_support':
            TrialSupportSource.objects.create(trial=ct,institution=sponsor)

    # Add Contacts
    contactList = {}
    for personNode in root.xpath('contacts/person'):
        contact = Contact()

        for attr in ['firstname','middlename','lastname','email','address','city','zip','telephone']:
            value = personNode.find(attr)
            if value is not None:
                setattr(contact, attr, value.text)
        contact.country = CountryCode.objects.get(label=sponsorNode.attrib['country_code'])
        contact.save()
        contactList[ personNode.attrib['pid'] ] = contact

    # Assign PublicContact, ScientificContact and SiteContact to the trial
    for cType,model in contact_types_map.items():
        for typeNode in root.xpath('contacts/'+cType):
            pattern = re.compile('p[0-9]+')
            for person in pattern.findall(typeNode.attrib['persons']):
                model.objects.create(trial=ct,contact=contactList[person])

    # Interventions
    for icodeNode in root.xpath('interventions/i_code'):
        i_code = InterventionCode.objects.get(label=icodeNode.attrib['value'])
        if isinstance(i_code,InterventionCode):
            ct.i_code.add(i_code)

    # Recruitment Country
    for rcountryNode in root.xpath('recruitment/recruitment_country'):
        ccode = CountryCode.objects.get(label=rcountryNode.attrib['value'])
        if isinstance(ccode,CountryCode):
            ct.recruitment_country.add(ccode)

    # StudyType
    study_type_node = StudyType.objects.get(label=root.attrib['type'])
    if study_type_node is not None:
        ct.study_type = study_type_node

    study_design_node = root.find('study_type/study_design')
    if study_design_node is not None:
        for attr,model in study_design_map.items():
            setattr(ct, attr, model.objects.get(label=study_design_node.attrib[attr]))

    study_phase_node = root.find('study_type/phase')
    if study_phase_node is not None:
        ct.phase = StudyPhase.objects.get(label=study_phase_node.attrib['value'])

    recruitment_status = RecruitmentStatus.objects.get(label = root.find('recruitment').attrib['study_status'])
    if recruitment_status is not None:
        ct.status = recruitment_status
Exemplo n.º 3
0
    def set_trial_children(self, ct, fields):

        _vocabularies = {'decs':'DeCS',
                         'icd10':'ICD-10'}

        for country in fields.get('recruitment_country', []):
            if country.get('label', None):
                country_obj, new = CountryCode.objects.get_or_create(
                        label=country['label'],
                        defaults={'description': country.get('description', '')},
                        )
            else:
                country_obj = CountryCode.objects.get(description=country['description'])
            ct.recruitment_country.add(country_obj)

        for person in fields.get('persons', []):

            contact = Contact.objects.filter(email=person['email'], creator=self.creator)

            if contact:
                contact = contact[0]
            else:
                contact = Contact()
                contact.creator = self.creator
                contact.firstname = person.get('firstname')
                contact.middlename = person.get('middlename')
                contact.lastname = person.get('lastname')
                contact.address = person.get('address')
                contact.city = person.get('city')
                if person.get('country_code', None):
                    if person['country_code'].get('label', None):
                        contact.country = CountryCode.objects.get(label=person['country_code']['label'])
                    else:
                        contact.country = CountryCode.objects.get(description=person['country_code']['description'])
                contact.zip = person.get('zip')
                contact.telephone = person.get('telephone')
                contact.email = person.get('email')
                if person.get('affiliation', None):
                    contact.affiliation = self.get_instituion_from_db(person['affiliation'])

                contact.save()

        for item in fields.get('public_contact', []):
            if item.get('pid', None):
                try:
                    contact = Contact.objects.get(pk=item['pid'])
                except Contact.DoesNotExist:
                    contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            else:
                contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            PublicContact.objects.get_or_create(trial=ct, contact=contact)

        for item in fields.get('scientific_contact', []):
            if item.get('pid', None):
                try:
                    contact = Contact.objects.get(pk=item['pid'])
                except Contact.DoesNotExist:
                    contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            else:
                contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            ScientificContact.objects.get_or_create(trial=ct, contact=contact)

        for item in fields.get('site_contact', []):
            if item.get('pid', None):
                try:
                    contact = Contact.objects.get(pk=item['pid'])
                except Contact.DoesNotExist:
                    contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            else:
                contact = Contact.objects.filter(email=person['email'], creator=self.creator)[0]
            SiteContact.objects.get_or_create(trial=ct, contact=contact)

        TrialNumber.objects.get_or_create(
                    trial=ct,
                    issuing_authority=fields.get('reg_name', ''),
                    id_number=fields['trial_id'],
                    )

        for item in fields.get('secondary_ids', []):
            TrialNumber.objects.get_or_create(
                    trial=ct,
                    issuing_authority=item.get('issuing_authority', ''),
                    id_number=item['sec_id'],
                    )

        for item in fields.get('secondary_sponsors', []):
            inst = self.get_instituion_from_db(item)
            TrialSecondarySponsor.objects.get_or_create(trial=ct, institution=inst)

        for item in fields.get('source_support', []):
            inst = self.get_instituion_from_db(item)
            TrialSupportSource.objects.get_or_create(trial=ct, institution=inst)

        #TODO! This try-except shoud be refactored, its is used in several places
        for item in fields.get('primary_outcomes', []):
            outcome, new = Outcome.objects.get_or_create(trial=ct, interest='primary', description=item['value'])
            for trans in item.get('translations', []):
                try:
                    trans = outcome.translations.get(language=trans['lang'], description=trans['value'])
                except outcome.translations.model.DoesNotExist:
                    trans = outcome.translations.model(language=trans['lang'], description=trans['value'])
                    trans.content_object = outcome
                trans.save()

        for item in fields.get('secondary_outcomes', []):
            outcome, new = Outcome.objects.get_or_create(trial=ct, interest='secondary', description=item['value'])
            for trans in item.get('translations', []):
                try:
                    trans = outcome.translations.get(language=trans['lang'], description=trans['value'])
                except outcome.translations.model.DoesNotExist:
                    trans = outcome.translations.model(language=trans['lang'], description=trans['value'])
                    trans.content_object = outcome
                trans.save()

        for item in fields.get('hc_codes', []):
            descriptor, new = Descriptor.objects.get_or_create(
                    trial=ct,
                    aspect='HealthCondition',
                    level='general',
                    vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME
                    code=item['code'],
                    defaults={
                        'version': item.get('version', ''),
                        'text': item.get('value', ''),
                        }
                    )
            for trans in item.get('translations', []):
                trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True)
                trans_obj.text=trans['value']
                trans_obj.save()

        for item in fields.get('hc_keywords', []):
            descriptor, new = Descriptor.objects.get_or_create(
                    trial=ct,
                    aspect='HealthCondition',
                    level='specific',
                    vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME
                    code=item['code'],
                    defaults={
                        'version': item.get('version', ''),
                        'text': item.get('value', ''),
                        }
                    )
            for trans in item.get('translations', []):
                trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True)
                trans_obj.text=trans['value']
                trans_obj.save()

        for item in fields.get('i_codes', []):
            i_code, new = InterventionCode.objects.get_or_create(label=item['value'])
            ct.i_code.add(i_code)

        for item in fields.get('i_keywords', []):
            descriptor, new = Descriptor.objects.get_or_create(
                    trial=ct,
                    aspect='Intervention',
                    level='general',
                    vocabulary=_vocabularies[item.get('vocabulary', 'decs')], # FIXME
                    code=item['code'],
                    defaults={
                        'version': item.get('version', ''),
                        'text': item.get('value', ''),
                        }
                    )
            for trans in item.get('translations', []):
                trans_obj = descriptor.translations.get_translation_for_object(trans['lang'], descriptor, create_if_not_exist=True)
                trans_obj.text=trans['value']
                trans_obj.save()
Exemplo n.º 4
0
def deserialize_contact(data, persistent=False):
    """
    Transforms data (as JSON string or a dict) to a contact object
    """
    if not data:
        return None

    if not persistent:
        pass  # TODO if necessary

    else:
        from repository.models import Contact
        from vocabulary.models import CountryCode

        if isinstance(data, basestring):
            data = DictKeyAttribute(simplejson.loads(data))
        elif isinstance(data, dict):
            data = DictKeyAttribute(data)

        try:
            obj = Contact.objects.get(pk=data.pk)
        except Contact.DoesNotExist:
            obj = Contact()
            obj.id = data.pk
            obj.firstname = data.firstname
            obj.middlename = data.middlename
            obj.lastname = data.lastname
            obj.email = data.email
            obj.affiliation = deserialize_institution(data.affiliation, True)
            obj.address = data.address
            obj.city = data.city
            obj.country = deserialize_vocabulary(data.country, True,
                                                 CountryCode)
            obj.zip = data.zip
            obj.telephone = data.telephone
            obj.creator = deserialize_user(data.creator, True)
            obj.save()

        return obj