Convert-Pheno

 view release on metacpan or  search on metacpan

lib/Convert/Pheno/CSV.pm  view on Meta::CPAN

        next unless ref $row->{$key};
        my $type = ref $row->{$key};

        if ( $type =~ /::Boolean$/ ) {
            $row->{$key} = $row->{$key} ? 'true' : 'false';
            next;
        }

        my $encoded = eval { $JSON->encode( $row->{$key} ) };
        $row->{$key} = defined $encoded ? $encoded : q{};
    }

    return $row;
}

###############
###############
#  CSV2BFF    #
###############
###############

sub do_csv2bff {
    my ( $self, $participant ) = @_;
    my $data_mapping_file = $self->{data_mapping_file};

    ####################################
    # START MAPPING TO BEACON V2 TERMS #
    ####################################

    # $participant =
    #       {
    #         'abdominal_mass' => 'No',
    #         'abdominal_pain' => 'Yes',
    #         'age' => 25,
    #         'age_first_diagnosis' => 24
    #          ...
    #        }

    print Dumper $participant
      if ( defined $self->{debug} && $self->{debug} > 4 );

    # Data structure (hashref) for each individual
    my $individual = {};
    my $record = Convert::Pheno::Tabular::Record->new(
        {
            source => $data_mapping_file->{project}{source},
            raw    => $participant,
        }
    );

    # Intialize parameters for most subs
    my $param_sub = {
        source               => $data_mapping_file->{project}{source},
        project_id           => $data_mapping_file->{project}{id},
        project_ontology     => $data_mapping_file->{project}{ontology},
        data_mapping_file    => $data_mapping_file,
        participant          => $participant,
        record               => $record,
        self                 => $self,
        individual           => $individual,
        term_mapping_cursor  => undef,
        participant_id_field => undef,
        participant_id       => undef
    };

    $param_sub->{lock_keys} = [ 'lock_keys', keys %$param_sub ];
    lock_keys %$param_sub, @{ $param_sub->{lock_keys} };

    # *** ABOUT REQUIRED PROPERTIES ***
    # 'id' and 'sex' are required properties in <individuals> entry type
    my ( $sex_field, $id_field ) = get_required_terms($param_sub);

    # Now propagate fields according to user selection
    propagate_fields( $id_field, $param_sub );

    # Premature return (undef) if fields are not defined or present
    return
      unless ( defined $participant->{$id_field}
        && $participant->{$sex_field} );

    # NB: We don't need to initialize terms (unless required)
    # e.g.,
    # $individual->{diseases} = undef;
    #  or
    # $individual->{diseases} = []
    # Otherwise the validator may complain about being empty

    # ========
    # diseases
    # ========

    map_diseases($param_sub);

    # =========
    # ethnicity
    # =========

    map_ethnicity($param_sub);

    # =========
    # exposures
    # =========

    map_exposures($param_sub);

    # ================
    # geographicOrigin
    # ================

    #$individual->{geographicOrigin} = {};

    # ==
    # id
    # ==

    # Concatenation of the values in @id_fields (mapping file)
    $individual->{id} = join ':',
      map { $participant->{$_} // 'NA' } @{ $data_mapping_file->{id}{fields} };

    # ====
    # info



( run in 0.686 second using v1.01-cache-2.11-cpan-39bf76dae61 )