Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Compare elements of a line in a file

by oxydeepu (Novice)
on Sep 13, 2012 at 07:25 UTC ( #993390=perlquestion: print w/ replies, xml ) Need Help??
oxydeepu has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I need a small help with a script. i have a file in the given format it is tab-seperated file

id orient co-ordinates

Contig0 + 109027 162520 184045 324019 375437
Contig0 - 165846 193861 243737 243740 243741 395893
Contig1 + 126334 145895 150562
Contig1 - 126804 152668 152671 152672 152673
Contig10 + 1069 126046 126047 126048
Contig10 - 281973 83318 83395
Contig100 + 249511 249557 51059
Contig100 - 161016 161020 245920
Contig10003 + 17593 17594
Contig1001 + 15558 15559 55338 55345 92236

So i need to compare the co-ordinates for a particular id on particular orientation and filter out co-ordiantes which are within 50 bases and keep the smallest co-ordiante amongst them. Basically comparison linewise

eg:
Contig10 + 1069 126046 126047 126048
Contig1001 + 15558 15559 55338 55345 92236

output :
Contig10 + 1069 126046
Contig1001 + 15558 55338 92236

I hope I explained it properly.
Can you please help me with this problem.
Thank you in advance

Best
Deepak

Comment on Compare elements of a line in a file
Re: Compare elements of a line in a file
by frozenwithjoy (Curate) on Sep 13, 2012 at 07:55 UTC
    Here is a start:
    perl -E '@coords = qw(1069 126046 126047 126048); map {say if $_ > $la +st+50; $last = $_} @coords' ______ 1069 126046

      Thank you for the reply.

      I have these data inside a file. So how am I supposed to format? This code is direct from command line right?

      Thank you
      Deepak

        So, based on the code you posted in response to Dave, you should be able to replace your foreach loop with:
        foreach (sort keys %h) { @n = split /\t/, $h{$_}; map {say if $_ > $last+50; $last = $_} @n; }
Re: Compare elements of a line in a file
by davido (Archbishop) on Sep 13, 2012 at 07:55 UTC

    I need a small help with a script.

    Please post the portion of the script that you need help with.


    Dave

      Hi Dave,

      So i have created a hash with the first to columns as an identifier. What i need to is compare the hash values which are in an array.

      The code is given below

      Best and Thanks
      Deepak

      open($f,$ARGV[0]); while(<$f>) { chomp; @l = split "_",$_; $h{$l[0]."\t".$l[1]} .= $l[2]."\t"; } foreach (sort keys %h) { @n = split "\t",$h{$_}; if(scalar @n == 1) { print "$_\t$h{$_}\n"; } else { @s = sort {$a <=> $b} @n; for($i=0; $i <= $#s; $i++) { if($i == 0) { push(@na,$s[$i]); } else { $len = $#na; if($s[$i]-$na[$length] <= 45) { next; } else { push(@na,$s[$i]); } } } foreach $k(@na) { $hn{$k} = $_; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993390]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-09-23 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls