2

I have a file given below:

>AAF88103.1 zinc finger protein 226 [Homo sapiens]
MNMFKEAVTFKDVAVAFTEEELGLLGPAXRKLYRDVMVENFRNLLSVGHPPFKQDVSPIERNEQLWIMTT
ATRRQGNLGEKNQSKLITVQDRESEEELSCWQIWQQIANDLTRCQDSMINNSQCHKQGDFPYQVGTELSI
QISEDENYIVNKADGPNNTGNPEFPILRTQDSWRKTFLTESQRLNRDQQISIKNKLCQCKKGVDPIGWIS
HHDGHRVHKSEKSYRPNDYEKDNMKILTFDHNSMIHTGQKSYQCNECKKPFSDLSSFDLHQQLQSGEKSL
TCVERGKGFCYSPVLPVHQKVHVGEKLKCDECGKEFSQGAHLQTHQKVHVIEKPYKCKQCGKGFSRRSAL
NVHCKVHTAEKPYNCEECGRAFSQASHLQDHQRLHTGEKPFKCDACGKSFSRNSHLQSHQRVHTGEKPYK
CEECGKGFICSSNLYIHQRVHTGEKPYKCEECGKGFSRPSSLQAHQGVHTGEKSYICTVCGKGFTLSSNL
QAHQRVHTGEKPYKCNECGKSFRRNSHYQVHLVVHTGEKPYKCEICGKGFSQSSYLQIHQKAHSIEKPFK
CEECGQGFNQSSRLQIHQLIHTGEKPYKCEECGKGFSRRADLKIHCRIHTGEKPYNCEECGKVFRQASNL
LAHQRVHSGEKPFKCEECGKSFGRSAHLQAHQKVHTGDKPYKCDECGKGFKWSLNLDMHQRVHTGEKPYK
CGECGKYFSQASSLQLHQSVHTGEKPYKCDVCGKVFSRSSQLQSHQRVHTGEKPYKCEICGKSFSWRSNL
TVHHRIHVGDKSYKSNRGGKNIRESTQEKKSIK.

In this file i am trying to look for a sequence i.e: CDECGKEFSQGAHLQTHQKVH I have to hardcode this pattern in the program and then look for it, my code is as follows

    open FILE1, "file.fasta" or die;

while (my $line= <FILE1>) {

chomp $line;

}

if ($line =~ /CDECGKEFSQGAHLQTHQKVH/) {
    print "The protein contains the domain";
}else{
    print "The protein doesn't contain the domain";
}

Now this pattern occurs in the sequence but i always get the message "The protein doesn't contain the domain". Am i doing it wrong?

1
  • Crossposted to PerlMonks. Commented Apr 14, 2020 at 14:24

1 Answer 1

3

The $line evaluation occurs outside of the loop... Give this a try:

my $found = 0;
open(my $fh, '<', 'file.fasta') or die "Could not open file '$filename': $!";
while (my $line = <$fh>) {
    if ($line =~ /CDECGKEFSQGAHLQTHQKVH/) {
        $found = 1;
        last;
    }
}

if ($found == 1) {
    print "The protein contains the domain";
} else {
    print "The protein doesn't contain the domain";
}
Sign up to request clarification or add additional context in comments.

5 Comments

my $found = 0;
fixed... in my defence it's been 5 years since I last played find the missing semicolon... Thanks!
Please also check Modern Perl page 139 for usage of open.
Thank you it does what i want but in your opinion is it a good idea to pass such a long string in regex pattern?
@perly this does not consume the entire file all at once. It consumes the contents of the file line by line. See "Reading Files as Iterator' in devglan.com/python/file-handling-in-python for an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.