<commit_before>require "subfuncs.pl";

use File::Basename;
use Getopt::Long;
use Pod::Usage;

my ($inputfile, $labelfile, $outfile, $simplename) = 0;
GetOptions ('input|samples=s' => \$inputfile,
            'labels|names=s' => \$labelfile,
            'simple' => \$simplename,
			'outfile=s' => \$outfile) or pod2usage(-msg => "GetOptions failed.", -exitval => 2);

unless ($inputfile && $labelfile) {
    pod2usage(-msg => "Error: an option was misspecified\n", -exitval => 2);
}


open FH, "<", $inputfile;
my @input_lines = <FH>;
close FH;

my $out_fh;
if ($outfile) {
	print "opening $outfile...\n";
	open $out_fh, ">", $outfile or die "couldn't open $outfile\n";
} else {
	$out_fh = STDOUT;
}

my $labels;
$labels = make_label_lookup ($labelfile);
my $i=0;
foreach my $line (@input_lines) {
	if ($line =~ /([\(\,]+)(.*?)([\:\,\)])(.*)/) {
		# we're in a newick tree
		my $templine = $line;
		$line = "";
		while ($templine =~ /^(.*?[\(\,]+)(.*?)([\:\,\)])(.*)/) {
			my ($head, $key, $tail, $remainder) = ($1, $2, $3, $4);
			my $label = $key;
			if (exists $labels->{$key}) {
				$label = $labels->{$key};
			}
			$line .= "$head$label$tail";
			$templine = $remainder;
		}
		$line .= "$templine\n";
		$templine = $line;
		$line = "";
		while ($templine =~ /^(.+?[\(\,]+.*?\,)(.*?)([\:\)])(.*)/) {
			my ($head, $key, $tail, $remainder) = ($1, $2, $3, $4);
			if ($key =~ /(\(.+\,)(.+)/) {
				$head = $head . $1;
				$key = $2;
			}
			my $label = $key;
			if (exists $labels->{$key}) {
				$label = $labels->{$key};
			}
			$line .= "$head$label$tail";
			$templine = $remainder;
		}
		$line .= "$templine\n";
	} elsif ($line =~ /^>(.+?)(\s.*|$)/) {
		# it is a fasta-type line
		my $key = $1;
		my $remainder = $2;
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		if ($simplename) {
			$remainder = "";
		}
		$line = ">$label$remainder\n";
	} elsif ($line =~ /^(\s*\d+\s+)(.+?)(,*)$/) {
		# it is a TRANSLATE-type line (numbered list of taxa)
		print "translate $line";
		my $beginning = $1;
		my $key = $2;
		my $remainder = $3;
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		$line = "$beginning$label$remainder\n";
	} elsif ($line =~ /^(.+?)(\s+.+)$/) {
		# it is a generic NEXUS/phylip-type line
		my $key = $1;
		my $remainder = $2;
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		$line = "$label$remainder\n";
	}
 	print $out_fh "$line";
 	$i++;
}

close $out_fh;

__END__

=head1 NAME

slice_fasta_file

=head1 SYNOPSIS

relabel_samples -input input_file -labels label_file [-outfile output_file]<commit_msg>Added ability to relabel based on unique DNA identifier code<commit_after>require "subfuncs.pl";

use File::Basename;
use Getopt::Long;
use Pod::Usage;

my ($inputfile, $labelfile, $outfile, $simplename, $dnacode) = 0;
GetOptions ('input|samples=s' => \$inputfile,
            'labels|names=s' => \$labelfile,
            'simple' => \$simplename,
            'dna|code' => \$dnacode,
			'outfile=s' => \$outfile) or pod2usage(-msg => "GetOptions failed.", -exitval => 2);

unless ($inputfile && $labelfile) {
    pod2usage(-msg => "Error: an option was misspecified\n", -exitval => 2);
}


open FH, "<", $inputfile;
my @input_lines = <FH>;
close FH;

my $out_fh;
if ($outfile) {
	print "opening $outfile...\n";
	open $out_fh, ">", $outfile or die "couldn't open $outfile\n";
} else {
	$out_fh = STDOUT;
}

my $labels;
$labels = make_label_lookup ($labelfile);
my $i=0;
foreach my $line (@input_lines) {
	if ($line =~ /([\(\,]+)(.*?)([\:\,\)])(.*)/) {
		# we're in a newick tree
		my $templine = $line;
		$line = "";
		while ($templine =~ /^(.*?[\(\,]+)(.*?)([\:\,\)])(.*)/) {
			my ($head, $key, $tail, $remainder) = ($1, $2, $3, $4);
			$key = get_key($key, $dnacode);
			my $label = $key;
			if (exists $labels->{$key}) {
				$label = $labels->{$key};
			}
			$line .= "$head$label$tail";
			$templine = $remainder;
		}
		$line .= "$templine\n";
		$templine = $line;
		$line = "";
		while ($templine =~ /^(.+?[\(\,]+.*?\,)(.*?)([\:\)])(.*)/) {
			my ($head, $key, $tail, $remainder) = ($1, $2, $3, $4);
			if ($key =~ /(\(.+\,)(.+)/) {
				$head = $head . $1;
				$key = $2;
			}
			$key = get_key($key, $dnacode);
			my $label = $key;
			if (exists $labels->{$key}) {
				$label = $labels->{$key};
			}
			$line .= "$head$label$tail";
			$templine = $remainder;
		}
		$line .= "$templine\n";
	} elsif ($line =~ /^>(.+?)(\s.*|$)/) {
		# it is a fasta-type line
		my ($key, $remainder) = ($1,$2);
		$key = get_key($key, $dnacode);
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		if ($simplename) {
			$remainder = "";
		}
		$line = ">$label$remainder\n";
	} elsif ($line =~ /^(\s*\d+\s+)(.+?)(,*)$/) {
		# it is a TRANSLATE-type line (numbered list of taxa)
		print "translate $line";
		my ($beginning, $key, $remainder) = ($1,$2,$3);
		$key = get_key($key, $dnacode);
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		$line = "$beginning$label$remainder\n";
	} elsif ($line =~ /^(.+?)(\s+.+)$/) {
		# it is a generic NEXUS/phylip-type line
		my ($key, $remainder) = ($1,$2);
		$key = get_key($key, $dnacode);
		my $label = $key;
		if (exists $labels->{$key}) {
			$label = $labels->{$key};
		}
		chomp $remainder;
		$line = "$label$remainder\n";
	}
 	print $out_fh "$line";
 	$i++;
}

close $out_fh;

sub get_key {
	my $key = shift;
	my $dnacode = shift;

	if ($dnacode) {
		$key =~ s/.*DNA(\d+).*/$1/;
	}

	return $key;
}
__END__

=head1 NAME

slice_fasta_file

=head1 SYNOPSIS

relabel_samples -input input_file -labels label_file [-outfile output_file]
{'fraction_non_alphanumeric': 0.2595221606648199, 'fraction_numerical': 0.008829639889196677, 'mean_word_length': 3.1412186379928317, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3601158', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2459, 'n_words': 550}