Hello Perl Monks, The same question was asked here:

http://stackoverflow.com/questions/40414018/perl-program-to-look-for-k-mer-with-specific-sequence

but seems like it wasn't answered correctly or wasn't finished.

I need to build upon a previous program that I've written and find the first 1000 unique, k-mers of length 23 that end with GG from a Fasta file.

The first 20 lines of the Fasta file look like this:
> >2L type=chromosome_arm; loc=2L:1..23011544; ID=2L; dbxref=REFSEQ: +NT_033779,GB:AE014134; MD5=bfdfb99d39fa5174dae1e2ecd8a231cd; length=2 +3011544; release=r5.54; species=Dmel; CGACAATGCACGACAGAGGAAGCAGAACAGATATTTAGATTGCCTCTCAT TTTCTCTCCCATATTATAGGGAGAAATATGATCGCGTATGCGAGAGTAGT GCCAACATATTGTGCTCTTTGATTTTTTGGCAACCCAAAATGGTGGCGGA TGAACGAGATGATAATATATTCAAGTTGCCGCTAATCAGAAATAAATTCA TTGCAACGTTAAATACAGCACAATATATGATCGCGTATGCGAGAGTAGTG CCAACATATTGTGCTAATGAGTGCCTCTCGTTCTCTGTCTTATATTACCG CAAACCCAAAAAGACAATACACGACAGAGAGAGAGAGCAGCGGAGATATT TAGATTGCCTATTAAATATGATCGCGTATGCGAGAGTAGTGCCAACATAT TGTGCTCTCTATATAATGACTGCCTCTCATTCTGTCTTATTTTACCGCAA ACCCAAATCGACAATGCACGACAGAGGAAGCAGAACAGATATTTAGATTG CCTCTCATTTTCTCTCCCATATTATAGGGAGAAATATGATCGCGTATGCG AGAGTAGTGCCAACATATTGTGCTCTTTGATTTTTTGGCAACCCAAAATG GTGGCGGATGAACGAGATGATAATATATTCAAGTTGCCGCTAATCAGAAA TAAATTCATTGCAACGTTAAATACAGCACAATATATGATCGCGTATGCGA GAGTAGTGCCAACATATTGTGCTAATGAGTGCCTCTCGTTCTCTGTCTTA TATTACCGCAAACCCAAAAAGACAATACACGACAGAGAGAGAGAGCAGCG GAGATATTTAGATTGCCTATTAAATATGATCGCGTATGCGAGAGTAGTGC CAACATATTGTGCTCTCTATATAATGACTGCCTCTCATTCTGTCTTATTT TACCGCAAACCCAAATCGACAATGCACGACAGAGGAAGCAGAACAGATAT
And the expected outcome should be in this format:
>crispr_1 GGGTGGAGCTCCCGAAATGCAGG >crispr_2 TTAATAAATATTGACACAGCGGG >crispr_3 ATCGTGGGGCGTTTTGTGAAAGG >crispr_4 AGTTTTTCACATAATCAGACAGG >crispr_5 GTGTTGGATGAGTGTCCTCTGGG >crispr_6 ATAGGTTGGTTGTTTTAAAAGGG >crispr_7 AAATTTTTGTTGCCACTGAATGG >crispr_8 AAGTTTCGAACTACGATGGTTGG >crispr_9 CATGCTTTGTGGAAATAAGTCGG >crispr_10 CACAGTGGGTGTTTGCACCTCGG .....and so on
It should be the first 1000 unique sequences.

This is what I have so far:
#!/usr/bin/perl use warnings; use strict; my %windowSeqCount = (); my $sequenceRef = loadSequence("input.fasta"); #writing to a new file open (UNIQUEKMERS,">",output.fasta') or die $!; my $windowSize = 23; my $stepSize = 1; for( my $windowStart = 0 ; $windowStart <= ( length($$sequenceRef) - $windowSize ); $windowStart += $stepSize ) { my $windowSeq = substr ( $$sequenceRef, $windowStart, $windowSize); $windowSeqCount{$windowSeq}++ if $windowSeq =~ /GG$/; } for (keys %windowSeqCount){ print UNIQUEKMERS $_, "\t", $windowSeqCount{$_}, "\n"; } sub loadSequence { my ($sequenceFile) = @_; my $sequence = ""; unless ( open( FASTA, "<", $sequenceFile ) ) { die $!; } while (<FASTA>){ my $line = $_; chomp ($line); if ($line !~ /^>/ ) { $sequence .= $line; } } return \$sequence; }
I know my current print section is incorrect and that it will find 23 length sequences ending with GG and store them in a hash with the number of occurences as values.
I can't seem to figure what I need to include in my code to find just the first 1000 unique sequences of length 23 ending in GG in the expected format.
Any help would be greatly appreciated!! Thank you!!!

In reply to program to look for specific K-mer sequence by pearllearner315

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.