Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Assigning multiple lines into first element of array

by johngg (Canon)
on Mar 01, 2020 at 00:46 UTC ( #11113590=note: print w/replies, xml ) Need Help??


in reply to Assigning multiple lines into first element of array

Thank you for clearing up the file layout question. I don't know if the following approach will be helpful to you but I wonder if, rather than an array, a hash table might be something to explore. A hash (also sometimes called an associative array) is arranged as key and value pairs. Here I initialise scalars to hold the sequence name and the sequence characters and also the hash in which sequences will be stored. Then I read the file line by line using chomp to remove line endings then storing the ">......." line as the key and concatenating the following lines together to form the value. When I encounter the next ">......." line or when I reach the end of file I call the addSequence() subroutine to add the key/value pair to the hash. Note that when the first ">......." line is read addSequence() is called but nothing as added to the hash as the stored sequence title is empty.

The output shows the sorted keys from the resultant hash then I use the Data::Dumper module to show the actual hash in the form 'key' => 'value', the output now wrapping because of the long, concatenated lines. If it was preferred to keep the sequence data lines separated in an array this could be easily accomplished.

use 5.026; use warnings; use Data::Dumper; open my $inFH, q{<}, \ <<__EOD__ or die $!; >NM_030643.4 Homo sapiens apolipoprotein L4 (APOL4) GAGGTGCTGGGGAGCAGCGTGTTTGCTGTGCTTGATTGTGAGCTGCTGGGAAGTTGTGACTTTCATTTTA CCTTTCGAATTCCTGGGTATATCTTGGGGGCTGGAGGACGTGTCTGGTTATTATATAGGTGCACAGCTGG AGGTGAGATCCACACAGCTCAGACCAGCTGGATCTTGCTCAGTCTCTGTCAGAGGAAGATCCCTTGGAGG AGGCCCCGCAGCGACATGGAGGGAGCTGCTTTGCTGAAAATCTTTGTCGTCTGCATCTGGAACCAAAATC >NM_001198855.1 Homo sapiens cytochrome P450 family 2 subfamily C memb +er 8 (CYP2C8) ACATGTCAAAGAGACACACACTAAATTAGCAGGGAGTGTTATAAAAACTTTGGAGTGCAAGCTCACAGCT GTCTTAATAAGAAGAGAAGGCTTCAATGGAACCTTTTGTGGTCCTGGTGCTGTGTCTCTCTTTTATGCTT CTCTTTTCACTCTGGAGACAGAGCTGTAGGAGAAGGAAGCTCCCTCCTGGCCCCACTCCTCTTCCTATTA >NR_029834.1 Homo sapiens microRNA 200a (MIR200A), microRNA CCGGGCCCCTGTGAGCATCTTACCGGACAGTGCTGGATTTCCCAGCTTGACTCTAACACTGTCTGGTAAC GATGTTCAAAGGTGACCCGC >AC067940.1 Homo sapiens clone RP11-818E9, LOW-PASS SEQUENCE SAMPLING AAATACAACTTTAAATCAAAACGGTAAAAATTCCACTCTTTCATACTAACTTCAAAAGTATTTGCTTTAA AAAAAAAGNNNNNNNNNNAAACTGAATTTCTATTAAGCATCTATTTATAGAAGAGAGTAAACACCCCGTG AATAAAAGACAGAGAATTGTAGCAGCCCGAAGTCCCTTTTCTCTCCTCCCAAGCATTTGGCTCTGGTCCA AATTCACATATCCTGCTCCGTAAAACAAAGTGCCTTGGTTAACCTAACGTTATTCCTTGAACAGTAGTTT AGTGATCAACTAGTTTTTGTTGTTGTTGTTGTTTGAGACAGAGTCTCACTCTGTCGCCCAGGCTGGAGTG CAGTGGCGAGATCTCAGCTCACTGCAACCTCTGCTGCCCAGGTTCAAGGGATTCTCCTGCCTCAGCCTCC CAAGTAGCTGGTATTACAGGCACCTGCCACCGCGCCTGGCTAATTTTTTTTTTTTTTTTTTTTTGTATTT __EOD__ my $seqTitle = q{}; my $accumulator = q{}; my %sequences = (); while ( <$inFH> ) { chomp; if ( m{^>} ) { addSequence(); } else { $accumulator .= $_; } } addSequence(); close $inFH or die $!; say for sort keys %sequences; say q{-} x 50; print Data::Dumper ->new( [ \ %sequences ], [ qw{ *sequences } ] ) ->Sortkeys( 1 ) ->Dumpxs(); sub addSequence { $sequences{ $seqTitle } = $accumulator if $seqTitle; $seqTitle = $_; $accumulator = q{}; }

The output.

>AC067940.1 Homo sapiens clone RP11-818E9, LOW-PASS SEQUENCE SAMPLING >NM_001198855.1 Homo sapiens cytochrome P450 family 2 subfamily C memb +er 8 (CYP2C8) >NM_030643.4 Homo sapiens apolipoprotein L4 (APOL4) >NR_029834.1 Homo sapiens microRNA 200a (MIR200A), microRNA -------------------------------------------------- %sequences = ( '>AC067940.1 Homo sapiens clone RP11-818E9, LOW-PASS SE +QUENCE SAMPLING' => 'AAATACAACTTTAAATCAAAACGGTAAAAATTCCACTCTTTCATACTA +ACTTCAAAAGTATTTGCTTTAAAAAAAAAGNNNNNNNNNNAAACTGAATTTCTATTAAGCATCTATTTA +TAGAAGAGAGTAAACACCCCGTGAATAAAAGACAGAGAATTGTAGCAGCCCGAAGTCCCTTTTCTCTCC +TCCCAAGCATTTGGCTCTGGTCCAAATTCACATATCCTGCTCCGTAAAACAAAGTGCCTTGGTTAACCT +AACGTTATTCCTTGAACAGTAGTTTAGTGATCAACTAGTTTTTGTTGTTGTTGTTGTTTGAGACAGAGT +CTCACTCTGTCGCCCAGGCTGGAGTGCAGTGGCGAGATCTCAGCTCACTGCAACCTCTGCTGCCCAGGT +TCAAGGGATTCTCCTGCCTCAGCCTCCCAAGTAGCTGGTATTACAGGCACCTGCCACCGCGCCTGGCTA +ATTTTTTTTTTTTTTTTTTTTTGTATTT', '>NM_001198855.1 Homo sapiens cytochrome P450 family 2 +subfamily C member 8 (CYP2C8)' => 'ACATGTCAAAGAGACACACACTAAATTAGCAGGG +AGTGTTATAAAAACTTTGGAGTGCAAGCTCACAGCTGTCTTAATAAGAAGAGAAGGCTTCAATGGAACC +TTTTGTGGTCCTGGTGCTGTGTCTCTCTTTTATGCTTCTCTTTTCACTCTGGAGACAGAGCTGTAGGAG +AAGGAAGCTCCCTCCTGGCCCCACTCCTCTTCCTATTA', '>NM_030643.4 Homo sapiens apolipoprotein L4 (APOL4) ' +=> 'GAGGTGCTGGGGAGCAGCGTGTTTGCTGTGCTTGATTGTGAGCTGCTGGGAAGTTGTGACTTTCA +TTTTACCTTTCGAATTCCTGGGTATATCTTGGGGGCTGGAGGACGTGTCTGGTTATTATATAGGTGCAC +AGCTGGAGGTGAGATCCACACAGCTCAGACCAGCTGGATCTTGCTCAGTCTCTGTCAGAGGAAGATCCC +TTGGAGGAGGCCCCGCAGCGACATGGAGGGAGCTGCTTTGCTGAAAATCTTTGTCGTCTGCATCTGGAA +CCAAAATC', '>NR_029834.1 Homo sapiens microRNA 200a (MIR200A), mic +roRNA' => 'CCGGGCCCCTGTGAGCATCTTACCGGACAGTGCTGGATTTCCCAGCTTGACTCTAACA +CTGTCTGGTAACGATGTTCAAAGGTGACCCGC' );

This is all speculative. You may have a very good reason to keep all of the lines together as one element of an array but if you need to access individual sequences by name I think that a hash table is the way to go.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2020-05-29 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (169 votes). Check out past polls.

    Notices?