|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +# Reads in column containing taxon id and adds column containing the superkingdom of |
| 4 | +# that taxon id. |
| 5 | + |
| 6 | +# Usage: |
| 7 | +# perl add_column_with_superkingdom_of_taxon_id.pl [table] |
| 8 | +# [title of column containing taxon ids] [nodes.dmp file from NCBI] |
| 9 | + |
| 10 | +# Prints to console. To print to file, use |
| 11 | +# perl add_column_with_superkingdom_of_taxon_id.pl [table] |
| 12 | +# [title of column containing taxon ids] [nodes.dmp file from NCBI] > [output table path] |
| 13 | + |
| 14 | + |
| 15 | +use strict; |
| 16 | +use warnings; |
| 17 | + |
| 18 | + |
| 19 | +my $table = $ARGV[0]; |
| 20 | +my $taxon_id_column_title = $ARGV[1]; |
| 21 | +my $nodes_file = $ARGV[2]; # nodes.dmp file from NCBI: ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz |
| 22 | + |
| 23 | + |
| 24 | +my $NO_DATA = "NA"; |
| 25 | +my $NEWLINE = "\n"; |
| 26 | +my $DELIMITER = "\t"; |
| 27 | +my $TAXONDUMP_DELIMITER = "\t[|]\t"; # nodes.dmp and names.dmp |
| 28 | + |
| 29 | +my $SUPERKINGDOM_COLUMN_TITLE = "superkingdom"; # column to add |
| 30 | + |
| 31 | +# superkingdoms |
| 32 | +my %TAXON_ID_TO_SUPERKINGDOM = ( |
| 33 | + 2157 => "Archaea", |
| 34 | + 2 => "Bacteria", |
| 35 | + 2759 => "Eukaryota", |
| 36 | + 10239 => "Viruses"); |
| 37 | +my $ROOT_TAXON_ID = 1; |
| 38 | + |
| 39 | +# nodes.dmp and names.dmp |
| 40 | +my $TAXONID_COLUMN = 0; # both |
| 41 | +my $PARENTID_COLUMN = 1; # nodes.dmp |
| 42 | +my $RANK_COLUMN = 2; # nodes.dmp |
| 43 | +my $NAMES_COLUMN = 1; # names.dmp |
| 44 | +my $NAME_TYPE_COLUMN = 3; # names.dmp |
| 45 | + |
| 46 | + |
| 47 | +# verifies that all input files exist and are non-empty |
| 48 | +if(!$nodes_file or !-e $nodes_file or -z $nodes_file) |
| 49 | +{ |
| 50 | + print STDERR "Error: nodes.dmp file not provided, does not exist, or empty:\n\t" |
| 51 | + .$nodes_file."\nExiting.\n"; |
| 52 | + die; |
| 53 | +} |
| 54 | +if(!$table or !-e $table or -z $table) |
| 55 | +{ |
| 56 | + print STDERR "Error: input table not provided, does not exist, or is empty:\n\t" |
| 57 | + .$table."\nExiting.\n"; |
| 58 | + die; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +# reads in nodes file |
| 63 | +my %taxonid_to_parent = (); # key: taxon id -> value: taxon id of parent taxon |
| 64 | +my %taxonid_to_rank = (); # key: taxon id -> value: rank of taxon |
| 65 | +open NODES_FILE, "<$nodes_file" || die "Could not open $nodes_file to read\n"; |
| 66 | +while(<NODES_FILE>) |
| 67 | +{ |
| 68 | + chomp; |
| 69 | + if($_ =~ /\S/) |
| 70 | + { |
| 71 | + my @items = split($TAXONDUMP_DELIMITER, $_); |
| 72 | + my $taxonid = $items[$TAXONID_COLUMN]; |
| 73 | + my $parent_taxonid = $items[$PARENTID_COLUMN]; |
| 74 | + my $rank = $items[$RANK_COLUMN]; |
| 75 | + |
| 76 | + $taxonid_to_parent{$taxonid} = $parent_taxonid; |
| 77 | + $taxonid_to_rank{$taxonid} = $rank; |
| 78 | + } |
| 79 | +} |
| 80 | +close NODES_FILE; |
| 81 | + |
| 82 | + |
| 83 | +# reads in taxon id column of table and adds superkingdom column |
| 84 | +my $first_line = 1; |
| 85 | +my $taxon_id_column = -1; |
| 86 | +open TABLE, "<$table" || die "Could not open $table to read; terminating =(\n"; |
| 87 | +while(<TABLE>) # for each row in the file |
| 88 | +{ |
| 89 | + chomp; |
| 90 | + my $line = $_; |
| 91 | + if($line =~ /\S/) # if row not empty |
| 92 | + { |
| 93 | + my @items_in_line = split($DELIMITER, $line, -1); |
| 94 | + if($first_line) # column titles |
| 95 | + { |
| 96 | + # identifies column to merge by and columns to save |
| 97 | + my $column = 0; |
| 98 | + foreach my $column_title(@items_in_line) |
| 99 | + { |
| 100 | + if(defined $column_title and $column_title eq $taxon_id_column_title) |
| 101 | + { |
| 102 | + if($taxon_id_column != -1) |
| 103 | + { |
| 104 | + print STDERR "Warning: column title ".$taxon_id_column_title |
| 105 | + ." appears more than once in input table:\n\t".$table."\n"; |
| 106 | + } |
| 107 | + $taxon_id_column = $column; |
| 108 | + } |
| 109 | + $column++; |
| 110 | + } |
| 111 | + |
| 112 | + # verifies that we have found column to merge by |
| 113 | + if($taxon_id_column == -1) |
| 114 | + { |
| 115 | + print STDERR "Warning: column title ".$taxon_id_column_title |
| 116 | + ." not found in input table:\n\t".$table."\nExiting.\n"; |
| 117 | + die; |
| 118 | + } |
| 119 | + $first_line = 0; # next line is not column titles |
| 120 | + |
| 121 | + # prints line as is |
| 122 | + print $line; |
| 123 | + |
| 124 | + # prints titles of new superkingdom column |
| 125 | + print $DELIMITER.$SUPERKINGDOM_COLUMN_TITLE.$NEWLINE; |
| 126 | + } |
| 127 | + else # column values (not column titles) |
| 128 | + { |
| 129 | + # retrieves taxon id |
| 130 | + my $taxon_id = $items_in_line[$taxon_id_column]; |
| 131 | + |
| 132 | + # retrieves superkingdom |
| 133 | + my $superkingdom = $NO_DATA; |
| 134 | + my $ancestor_taxon_id = $taxon_id; |
| 135 | + while($superkingdom eq $NO_DATA |
| 136 | + and $taxonid_to_parent{$ancestor_taxon_id} |
| 137 | + and $ancestor_taxon_id ne $taxonid_to_parent{$ancestor_taxon_id} |
| 138 | + and $ancestor_taxon_id ne $ROOT_TAXON_ID) |
| 139 | + { |
| 140 | + if($TAXON_ID_TO_SUPERKINGDOM{$ancestor_taxon_id}) |
| 141 | + { |
| 142 | + $superkingdom = $TAXON_ID_TO_SUPERKINGDOM{$ancestor_taxon_id}; |
| 143 | + } |
| 144 | + $ancestor_taxon_id = $taxonid_to_parent{$ancestor_taxon_id} |
| 145 | + } |
| 146 | + |
| 147 | + # prints line as is |
| 148 | + print $line; |
| 149 | + |
| 150 | + # prints superkingdom column value |
| 151 | + print $DELIMITER.$superkingdom.$NEWLINE; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | +close TABLE; |
| 156 | + |
| 157 | + |
| 158 | +# April 4, 2023 |
0 commit comments