<commit_before>use CatalystX::GlobalContext qw( $c );
#!/usr/bin/perl -wT

=head1 DESCRIPTION
A script for downloading population 
phenotype raw data in tab delimited format.

=head1 AUTHOR(S)

<NAME> (<EMAIL>)

=cut

use strict;

use CXGN::DB::Connection;
use CXGN::Phenome::Population;
use CXGN::Scrap;
use Cache::File;
use CGI ();
use CatalystX::GlobalContext qw( $c );

my $scrap = CXGN::Scrap->new();
my $dbh   = CXGN::DB::Connection->new();

my %args = $scrap->get_all_encoded_arguments();
my $population_id = $args{population_id};

my $pop = CXGN::Phenome::Population->new( $dbh, $population_id );
my $name = $pop->get_name();


my $p_file = $pop->phenotype_file($c);

my $cgi = CGI->new();

if (-e $p_file) {   
   
    print $cgi->header(
	-type => 'application/x-download',
	-attachment => "phenotype_data_${population_id}.txt",
	);

    print "phenotype data for $name\n\n\n";
 
    open my $f, "<$p_file" or die "can't open file $p_file: $!\n";
  
    while (my $row=<$f>) {
	$row =~ s/,/\t/g;
	print "$row";
    }

}
else {
    print $cgi->header ('text/plain');   
    print "No Phenotype data file found for this population";
}

<commit_msg>some refactoring..<commit_after>
=head1 DESCRIPTION
A script for downloading population 
phenotype raw data in a tab delimited format.

=head1 AUTHOR(S)

<NAME> (<EMAIL>)

=cut

use strict;

use CXGN::DB::Connection;
use CXGN::Phenome::Population;
use Cache::File;
use CGI;

use CatalystX::GlobalContext qw( $c );


my $cgi = CGI->new();
my $population_id = $cgi->param('population_id'); 

my $dbh   = CXGN::DB::Connection->new();

my $pop = CXGN::Phenome::Population->new( $dbh, $population_id );
my $name = $pop->get_name();


my $p_file = $pop->phenotype_file($c);



if (-e $p_file) {   
   
    print $cgi->header(
	-type => 'application/x-download',
	-attachment => "phenotype_data_${population_id}.txt",
	);

    print "phenotype data for $name\n\n\n";
 
    open my $f, "<$p_file" or die "can't open file $p_file: $!\n";
  
    while (my $row=<$f>) {
	$row =~ s/,/\t/g;
	print "$row";
    }

}
else {
    print $cgi->header ('text/plain');   
    print "No Phenotype data file found for this population";
}


{'fraction_non_alphanumeric': 0.1832412523020258, 'fraction_numerical': 0.001841620626151013, 'mean_word_length': 3.8721973094170403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4875834', 'n_tokens_mistral': 897, 'n_tokens_neox': 828, 'n_words': 213}