Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

A question About Array Indexing

by ccelt09 (Sexton)
on Aug 26, 2013 at 07:14 UTC ( #1050905=perlquestion: print w/ replies, xml ) Need Help??
ccelt09 has asked for the wisdom of the Perl Monks concerning the following question:

If one has an array can you assign the length of its current index value from the beginning to a scalar variable?

Essentially I am reprinting a text file made of one long string of 0's and N's with no spaces. At the positions along this string that lie outside of certain intervals, I want all the elements reprinted as N's.

input file looks like this: 0000000NNNN00NNN0NNN0N0N0NN0N #------------------------------------------------- my $population = "all28_males"; my $count = 0; my $position = 0; my ($n, $data, @info, $info, $buf); my $filtered_sites = "/Users/logancurtis-whitchurch/Dropbox/thesis_fol +der/galaxy_chrX_data/filtered_chrX_rawdata.interval"; open (INTERVAL, "<$filtered_sites") or die "can't open $filtered_sites +"; open(MASK, "/Users/logancurtis-whitchurch/Dropbox/thesis_folder/consen +sus_files/filtered_mask_files/filtered.mask."."$population".".txt") o +r die "can't open masked file\n"; my $mask = <MASK>; chomp ($mask); for each (my @interval = <INTERVAL>); { chomp (@interval); my @positions = split(/\t/, $interval[$count]); my $start = $positions[1]; my $end = $positions[2]; while (($n = read MASK, $data, 100000) != 0) { my @info = split(//, $data); my $length = 1; foreach $info (@info){ if ( $length < $start]) { substr($mask,($info),1,'N'); $length++; } if (( $length >= $start) && ($length <= $end)) { next; $length++ } if ($length > $end) { $count++; } } $buf .= $data; print OUT "$mask"; close OUT; close MASK; } }

Comment on A question About Array Indexing
Download Code
Re: A question About Array Indexing
by Anonymous Monk on Aug 26, 2013 at 07:35 UTC

    ... words logic ... input file looks like this: 0000000NNNN00NNN0NNN0N0N0NN0N

    But but but open (INTERVAL ... open(MASK so what is input?!

    Sorry, I'm lazy

    Also I do
    understand 1) If one has an array
    ??understand?? 2) can you assign its length
    ??understand?? 3) at any given index position
    understand 4) to a scalar variable

    2-3 don't make sense. Array has one length (its size), it doesn't change based on index position; So yes, I'm sure it is possible but i'm not sure what it is.

    I know I'm not thinking clearly at the moment, but I also know you're not thinking (communicating) clearly at the moment. You could clarify yourself by asking like this
    my @interval = ( 1,2,3,4); ## WHATEVER, 2-5 lines of representative data
    my @positions = (qw/ a b q q/); ## ... 2-5 ...
    my @info = ( 6,7 ) ; ## ... 2-5 ... representative
    and then explain what you want that you're not getting (in terms of @interval/@positions/@info real representative values). If @positions is wrong, or @info is wrong, or its the next part thats wrong .... show some real representative numbers , like boundary conditions (min/max allowed values), so that logic can be inferred/guessed

      I apologize for the poorly constructed explanation and I do appreciate your help very much. A text file with a single line consisting of ~155e6 characters, all 0's and N's, is read in as a scalar variable. Each character is assigned to a single element of an array:

      open(INPUT, "/Users/logancurtis-whitchurch/Dropbox/thesis_folder/conse +nsus_files/mask_files/mask."."$population".".chr.23.txt") or die "can +'t open masked file\n"; ;; my $input = <INPUT>; ;; chomp($input); ;; my @info = split( //, $input );

      I'd like to read each element of @info sequentially, at the same time have a scalar $length that corresponds to the length from the beginning of @info, $info[0], to the current, farthest position being read, say $info[3000] where $length would equal 3001. With this information I can tell the program, if $length is within the bounds of my current interval values ( $start and $end) go on to the next element of @info and increment $length and so on until $length is greater than $end, in which case I increment my interval values which have been similarly assigned to an array from an input text file. Then I would like to tell the program if $length is less than $start change the current element of @info to an 'N' , move to the next element of @info and increment $length. The goal: change all elements of the @info array that lie outside of my intervals to N's.

      this shows the format of my interval input file and how I assign $start and $end values:

      input format for intervals: chrX 1 6000 chrX 6045 6302 chrX 7204 8239 ...etc until all 155e6 positions have been covered. my $filtered_sites = "/Users/logancurtis-whitchurch/Dropbox/thesis_fol +der/galaxy_chrX_data/filtered_chrX_rawdata.interval"; ;; open (INTERVAL, "<$filtered_sites") or die "can't open $filtered_sites +"; ;; my @interval = <INTERVAL>; ;; close (INTERVAL); ;; chomp (@interval); ;; my @site_info= split(/\t/, $interval[$count]); ;; my $start = $site_info[1]; ;; my $end = $site_info[2]; ;;

      so every time $length is greater than $end I increment $count to assign new $start and $end values.

        A text file with a single line consisting of ~155e6 characters, all 0's and N's, is read in as a scalar variable. Each character is assigned to a single element of an array

        As a scalar, your 155e6 file will require ~ 150 MegaBytes of memory.

        As an array, 1 char per element, it will require ~ 10 GigaBytes of memory. Assuming you have that available.

        But for your stated goal:

        The goal: change all elements of the @info array that lie outside of my intervals to N's.

        There is no need to go through the time and memory costly process of spliting your string to an array. And no need to iterate over 155 million characters one at a time.

        You can easily and quickly overwrite the characters outside your ranges with 'N's, in-place in the scalar:

        open( SEQ, "/Users/logancurtis-whitchurch/Dropbox/thesis_folder/consensus_fil +es/mask_files/mask."."$population".".chr.23.txt" ) or die "can't open masked file\n"; my $bigScalar = <SEQ>; close SEQ; open (INTERVAL, "<$filtered_sites") or die "can't open $filtered_sites +"; my $lastEnd = 0; while( <INTERVAL> ) { my( $start, $end ) = split "\t", $_; ## change everything from the end of the last range ## to the start of this range to 'N' substr( $bigScalar, $lastEnd, $start ) =~ tr[\x00-\xff][N]; $lastEnd = $end; } close INTERVAL; ## change everything from the end of the last range to the end of stri +ng to 'N' substr( $bigScalar, $lastEnd, length( $bigScalar ) ) =~ tr[\x00-\xff][ +N]; ## do something with $bigScalar ...

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: A question About Array Indexing
by AnomalousMonk (Abbot) on Aug 26, 2013 at 07:57 UTC
Re: A question About Array Indexing
by poj (Priest) on Aug 26, 2013 at 08:28 UTC

    I'm guessing this is sort of what you are trying to do ;

    #!perl use strict; my $str = '0000000NNNN00NNN0NNN0N0N0NN0N'; my @str = split '',$str; # input my @mask = map{ 'N' }(0..$#str); while (<DATA>){ chomp; my (undef,$start,$end) = split "[,\t]",$_; for my $ix ($start..$end){ $mask[$ix-1] = ' '; } } # process my @output=(); for my $ix (0..$#str){ $output[$ix] = ($mask[$ix] eq 'N') ? 'N' : $str[$ix]; } # output print 'mask ',join '',@mask,"\n"; print 'string ',join '',@str,"\n"; print 'result ',join '',@output,"\n"; # intervals no,start,end __DATA__ 1,2,4 2,6,10 3,20,25 4,28,28
    poj
Re: A question About Array Indexing
by AnomalousMonk (Abbot) on Aug 26, 2013 at 08:35 UTC

    I don't feel I clearly understand the problem ccelt09 is trying to address, but here's an approach (one of many) that might be helpful as a first approximation to a solution. Note: No validation of ranges is done to insure against negative start/end offsets, start/end inversion, overlap, etc.

    >perl -wMstrict -le "my $test = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; ;; my %inclusive_ranges = map { $_ => 1 } 7 .. 11, 18 .. 22; ;; my @excluded = grep ! $inclusive_ranges{$_}, 0 .. length($test) - 1; ;; my $str = $test; print qq{'$str'}; ;; substr $str, $_, 1, '-' for @excluded; print qq{'$str'}; " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' '-------HIJKL------STUVW---'

    Update: Another way, same caveats. Note: This approach assumes the  \x00 (null) character is never part of input data.

    >perl -wMstrict -le "my $test = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; ;; my %inclusive_ranges = map { $_ => 1 } 7 .. 11, 18 .. 22; ;; my $exclude_mask = join '', map $inclusive_ranges{$_} ? qq{\xFF} : qq{\x00}, 0 .. length($test) - 1 ; ;; my $str = $test; print qq{'$str'}; ;; $str &= $exclude_mask; $str =~ tr/\x00/-/; print qq{'$str'}; " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' '-------HIJKL------STUVW---'

      This is a correct assumption! The only variations: 1) The test scalar will be read from an input file. 2) The inclusive ranges are also read in from a text file with the following format:

      chrX 1 60000 chrX 60038 60041 chrX 60446 60468 chrX 60689 61799 chrX 62272 62290 chrX 62703 62797

      Writing all the inclusive ranges would take a fair amount of time as there are well over 20,000. Can they also be read in within this block of code?

        BrowserUk seems to answer these questions pretty well here.

        Update: Actually, upon closer inspection, I think I would change the statement
            my( $start, $end ) = split "\t", $_;
        in the  while( <INTERVAL> ) { ... } loop to
            my( undef, $start, $end ) = split "\t", $_;
        because the interval data seems to be three-field, tab-delimited records like
        chrX    1    60000
        in which the first field, to be ignored, is a label of some kind.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050905]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-21 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (168 votes), past polls