SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl semna Bioinformatics 6 09-16-2011 01:16 AM
perl? semna Bioinformatics 5 08-18-2011 01:38 AM
perl? semna Bioinformatics 1 07-27-2011 08:05 AM
perl? semna Bioinformatics 8 07-18-2011 06:24 AM
Perl Help Wei-HD Bioinformatics 9 02-25-2010 01:35 PM

Reply
 
Thread Tools
Old 07-06-2017, 12:42 PM   #1
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default Need help with Perl

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.

Quote:
#!/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);

Could you please help me with this
Many thanks
Fad2012 is offline   Reply With Quote
Old 07-06-2017, 12:48 PM   #2
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

PS.
The code is printing nothing upon execution!
Fad2012 is offline   Reply With Quote
Old 07-06-2017, 04:58 PM   #3
SNPsaurus
Registered Vendor
 
Location: Eugene, OR

Join Date: May 2013
Posts: 421
Default

I'd add some informative print statements like print "fields[0] fields[1] in Ancestor if block\n" at different locations to see what your computer is reading in.

I was going to say, "oh it is a typo that you spelled children childern" but you were consistent the whole time!".
__________________
Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com
SNPsaurus is offline   Reply With Quote
Old 07-11-2017, 03:46 PM   #4
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

Thank you SNPsaurus. I am not sure what is going on with this code.
Any suggestion anyone
Many thanks
Fad2012 is offline   Reply With Quote
Old 07-11-2017, 04:18 PM   #5
SNPsaurus
Registered Vendor
 
Location: Eugene, OR

Join Date: May 2013
Posts: 421
Default

Did you try to add some informative print statements to see what information is in your variables?
__________________
Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com
SNPsaurus is offline   Reply With Quote
Old 07-12-2017, 03:09 PM   #6
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

Hi SNPsaurus

Yes, I tried. The only way it printed something when I put

print "fields[0] \n"

Just after the split command. I tried to play with many other things with no positive results. For example, I tried to change the variable types from my $Ancestor=""; to my $Ancestor=0;. I also tried to move print "$Ancestor \t $Childern\n"; around the code, but nothing worked...
Fad2012 is offline   Reply With Quote
Old 07-12-2017, 03:13 PM   #7
SNPsaurus
Registered Vendor
 
Location: Eugene, OR

Join Date: May 2013
Posts: 421
Default

What did it print? It is hard to help when you provide so little information. Did it print "259 P0_P2" or the proper "259"? After the split, add:
print "f0 $fields[0]\tf1 $fields[1]\n";

What is the output from that?
__________________
Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com
SNPsaurus is offline   Reply With Quote
Old 07-12-2017, 03:27 PM   #8
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

Thank you very much SNPsaurus.

I removed the column titles for now. So I removed the ID nad the Kind from the table. WhenI use print "f0 $fields[0]\tf1 $fields[1]\n"; It prints the following

Quote:
f0 259 f1 P0_P2
f0 260 f1 P0_P2
f0 57 f1 P2
f0 58 f1 P2
f0 59 f1 P2
f0 60 f1 P2
f0 61 f1 P2
f0 62 f1 P2
When I use:
print "f0 $fields[0]\n"; it prints:

Quote:
f0 259
f0 260
f0 57
f0 58
f0 59
f0 60
f0 61
f0 62
In both cases it should be after the split line

Many thanks
Fad2012 is offline   Reply With Quote
Old 07-12-2017, 03:38 PM   #9
SNPsaurus
Registered Vendor
 
Location: Eugene, OR

Join Date: May 2013
Posts: 421
Default

OK, so the split works. Let's see if your if statement works. After if ($fields[1] eq "P0_P2") {
put:
print "in if statement\n";
__________________
Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com
SNPsaurus is offline   Reply With Quote
Old 07-12-2017, 04:06 PM   #10
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

Quote:
Originally Posted by SNPsaurus View Post
OK, so the split works. Let's see if your if statement works. After if ($fields[1] eq "P0_P2") {
put:
print "in if statement\n";
Nothing appeared, so that means the statement is not working?
Fad2012 is offline   Reply With Quote
Old 07-12-2017, 04:15 PM   #11
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default

Well, when I replaced the "eq" with "=~" things worked and it printed:
in if statement
in if statement

It seems that the eq is not working
Fad2012 is offline   Reply With Quote
Old 07-12-2017, 04:23 PM   #12
SNPsaurus
Registered Vendor
 
Location: Eugene, OR

Join Date: May 2013
Posts: 421
Default

Right. I wonder if your input file has DOS or other funny line breaks/ carriage returns at the end. That used to be the source of most of my woes.

You could try chop; chop; instead of chomp; I'd print field[0] and [1] again to see if it is trimming your actual data instead of getting rid of both line end characters.

You could also do

local $/ = "\n";

for my $line (<NGS>) {
$line =~ s/\r?\n$//;

Which should get rid end of line weirdness. You could re-type your if statement to make sure P0 is not PO or other random typos.
__________________
Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com
SNPsaurus is offline   Reply With Quote
Old 07-12-2017, 04:27 PM   #13
Fad2012
Member
 
Location: UK

Join Date: Sep 2012
Posts: 60
Default Thank you

I really appreciate your help, I will play around with it and will let you know

Many thanks
Fadi
Fad2012 is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off




All times are GMT -8. The time now is 07:34 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Single Sign On provided by vBSSO