Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Splice an array into another array

by Smeb (Novice)
on Jun 23, 2017 at 22:30 UTC ( [id://1193411]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys,

As the title says, what I wanted to do is take one DNA file and put it into an array, do the same to a second DNA file and then insert one of the files somewhere in the other.

I have 2 files, DNA3 and DNA4

The content within the DNA3 file is: ATCGC

The content within the DNA4 file is: AAATTGC

What I want to do is to insert DNA4 into position #1 in DNA3, so the result would look like this: AAAATTGCTCGC

Currently my code looks like this:

#!/usr/bin/perl -w $DNA3file = 'testDNA3'; open(DNA3handle, $DNA3file); @DNA3 = <DNA3handle>; $DNA4file = 'testDNA4'; open(DNA4handle, $DNA4file); @DNA4 = <DNA4handle>; splice (@DNA3, 1, 0, @DNA4); print @DNA3, "\n"; exit;

However, what is displayed really puzzled me:

ATCGC

AAATTGC

*Note, in my complier there isn't the space in between the output*

For the life of me I can't figure out what is wrong, I can run the above operation fine if I manually insert the DNA sequences into an array, but for large DNA set that is simply not possible. Really appreciate any help here, thanks for your time!

Replies are listed 'Best First'.
Re: Splice an array into another array
by huck (Prior) on Jun 23, 2017 at 22:45 UTC

    @DNA3 = <DNA3handle>; creates an array with one member containing "ATCGC" and any line endings.

    use strict; use warnings; my $dna3='ATCGC'; my $dna4='AAATTGC'; my $after=0; $dna3=substr($dna3,$after,1).$dna4.substr($dna3,$after+1); print $dna3."\n";

      Could also be:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $dna3 = 'ATCGC'; my $dna4 = 'AAATTGC'; ;; my $offset = 1; substr $dna3, $offset, 0, $dna4; print qq{'$dna3'}; " 'AAAATTGCTCGC'

      Thanks for the reply, really appreciate it.

      So are you saying that, I cannot store DNA files I downloaded from Genbank as arrays via the filehandler, and then modify them? Because all of the example I have seen where people modify arrays (in my case I am obviously interested in the splice function) amounts to the user manually type in some elements, store them as array and then modify them (like the code you provided).

        Let me try again

        @DNA3 = <DNA3handle>; creates an array where the contents of each line of DNAHANDLE (along with any line endings) becomes its own element of the array

        It is not an array where each element is a single character of the file

        @DNA3 = <DNA3handle>; chomp @DNA3; @DNA3=split('',join('',@DNA3));
        Would be that kind of array (without line endings (chomp))

Re: Splice an array into another array
by AnomalousMonk (Archbishop) on Jun 24, 2017 at 05:33 UTC

    A parenthetic note on debugging...

    For the life of me I can't figure out what is wrong, I can run the above operation fine if I manually insert the DNA sequences into an array ...

    It sometimes helps a lot to see just what data you have. If you had written debug statements like

    use Data::Dumper; ... open(DNA3handle, $DNA3file); @DNA3 = <DNA3handle>; print Dumper \@DNA3; # what's actually in the array right now? ... open(DNA4handle, $DNA4file); @DNA4 = <DNA4handle>; print Dumper \@DNA4; # ditto ...
    you might have gotten further down the "That's not what I want/expect. Why not? Oh, of course..." path to wisdom. Nothing wrong with asking questions, but it's usually much more satisfying to manage on your own. See Data::Dumper, which is core. (I prefer Data::Dump::dd() and friends, but it's not core.) See also toolic's Basic debugging checklist.

    And a note about Best Practices...

    In your OPed code, I see things like the use of global variables and filehandles, no checking of open operations. These are all officially Frowned Upon. An extensive (and expensive!) discussion of Perl best practices can be found in TheDamian's Perl Best Practices. Not everyone (to put it mildly!) agrees with every recommended BP, but a thoroughly developed rationale is given for each. Another good discussion of current best practices is in chromatic's Modern Perl (free download here).


    Give a man a fish:  <%-{-{-{-<

      Thank you kindly for the thoughtful follow up, really. I will look up the correct practices for Perl as I am still relatively new to programming as a whole, coming from a biological background.
Re: Splice an array into another array
by AnomalousMonk (Archbishop) on Jun 23, 2017 at 23:21 UTC

    splice operates on array elements. 'ATCGC' is a string in Perl, not an array. A string can be converted into an array and operated on as such, but in this case it seems like a lot of wasted motion:

    c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; my $DNA3 = 'ATCGC'; my @DNA3 = split '', $DNA3; ;; my $DNA4 = 'AAATTGC'; my @DNA4 = split '', $DNA4; ;; my $offset = 1; splice @DNA3, $offset, 0, @DNA4; my $newDNA3 = join '', @DNA3; ;; is $newDNA3, 'AAAATTGCTCGC', 'array splice ok'; ;; done_testing; " ok 1 - array splice ok 1..1 ok 2 - no warnings 1..2


    Give a man a fish:  <%-{-{-{-<

Re: Splice an array into another array
by AnomalousMonk (Archbishop) on Jun 23, 2017 at 23:00 UTC

    Another way of operating directly on the DNA strings as strings (needs Perl version 5.10 or higher for  \K regex operator):

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my $DNA3 = 'ATCGC'; my $DNA4 = 'AAATTGC'; ;; my $n = 1; $DNA3 =~ s{ \A .{$n} \K }{$DNA4}xms; ;; is $DNA3, 'AAAATTGCTCGC', 'string splice ok'; ;; done_testing; " ok 1 - string splice ok 1..1 ok 2 - no warnings 1..2


    Give a man a fish:  <%-{-{-{-<

      I think I am getting it now, thank you

      Final question for clarity's sake: in the end I have to manually enter each nucleotide, am I understanding this right? In other words I can't be lazy and store several DNA files I downloaded online into arrays and then operate on them. For each of my array I have to manually type the nucleotide sequences in myself or copy+paste them in

      below I includes 2 examples of what I mean, in the first example I manually stored the nucleotides into arrays, and everything works. In the second example the nucleotides are stored into arrays via filehandler and things do not work (in the way I want)

      this will splice the 2 arrays together in whatever fashion I want

      #!/usr/bin/perl -w @DNA3 = qw(A T C C A T G C); @DNA4 = qw(G C C G A A T T A); splice (@DNA3, 1, 0, @DNA4); print @DNA3, "\n"; exit;

      this will not work, I cannot use this to splice 2 arrays together

      #!/usr/bin/perl -w $DNA3file = 'testDNA3'; open(DNA3handle, $DNA3file); @DNA3 = <DNA3handle>; $DNA4file = 'testDNA4'; open(DNA4handle, $DNA4file); @DNA4 = <DNA4handle>; splice (@DNA3, 1, 0, @DNA4); print @DNA3, "\n"; exit;
        ... in the end I have to manually enter each nucleotide, am I understanding this right? In other words I can't be lazy and store several DNA files I downloaded online into arrays and then operate on them. For each of my array I have to manually type the nucleotide sequences in myself or copy+paste them in

        You have to realize that when you read a file with | with a sequence of statements like
            $DNA3file = 'testDNA3';
            open(DNA3handle, $DNA3file);
            @DNA3 = <DNA3handle>;
        the  @DNA3 array ends up with all the lines of the file, and each line is a string that looks like  "ATCCATGC\n" (note the newline that will be at the end of (almost) every line).

        If you have the lines of the file as strings, you | your program can operate on them as strings, break them up into arrays and operate on them, etc., as appropriate for your application. Your choice.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found