def test_export_individuals(self):
        test_individuals = Individual.objects.filter(individual_id='NA19675_1')

        # test tsv with all columns
        response = export_individuals(
            'test_individuals_table',
            test_individuals,
            'tsv',
            include_project_name=True,
            include_case_review_status=True,
            include_case_review_last_modified_date=True,
            include_case_review_last_modified_by=True,
            include_case_review_discussion=True,
            include_hpo_terms_present=True,
            include_hpo_terms_absent=True,
            include_paternal_ancestry=True,
            include_maternal_ancestry=True,
            include_age_of_onset=True,
        )
        self.assertEqual(response.status_code, 200)
        rows = [
            row.split('\t')
            for row in response.content.rstrip('\n').split('\n')
        ]

        self.assertEqual(rows[0][0], 'Project')
        self.assertEqual(rows[0][1], 'Family ID')
        self.assertEqual(len(rows), 2)

        # test Excel format
        response = export_individuals(
            'test_families_table',
            test_individuals,
            'xls',
            include_project_name=True,
            include_case_review_status=True,
            include_case_review_last_modified_date=True,
            include_case_review_last_modified_by=True,
            include_case_review_discussion=True,
            include_hpo_terms_present=True,
            include_hpo_terms_absent=True,
            include_paternal_ancestry=True,
            include_maternal_ancestry=True,
            include_age_of_onset=True)
        self.assertEqual(response.status_code, 200)
        load_workbook(StringIO(response.content))

        # test unknown format
        self.assertRaisesRegexp(
            ValueError, '.*format.*',
            lambda: export_individuals('test_families_table',
                                       test_individuals,
                                       file_format='unknown_format'))
Beispiel #2
0
def export_project_individuals_handler(request, project_guid):
    """Export project Individuals table.

    Args:
        project_guid (string): GUID of the project for which to export individual data
    """

    file_format = request.GET.get('file_format', 'tsv')
    include_phenotypes = bool(request.GET.get('include_phenotypes'))

    project = get_project_and_check_permissions(project_guid, request.user)

    # get all individuals in this project
    individuals = Individual.objects.filter(family__project=project).order_by(
        'family__family_id', 'affected')

    filename_prefix = "%s_individuals" % _slugify(project.name)

    return export_individuals(
        filename_prefix,
        individuals,
        file_format,
        include_hpo_terms_present=include_phenotypes,
        include_hpo_terms_absent=include_phenotypes,
    )
Beispiel #3
0
    def handle(self, *args, **options):
        project_names = options.get('projects')
        if options.get('project_category'):
            project_names += [
                p.name for p in ProjectCategory.objects.get(
                    name=options['project_category']).projects.all()
            ]

        if not len(project_names):
            raise Exception('Error: No projects specified')
        print('Exporting individuals from {} projects'.format(
            len(project_names)))

        individuals = Individual.objects.filter(
            family__project__name__in=project_names).all()
        print('Found {} individuals to export'.format(len(individuals)))
        response = export_individuals(
            'projects_individuals',
            individuals,
            'xls',
            include_project_name=True,
            include_project_created_date=True,
            include_created_date=True,
            include_analysis_status=True,
            include_coded_phenotype=True,
            include_hpo_terms_present=True,
            include_hpo_terms_absent=True,
            include_first_loaded_date=True,
        )
        print('Parsed individual data')
        filename = re.search(
            'filename="(?P<file_name>.*)"',
            response.get('Content-Disposition')).group('file_name')
        with open(filename, 'w') as f:
            f.write(response.content)
        print('Wrote results to {}'.format(filename))