« CLIPSeqTools/PlotApp/cluster_size_and_score_distribution.pm
↑ Summary
CLIPSeqTools/PlotApp/conservation_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::CompareApp::join_tables - Perform inner join on tab delimited | ||
| 4 | table files. Can be used to prepare DESeq input file from counts tables. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools-compare join_tables [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Perform inner join on tab delimited table files. | ||
| 13 | Joins two or more tables together based on key columns. The output table will | ||
| 14 | have the key columns used (must have the same name in all input tables) plus | ||
| 15 | one value column (must also have the same name in all input tables) from each | ||
| 16 | of the input tables. The value columns will be named based on the -name | ||
| 17 | option in the same order as the -table option. | ||
| 18 | |||
| 19 | eg. Given files with the following column structure | ||
| 20 | |||
| 21 | FileA: key_1 key_2 valuecol_1 valuecol_2 | ||
| 22 | FileB: key_1 key_2 valuecol_1 valuecol_2 | ||
| 23 | FileC: key_1 key_2 valuecol_1 valuecol_2 | ||
| 24 | |||
| 25 | if the following command is used | ||
| 26 | |||
| 27 | clipseqtools-compare join_tables \ | ||
| 28 | --table FileA --table FileB --table FileC \ | ||
| 29 | --key key_1 --key key_2 \ | ||
| 30 | --value valuecol_1 \ | ||
| 31 | --name FileA_values --name FileB_values --name FileC_values | ||
| 32 | |||
| 33 | the following output file is produced | ||
| 34 | |||
| 35 | key_1 key_2 FileA_values FileB_values FileC_values | ||
| 36 | |||
| 37 | |||
| 38 | =head1 OPTIONS | ||
| 39 | |||
| 40 | Input. | ||
| 41 | --table <Str> tab delimited file. The first line must have the | ||
| 42 | column names. Use multiple times to specify | ||
| 43 | multiple input tables. | ||
| 44 | --key <Str> name of column that has unique identifiers for each | ||
| 45 | row. Use multiple times to create a composite key. | ||
| 46 | --value <Str> name of column with the values that will be joined | ||
| 47 | in the output table. | ||
| 48 | --name <name> name of the value column in the output. Must be | ||
| 49 | given as many times as the -table option. | ||
| 50 | |||
| 51 | Output | ||
| 52 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 53 | extension to path. [Default: ./] | ||
| 54 | |||
| 55 | Other options. | ||
| 56 | -v --verbose print progress lines and extra information. | ||
| 57 | -h -? --usage --help print help message | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | package CLIPSeqTools::CompareApp::join_tables; | ||
| 62 | $CLIPSeqTools::CompareApp::join_tables::VERSION = '1.0.0'; | ||
| 63 | |||
| 64 | # Make it an app command | ||
| 65 | use MooseX::App::Command; | ||
| 66 | extends 'CLIPSeqTools::CompareApp'; | ||
| 67 | |||
| 68 | |||
| 69 | ####################################################################### | ||
| 70 | ####################### Load External modules ##################### | ||
| 71 | ####################################################################### | ||
| 72 | use Modern::Perl; | ||
| 73 | use autodie; | ||
| 74 | use namespace::autoclean; | ||
| 75 | |||
| 76 | |||
| 77 | ####################################################################### | ||
| 78 | ####################### Command line options ###################### | ||
| 79 | ####################################################################### | ||
| 80 | option 'table' => ( | ||
| 81 | is => 'rw', | ||
| 82 | isa => 'ArrayRef', | ||
| 83 | required => 1, | ||
| 84 | documentation => 'tsv file. Use multiple times to specify multiple tables.', | ||
| 85 | ); | ||
| 86 | |||
| 87 | option 'key' => ( | ||
| 88 | is => 'rw', | ||
| 89 | isa => 'ArrayRef', | ||
| 90 | required => 1, | ||
| 91 | documentation => 'key column name (must be the same for all tables). If given multiple times a composite key is used.', | ||
| 92 | ); | ||
| 93 | |||
| 94 | option 'value' => ( | ||
| 95 | is => 'rw', | ||
| 96 | isa => 'Str', | ||
| 97 | required => 1, | ||
| 98 | documentation => 'column name (must be the same for all tables).', | ||
| 99 | ); | ||
| 100 | |||
| 101 | option 'name' => ( | ||
| 102 | is => 'rw', | ||
| 103 | isa => 'ArrayRef', | ||
| 104 | required => 1, | ||
| 105 | documentation => 'name of the value column in the output. Must be given as many times as the -table option.', | ||
| 106 | ); | ||
| 107 | |||
| 108 | ###################################################################### | ||
| 109 | ######################### Consume Roles ########################## | ||
| 110 | ###################################################################### | ||
| 111 | with | ||
| 112 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 113 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 114 | -excludes => 'validate_args', | ||
| 115 | }; | ||
| 116 | |||
| 117 | |||
| 118 | ###################################################################### | ||
| 119 | ####################### Interface Methods ######################## | ||
| 120 | ###################################################################### | ||
| 121 | sub validate_args { | ||
| 122 | my ($self) = @_; | ||
| 123 | |||
| 124 | $self->_validate_args_for_output_prefix; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub run { | ||
| 128 | my ($self) = @_; | ||
| 129 | |||
| 130 | warn "Starting job: join_tables\n"; | ||
| 131 | |||
| 132 | warn "Validating arguments\n" if $self->verbose; | ||
| 133 | $self->validate_args(); | ||
| 134 | |||
| 135 | warn "Creating output path\n" if $self->verbose; | ||
| 136 | $self->make_path_for_output_prefix(); | ||
| 137 | |||
| 138 | warn "Joining Tables\n" if $self->verbose; | ||
| 139 | $self->join_tables( | ||
| 140 | $self->table, $self->key, $self->value, $self->name, | ||
| 141 | $self->o_prefix.'joined.tab'); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub join_tables { | ||
| 145 | my ($self, $table, $key, $value, $name, $outfile) = @_; | ||
| 146 | |||
| 147 | open (my $OUT, ">", $outfile); | ||
| 148 | |||
| 149 | my @tables = map {_read_table($_, $key, $value)} @{$table}; | ||
| 150 | |||
| 151 | for (my $i = 0; $i < @tables; $i++) { | ||
| 152 | if (keys %{$tables[$i]} != keys %{$tables[$i-1]}) { | ||
| 153 | die "Error: table sizes differ. Check input tables.\n"; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | # say OUT join("\t", "key", @{$opt->name}); | ||
| 157 | foreach my $key (keys %{$tables[0]}) { | ||
| 158 | my @values = map {$_->{$key}} @tables; | ||
| 159 | say $OUT join("\t", $key, @values); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | ####################################################################### | ||
| 164 | ######################## Private Functions ######################## | ||
| 165 | ####################################################################### | ||
| 166 | |||
| 167 | sub _read_table { | ||
| 168 | my ($file, $key_names, $value_name) = @_; | ||
| 169 | open(my $IN, "<", $file); | ||
| 170 | my $header = $IN->getline; | ||
| 171 | chomp $header; | ||
| 172 | my @colnames = split(/\t/, $header); | ||
| 173 | my @key_indices; | ||
| 174 | foreach my $key_name (@$key_names) { | ||
| 175 | my $idx = _column_name_to_idx(\@colnames, $key_name); | ||
| 176 | die "Error: cannot find column $key_name\n" if not defined $idx; | ||
| 177 | push @key_indices, $idx; | ||
| 178 | } | ||
| 179 | my $value_idx = _column_name_to_idx(\@colnames, $value_name); | ||
| 180 | die "Error: cannot find column $value_name\n" if not defined $value_idx; | ||
| 181 | |||
| 182 | my %data; | ||
| 183 | while (my $line = $IN->getline) { | ||
| 184 | chomp $line; | ||
| 185 | my @splitline = split(/\t/, $line); | ||
| 186 | my $key = join('|', @splitline[@key_indices]); | ||
| 187 | if (exists $data{$key}) { | ||
| 188 | die "Error: duplicate key $key found\n"; | ||
| 189 | } | ||
| 190 | $data{$key} = $splitline[$value_idx]; | ||
| 191 | } | ||
| 192 | $IN->close; | ||
| 193 | |||
| 194 | return \%data; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _column_name_to_idx { | ||
| 198 | my ($names, $name) = @_; | ||
| 199 | |||
| 200 | for (my $i=0; $i<@$names; $i++) { | ||
| 201 | return $i if $name eq $names->[$i]; | ||
| 202 | } | ||
| 203 | return undef; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |