<commit_before># Copyright 2013 European Molecular Biology Laboratory - European Bioinformatics Institute
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
package EpiRR::Service::IhecBinaryDatasetClassifier;

use Moose;

with 'EpiRR::Roles::DatasetClassifier';

has '+status_names' => ( default => sub { [ 'Complete', 'Incomplete' ] }, );
has '+type_names' =>
  ( default => sub { [ 'Composite', 'Pooled samples', 'Single donor' ] }, );

has 'required_experiment_types' => (
    is      => 'rw',
    isa     => 'ArrayRef[ArrayRef[Str]]',
    traits  => ['Array'],
    default => sub {
        [
            ['DNA Methylation'],
            ['ChIP-Seq Input'],
            ['Histone H3K4me1'],
            ['Histone H3K4me3'],
            ['Histone H3K9me3'],
            ['Histone H3K9ac'],
            ['Histone H3K27me3'],
            ['Histone H3K36me3'],
            ['mRNA-Seq'],
        ];
    },
    handles => { 'all_required_experiment_types' => 'elements' }
);

sub determine_classification {
    my ( $self, $dataset, $samples, $errors ) = @_;

    my $completeness = $self->experimental_completeness( $dataset, $errors );
    my $composition = $self->composition( $samples, $errors );

    return ( $completeness, $composition );
}

sub composition {
    my ( $self, $samples, $errors ) = @_;
    my $dataset_type;
    my %donors;
    my %pools;
    confess("No samples") unless @$samples;
    for my $s (@$samples) {
        if ( $s->get_meta_data('pool_id') ) {
            $pools{ $s->get_meta_data('pool_id') }++;
        }
        elsif ( $s->get_meta_data('donor_id') || $s->get_meta_data('line') ) {
            $donors{ $s->get_meta_data('donor_id')
                  || $s->get_meta_data('line') }++;
        }
        else {
            push @$errors,
              'No donor_id/pool_id/line found for sample ' . $s->sample_id();
        }
    }

    if ( scalar( keys %pools ) + scalar( keys %donors ) > 1 ) {
        $dataset_type = 'Composite';
    }
    elsif (%pools) {
        $dataset_type = 'Pooled samples';
    }
    elsif (%donors) {
        $dataset_type = 'Single donor';
    }
    else {
        push @$errors, 'No donor/line/pool information for dataset';
    }

    return $dataset_type;
}

sub experimental_completeness {
    my ( $self, $dataset, $errors ) = @_;
    my %et;

    for my $rd ( $dataset->all_raw_data() ) {
        if ( $rd->experiment_type() ) {
            $et{ $rd->experiment_type() }++;
        }
        else {
            push @$errors, 'No experiment type for ' . $rd->primary_id();
        }
    }

    my $classification = 'Complete';

    for my $ret ( $self->all_required_experiment_types() ) {
        if ( !exists $et{$ret} ) {
            $classification = 'Incomplete';
        }
    }

    return $classification;
}

__PACKAGE__->meta->make_immutable;
1;
<commit_msg>Allow 'one of x' options for required experiment type<commit_after># Copyright 2013 European Molecular Biology Laboratory - European Bioinformatics Institute
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
package EpiRR::Service::IhecBinaryDatasetClassifier;

use Moose;

with 'EpiRR::Roles::DatasetClassifier';

has '+status_names' => ( default => sub { [ 'Complete', 'Incomplete' ] }, );
has '+type_names' =>
  ( default => sub { [ 'Composite', 'Pooled samples', 'Single donor' ] }, );

has 'required_experiment_types' => (
    is      => 'rw',
    isa     => 'ArrayRef[ArrayRef[Str]]',
    traits  => ['Array'],
    default => sub {
        [
            ['DNA Methylation'],  ['ChIP-Seq Input'],
            ['Histone H3K4me1'],  ['Histone H3K4me3'],
            ['Histone H3K9me3'],  ['Histone H3K9ac'],
            ['Histone H3K27me3'], ['Histone H3K36me3'],
            ['mRNA-Seq'],
        ];
    },
    handles => { 'all_required_experiment_types' => 'elements' }
);

sub determine_classification {
    my ( $self, $dataset, $samples, $errors ) = @_;

    my $completeness = $self->experimental_completeness( $dataset, $errors );
    my $composition = $self->composition( $samples, $errors );

    return ( $completeness, $composition );
}

sub composition {
    my ( $self, $samples, $errors ) = @_;
    my $dataset_type;
    my %donors;
    my %pools;
    confess("No samples") unless @$samples;
    for my $s (@$samples) {
        if ( $s->get_meta_data('pool_id') ) {
            $pools{ $s->get_meta_data('pool_id') }++;
        }
        elsif ( $s->get_meta_data('donor_id') || $s->get_meta_data('line') ) {
            $donors{ $s->get_meta_data('donor_id')
                  || $s->get_meta_data('line') }++;
        }
        else {
            push @$errors,
              'No donor_id/pool_id/line found for sample ' . $s->sample_id();
        }
    }

    if ( scalar( keys %pools ) + scalar( keys %donors ) > 1 ) {
        $dataset_type = 'Composite';
    }
    elsif (%pools) {
        $dataset_type = 'Pooled samples';
    }
    elsif (%donors) {
        $dataset_type = 'Single donor';
    }
    else {
        push @$errors, 'No donor/line/pool information for dataset';
    }

    return $dataset_type;
}

sub experimental_completeness {
    my ( $self, $dataset, $errors ) = @_;
    my %et;

    for my $rd ( $dataset->all_raw_data() ) {
        if ( $rd->experiment_type() ) {
            $et{ $rd->experiment_type() }++;
        }
        else {
            push @$errors, 'No experiment type for ' . $rd->primary_id();
        }
    }

    my $classification = 'Complete';

    for my $rets ( $self->all_required_experiment_types() ) {
        my $found = 0;
        for my $ret (@$rets) {
            $found++ if ( exists $et{$ret} );
        }
        if ( !$found ) {
            $classification = 'Incomplete';
        }
    }

    return $classification;
}

__PACKAGE__->meta->make_immutable;
1;

{'fraction_non_alphanumeric': 0.14691176470588235, 'fraction_numerical': 0.008676470588235294, 'mean_word_length': 2.4752171691364335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2133699', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2149, 'n_words': 554}