perlbioinformaticsredundancyqiime

eliminate redundant in a list with perl


Hi I made this script to extract all phylum from a OTUs file obtained from qiime using silva database, I added a subroutine to eliminate duplicate taxon and extract all no redundant (one of each taxon), my problem is that I just get the las line (only one taxa)

#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my ($imput, $output, $line, $phylum, @taxon_list, @final_list);
GetOptions (
           'in=s'    =>\$imput,
           'ou=s'   =>\$output,
           'k'      =>\$phylum,

           );

if (!$imput or !$output){
    exit;
    print "Error";
}



#SUBRUTINE TO ELIMINATE DUPLICATES
# --------------------------------------------------------------------------------------------
sub uniq {
  my %seen;
  grep !$seen{$_}++, @_;
}
# --------------------------------------------------------------------------------------------

open INPUTFILE, "<", "$imput", or die "can`t open file\n";
open OUTPUTFILE, ">", "$output" or die "can`t creat file\n";

while (<INPUTFILE>){
$line=$_;
chomp($line);
    if ($line=~ m/^#/g){
        next;
    }
    elsif($phylum){
        my @kingd=($line=~m/D_1__(.*);D_2/g);
        foreach (@kingd){
            if ($_=~/^$/){
                next;
            }
            elsif ($_=~ m/^[Uu]nknown/g){
                next;
                }
            elsif ($_=~ m/^[Uu]ncultured$/g){
                next;
            }
            elsif ($_=~ m/^[Uu]nidentified$/g){

            }
            else {
                @taxon_list =$_;

                    @final_list = uniq @taxon_list;
            }
        }
    }
}

print OUTPUTFILE "@final_list\n";

close INPUTFILE;
close OUTPUTFILE;
exit;

Solution

  • I suspect issue is with:

    @taxon_list =$_;
    

    It is not appended to, but instead overwritten with the current element.

    Try:

    push @taxon_list, $_;
    

    You can also move the following outside the loop:

    @final_list = uniq @taxon_list;