I am very new to perl, and have been trying to get this code right, but I am completely lost. I have tried a few variations but have no idea where I am going wrong or what I need to do. I currently get no error messages of any sort. The program is supposed ot read a fasta file, searching for the sequence VILMFWCA, and the output is the header that contains that sequence and then the sequence and location. Here is what I have

#!/usr/bin/perl use strict; use warnings; use diagnostics; unless (open (INFILE, "<", "/scratch/SampleDataFiles/test.fasta")){ die "Unable to open file", $!; } local $/ = ">"; #find and print desired sequence while (<INFILE>) { chomp; #always chomp if ( $_ =~ /^(.*?)$(.*)$/ms ) { #match first line as h +eader my $header = $1; #assign the parts of the matc +h my $seq = $2; $seq =~ s/\n//g; #get rid of whitespace while($seq =~ /([VILMFWCA]{8,})/g){ #s +earch for desired sequence my $location = pos($seq); #fin +d location my $length = length($1 +); #determine length print "Hydrophobic str +etch found in: ", $header, "\n"; #printing outputs for results print $1, "\n"; print "The match was at positi +on: ", $location - $length + 1, "\n\n"; } } } close INFILE;

updates with where I am at now, but now I am getting too much output, every time it finds the stretch it prints the header instead of once per header, I tried moving the print command up but it didn't do it. The output is supposed to look like this:

Hydrophobic stretch found in: P30450 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 365 | Name=HLA-A; Synonyms=HLAA; AVVAAVMW The match was at position: 325 Hydrophobic stretch found in: A7MBM2 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 1401 | Name=DISP2; Synonyms=DISPB, KIAA1742; VAVLMLCLAVIFLC The match was at position: 170 LLALVAIFF The match was at position: 493 IWICWFAALAA The match was at position: 705 LALALAFA The match was at position: 970

but my current output is like this:

Hydrophobic stretch found in: P30450 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 365 | Name=HLA-A; Synonyms=HLAA; AVVAAVMW The match was at position: 325 Hydrophobic stretch found in: A7MBM2 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 1401 | Name=DISP2; Synonyms=DISPB, KIAA1742; VAVLMLCLAVIFLC The match was at position: 170 Hydrophobic stretch found in: A7MBM2 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 1401 | Name=DISP2; Synonyms=DISPB, KIAA1742; LLALVAIFF The match was at position: 493 Hydrophobic stretch found in: A7MBM2 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 1401 | Name=DISP2; Synonyms=DISPB, KIAA1742; IWICWFAALAA The match was at position: 705 Hydrophobic stretch found in: A7MBM2 | Homo sapiens (Human). | NCBI_Ta +xID=9606; | 1401 | Name=DISP2; Synonyms=DISPB, KIAA1742; LALALAFA The match was at position: 970

I'm so close


In reply to Where am I going wrong? by lairel

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.