|
Hi.
I'm trying to collapse 2 tables in 1 as follows:
table 1 - LOC_Os01g01020 1 -1 8174 9019 LOC_Os01g01020 CDS-ANE
-
R02-TIGRv4S1-000003F 2 1 33031 33967 LOC_Os02g01060 33032 33966 FL-AE
-
R02-TIGRv4S1-000007F 2 1 78460 79563 LOC_Os02g01140 78174 79540 FL-AE
-
table 2 - AK062483 001-103-G07 R02-TIGRv4S1-000003F
-
K067036 J013092C19 R02-TIGRv4S1-000007F
-
AK0624543 prueba R02-TIGRv4S1-000003F
outfile -
LOC_Os01g01020 1 -1 8174 9019 LOC_Os01g01020 CDS-ANE
-
R02-TIGRv4S1-000003F 2 1 33031 33967 LOC_Os02g01060 33032 33966 FL-AE AK062483 001-103-G07 R02-TIGRv4S1-000003F
-
R02-TIGRv4S1-000003F 2 1 33031 33967 LOC_Os02g01060 33032 33966 FL-AE AK0624543 prueba R02-TIGRv4S1-000003F
-
R02-TIGRv4S1-000007F 2 1 78460 79563 LOC_Os02g01140 78174 79540 FL-AE K067036 J013092C19 R02-TIGRv4S1-000007F
I made this, but it didn't work: - #! usr/bin/perl
-
use strict;
-
my $lista_1= shift or die;
-
my $lista_2= shift or die;
-
my $salida= shift or die;
-
open (ARCHIVO_1, "$lista_1");
-
open (ARCHIVO_2, "$lista_2");
-
open (SALIDA, ">>$salida");
-
my @tabla_1= <ARCHIVO_1>;
-
my @tabla_2= <ARCHIVO_2>;
-
close ARCHIVO_1;
-
close ARCHIVO_2;
-
my %hash='';
-
#####################################################################################
-
my $a= scalar @tabla_1;
-
my $b= scalar @tabla_2;
-
for (my $i=0; $i<$a ; $i++) {
-
my @fila_tabla1= split ("\t", @tabla_1[$i]);
-
my ($col_1,$col_2,$col_3,$col_4,$col_5,$col_6,$col_7,$col_8)= @fila_tabla1[0,1,2,3,4,5,6,7];
-
for (my $j=0; $j<$b; $j++) {
-
my @fila_tabla2= split ("\t", @tabla_2[$j]);
-
my ($col_A,$col_B,$col_C)=@fila_tabla2[0,1,2];
-
if ($col_1 eq $col_C) {
-
print SALIDA "$tabla_1[$i]\t$tabla_2[$j]\n";
-
$hash{$col_1}=1;
-
$j++;
-
}elsif ($j == $b-1) {
-
unless $hash{$col_1}=1{
-
print SALIDA "$tabla_1[$i]\n";
-
}
-
}else{
-
$j++;
-
}
-
}
-
}
-
The problem is this: never complys whith any of the conditions ( and then, the output is empty) and I don't understand why. Help!!! Thanks. (Sorry if I wrote something wrong, english is not my native lenguage)
| |
Share:
Expert 2GB |
The code you posted should not even compile as there is a syntax error here:
should be: - unless ($hash{$col_1}=1) {
but using the assignment operator "=" is also wrong, you should be using a comparison operator "==": - unless ($hash{$col_1} == 1){
So after fixing all that and I run your script I get this output: - LOC_Os01g01020 1 -1 8174 9019 LOC_Os01g01020 CDS-ANE
-
-
R02-TIGRv4S1-000003F 2 1 33031 33967 LOC_Os02g01060 33032 33966 FL-AE
-
AK0624543 prueba R02-TIGRv4S1-000003F
-
R02-TIGRv4S1-000007F 2 1 78460 79563 LOC_Os02g01140 78174 79540 FL-AE
| | Expert 2GB |
See if you can figure this out, if not, ask questions: - #!/usr/bin/perl
-
use strict;
-
use warnings;
-
my $lista_1 = shift or die;
-
my $lista_2 = shift or die;
-
my $salida = shift or die;
-
open (ARCHIVO_1, $lista_1);
-
open (ARCHIVO_2, $lista_2);
-
my @tabla_1 = <ARCHIVO_1>;
-
chomp (@tabla_1);
-
my @tabla_2 = <ARCHIVO_2>;
-
chomp (@tabla_2);
-
close ARCHIVO_1;
-
close ARCHIVO_2;
-
my %hash = ();
-
my @order = ();
-
#####################################################################################
-
my $header = shift @tabla_1;
-
foreach my $line (@tabla_1) {
-
chomp $line;
-
my ($key) = $line =~ /^(\S+)/;
-
push @order, $key;
-
push @{$hash{$key}},$line;
-
}
-
foreach my $line (@tabla_2) {
-
chomp $line;
-
my ($key) = $line =~ /(\S+)$/;
-
push @{$hash{$key}},$line;
-
}
-
open (SALIDA, ">>$salida");
-
print SALIDA $header,"\n";
-
foreach my $key (@order) {
-
print SALIDA shift @{$hash{$key}},"\n";
-
foreach my $line (@{$hash{$key}}) {
-
print SALIDA " $line\n";
-
}
-
}
The output is not exactly like what you posted but that would be easy to change. I just went ahead and put all the lines from tabla_2 under the corresponding line in tabla_1 instead of printing the tabla_1 lines more than once: - LOC_Os01g01020 1 -1 8174 9019 LOC_Os01g01020 CDS-ANE
-
R02-TIGRv4S1-000003F 2 1 33031 33967 LOC_Os02g01060 33032 33966 FL-AE
-
AK062483 001-103-G07 R02-TIGRv4S1-000003F
-
AK0624543 prueba R02-TIGRv4S1-000003F
-
R02-TIGRv4S1-000007F 2 1 78460 79563 LOC_Os02g01140 78174 79540 FL-AE
-
K067036 J013092C19 R02-TIGRv4S1-000007F
-
The code could probably be written a bit better but will leave that up to you or anyone else if they wish to contribute further.
| | |
Well, first of all, thanks for your help. I've never expected an answer so fast.
Second, this is very usefull because I've never worked with the push function (well, I started to "program" 4 months ago...yes, really, really newbie, so this world is new for me).
First I have to read about this and after I'm going to start to do questions.
So, ones more, thanks
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by newbie_mw |
last post: by
|
1 post
views
Thread by newbie_mw |
last post: by
|
9 posts
views
Thread by Arvind R |
last post: by
|
5 posts
views
Thread by Newbie! |
last post: by
|
32 posts
views
Thread by Tom Cole |
last post: by
|
11 posts
views
Thread by Ed Dana |
last post: by
|
2 posts
views
Thread by =?Utf-8?B?RW1tYSBIb3Bl?= |
last post: by
| | |
5 posts
views
Thread by Dave |
last post: by
| | | | | | | | | | | |