joomanji has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I've a file with 12 column with the following format: read_no,read_name,read_len,read_position_begin,read_position_end,reference_seq_no,reference_position_begin,reference_position_end,reversed,no_of_match,paired,alignment_score Example of the file:
223445 BG2A093.x1 51 7 51 4 75 96 0 1 0 44 223446 BG2A073.x1 51 7 51 4 43 95 0 1 0 44 223447 BG2A033.x1 51 7 51 4 44 92 0 1 0 42 223448 BG2A693.x1 51 7 51 4 45 93 1 1 0 43 223449 BG2A045.x1 51 7 51 4 47 95 1 1 0 47 223501 BG2A034.x1 51 7 51 4 47 95 0 1 0 46 223442 BG2A021.x1 51 7 51 4 48 96 0 1 0 45
What I'm trying to do is to add a number next to column no.7, so that whenever there is an duplication in column 7 and column 8 to the next line. So the result will be something like this:
223445 BG2A093.x1 51 7 51 4 1 75 96 0 1 0 +44 223446 BG2A073.x1 51 7 51 4 2 75 95 0 1 0 4 +3 223447 BG2A033.x1 51 7 51 4 1 44 92 0 1 0 +42 223448 BG2A693.x1 51 7 51 4 1 45 93 1 1 0 +43 223449 BG2A045.x1 51 7 51 4 1 47 95 1 1 0 +47 223501 BG2A034.x1 51 7 51 4 2 47 95 0 1 0 +46 223442 BG2A021.x1 51 7 51 4 1 48 96 0 1 0 +45
I've try some perl scripts and came up something like this:
#!/usr/local/bin/perl -w my $pre_ref_pos_b; my $pre_ref_pos_e; my $count while (<>) { chomp; my ($r_no,$r_name,$r_len,$r_pos_b, $r_pos_e, $ref_seq_no, $ref_pos_ +b, $ref_pos_e, $rev, $no_match, $paired, $aln_score) = split; while (defined($pre_ref_pos_b) && defined($pre_ref_pos_e) eq $ref_p +os_b && $ref_pos_e) { $count++; print $r_name,$count,$r_ref_pos_b,$ref_pos_e\n; } $pre_ref_pos_b = $ref_pos_b; $pre_ref_pos_e = $ref_pos_e; } }
The problem is I've no idea how the lines of data being kept in the memory, and how should I put in the program so that it will reset the count whenever the the ref_pos_b and ref_pos_e is different from the proceeding lines. Your help is really appreciate, I'm learning the perl, and I'm still very very new to it. Best regads.

Replies are listed 'Best First'.
Re: Compute previous lines data
by almut (Canon) on Jul 13, 2010 at 05:02 UTC
    how should I put in the program so that it will reset the count whenever the the ref_pos_b and ref_pos_e is different from the proceeding lines.

    Just reset it if the current value isn't equal to the previous one, e.g.

    if ($ref_pos_b eq $pre_ref_pos_b) { $count++; } else { $count = 1; # reset }
Re: Compute previous lines data
by graff (Chancellor) on Jul 13, 2010 at 09:36 UTC
    If I understand your description of the task, it looks like your two snippets of sample data are different in ways that they shouldn't be -- that's a bit confusing.

    But based on your description (and assuming that the input data is correctly sorted according to the values of the "reference_position_begin" and "reference_position_end" columns), I think the following would do what you want:

    #!/usr/local/bin/perl use strict; use warnings; my $prev_key = ''; my $match_count; while (<>) { my @flds = split; my $key = join ' ', @flds[6,7]; $match_count = 0 if ( $key ne $prev_key ); $prev_key = $key; splice( @flds, 6, 0, ++$match_count ); print join( "\t", @flds ), "\n"; }
    If the input data isn't sorted properly yet, just look up the unix "sort" command to take care of that.
Re: Compute previous lines data
by jwkrahn (Abbot) on Jul 13, 2010 at 06:29 UTC
    while (defined($pre_ref_pos_b) && defined($pre_ref_pos_e) eq $ref_p +os_b && $ref_pos_e) {

    Because the  eq operator has higher precedence than the  && operator you are comparing the return value of defined($pre_ref_pos_e), which is either true or false, alphabetically to the numerical value in $ref_pos_b.    That expression will only be true if defined($pre_ref_pos_e) is true and $ref_pos_b contains "1".