sumankhanal

Read fasta in raku

Jul 7th, 2020
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.77 KB | None | 0 0
  1. # This is fasta file https://raw.githubusercontent.com/sumanstats/Coding-Biology-in-Raku/master/data/GCF_000006945.2_ASM694v2_genomic.fna
  2. # has a single line with >
  3. # If it has more > I wish to store the line with > as key and sequence below as string in a hash table
  4. # This is the function I wrote
  5.  
  6. # Any faster alternative?
  7.  
  8.  
  9. sub open_and_parse_fasta($filepath) {
  10.     my @lines = $filepath.IO.lines;
  11.     my %FASTA_hash;
  12.     my $FASTA_label = "";
  13.     for @lines {
  14.         my $line = $_.trim-trailing;
  15.         if $line.starts-with(">") {
  16.             $FASTA_label = $line.substr(1,);
  17.             %FASTA_hash{$FASTA_label} = "";
  18.         } else {
  19.             %FASTA_hash{$FASTA_label} = %FASTA_hash{$FASTA_label} ~ $line;
  20.         }
  21.     }
  22.     return %FASTA_hash  
  23. }
  24.  
  25. say open_and_parse_fasta('GCF_000006945.2_ASM694v2_genomic.fna').keys
  26.  
  27. # It takes 9 secs in my machine windows 10 !
Add Comment
Please, Sign In to add comment