Hello guys
I have the following dataset
ID Kind
259 P0_P2
260 P0_P2
57 P2
58 P2
59 P2
60 P2
61 P2
62 P2
I am trying to write a Perl code to pick the kind ID of "P0_P2" and attach it to the IDs of the rest of the kinds. So the expected results will be:
Anc Child
259 57
259 58
259 59
259 60
259 61
259 62
259 57
260 58
260 59
260 60
260 61
260 62
I wrote the following Perl code, but not sure why it is not working. The code takes an external tab delimited file, which contains the data.
Could you please help me with this
Many thanks
I have the following dataset
ID Kind
259 P0_P2
260 P0_P2
57 P2
58 P2
59 P2
60 P2
61 P2
62 P2
I am trying to write a Perl code to pick the kind ID of "P0_P2" and attach it to the IDs of the rest of the kinds. So the expected results will be:
Anc Child
259 57
259 58
259 59
259 60
259 61
259 62
259 57
260 58
260 59
260 60
260 61
260 62
I wrote the following Perl code, but not sure why it is not working. The code takes an external tab delimited file, which contains the data.
#!/usr/bin/perl
my $firFile = "$ARGV[0]";
open (NGS, $firFile);
my $Ancestor="";
my $Childern="";
my $starting_childern = 0;
while (<NGS>) {
chomp;
my @fields = split ('\t', $_);
if ($fields[1] eq "P0_P2") {
$starting_childern = 1;
$Ancestor = $fields[0];
print "$Ancestor\n";
}
elsif ($starting_childern == 1) {
$Childern = $fields[0];
if ($fields[1] ne "P0_P2"){
print "$Ancestor \t $Childern\n";
}
}
}
close (NGS);
my $firFile = "$ARGV[0]";
open (NGS, $firFile);
my $Ancestor="";
my $Childern="";
my $starting_childern = 0;
while (<NGS>) {
chomp;
my @fields = split ('\t', $_);
if ($fields[1] eq "P0_P2") {
$starting_childern = 1;
$Ancestor = $fields[0];
print "$Ancestor\n";
}
elsif ($starting_childern == 1) {
$Childern = $fields[0];
if ($fields[1] ne "P0_P2"){
print "$Ancestor \t $Childern\n";
}
}
}
close (NGS);
Could you please help me with this
Many thanks
Comment