<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::Parser::SRAXMLParser;

use strict;
use warnings;
use Carp;
use feature qw(switch say);

use Moose;
use namespace::autoclean;
use XML::Twig;

use EpiRR::Model::Sample;
use EpiRR::Model::RawData;

sub parse_experiment {
    my ( $self, $xml, $errors ) = @_;

    my ( $e_id, $s_id, $et, $ls );

    my $t = XML::Twig->new(
        twig_handlers => {
            'EXPERIMENT' => sub {
                my ( $t, $element ) = @_;
                my $id = $element->{'att'}->{'accession'};
                push @$errors,
                  "Found multiple experiments in XML ($e_id and $id)."
                  if $e_id;
                $e_id = $id;
            },
            'LIBRARY_STRATEGY' => sub {
                my ( $t, $element ) = @_;
                  $ls = $element->trimmed_text();
            },
            'SAMPLE_DESCRIPTOR' => sub {
                my ( $t, $element ) = @_;
                my $sample = $element->{'att'}->{'accession'};
                push @$errors,
                  "Found multiple samples in XML ($s_id and $sample)"
                  if ($s_id);
                $s_id = $sample;
            },
            'EXPERIMENT_ATTRIBUTE' => sub {
                my ( $t, $element ) = @_;

                if ( $element->first_child_text('TAG') eq 'EXPERIMENT_TYPE' ) {
                    my $experiment_type = $element->first_child_text('VALUE');
                    push @$errors,
"Found multiple experiment types in XML ($et and $experiment_type)"
                      if ($et);

                    $et = $experiment_type;
                }
            },
        }
    );
    $t->parse($xml);
    if(!defined $et && defined $ls){
      $et = $ls;
    }
    if($et eq 'Bisulfite-Seq') {
      $et = 'DNA Methylation';
    }
    push @$errors, "No experiment found" unless $e_id;
    if ($e_id) {
        push @$errors, "No experiment_type found"  unless $et;
        push @$errors, "No sample found"           unless $s_id;
        push @$errors, "No library_strategy found" unless $ls;
    }

    return ( $s_id, $et, $e_id, $ls );
}

sub parse_sample {
    my ( $self, $xml, $errors ) = @_;

    my $s = EpiRR::Model::Sample->new();

    my $t = XML::Twig->new(
        twig_handlers => {
            'SAMPLE' => sub {
                my ( $t, $element ) = @_;
                my $id = $element->{'att'}->{'accession'};
                push @$errors, "Cannot handle multiple samples"
                  if ( $s->sample_id() );
                $s->sample_id($id);
            },
            'SAMPLE_ATTRIBUTE' => sub {
                my ( $t, $element ) = @_;

                my $tag   = ( $element->first_child_text('TAG') );
                my $value = $element->first_child_text('VALUE');
                $s->set_meta_data( $tag, $value );
            },
            'SCIENTIFIC_NAME' => sub {
                my ( $t, $element ) = @_;<commit_msg>function parser experiment changed<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::Parser::SRAXMLParser;

use strict;
use warnings;
use Carp;
use feature qw(switch say);

use Moose;
use namespace::autoclean;
use XML::Twig;
use EpiRR::Model::Experiment;
use EpiRR::Model::Sample;
use EpiRR::Model::RawData;

sub parse_experiment {
    my ( $self, $xml, $errors ) = @_;

    my $e = EpiRR::Model::Experiment -> new();

    my $t = XML::Twig->new(
        twig_handlers => {
            'EXPERIMENT' => sub {
                my ( $t, $element ) = @_;
                my $id = $element->{'att'}->{'accession'};
                push @$errors,
                  "Found multiple experiments in XML ($e and $id)."
                  if $e;
                $e->experiment_id ($id);
            },
            'LIBRARY_STRATEGY' => sub {
                my ( $t, $element ) = @_;
		my $value = $element _> text();
                  $e->set_meta_data ('library_strategy', $value);
            },
            'SAMPLE_DESCRIPTOR' => sub {
                my ( $t, $element ) = @_;
                my $value = $element->{'att'}->{'accession'};
                
                  $e->set_meta_data ('sample_descriptor', $value);
                 
             
            },
            'EXPERIMENT_ATTRIBUTE' => sub {
                my ( $t, $element ) = @_;
                
                my  $tag= ($element->first_child_text('TAG');{
		my  $value = ($value->first_child_text('VALUE');{
		
		   $e->set_meta_data($tag, $value);
             
                }
            },
        }
    );

    $t->parse($xml);
   
    }
    push @$errors, "No experiment found" unless $e;

    return $e;
}

sub parse_sample {
    my ( $self, $xml, $errors ) = @_;

    my $s = EpiRR::Model::Sample->new();

    my $t = XML::Twig->new(
        twig_handlers => {
            'SAMPLE' => sub {
                my ( $t, $element ) = @_;
                my $id = $element->{'att'}->{'accession'};
                push @$errors, "Cannot handle multiple samples"
                  if ( $s->sample_id() );
                $s->sample_id($id);
            },
            'SAMPLE_ATTRIBUTE' => sub {
                my ( $t, $element ) = @_;

                my $tag   = ( $element->first_child_text('TAG') );
                my $value = $element->first_child_text('VALUE');
                $s->set_meta_data( $tag, $value );
            },
            'SCIENTIFIC_NAME' => sub {
                my ( $t, $element ) = @_;
{'fraction_non_alphanumeric': 0.13991031390134528, 'fraction_numerical': 0.002391629297458894, 'mean_word_length': 1.7591752577319588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '3845763', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1985, 'n_words': 548}