shabird has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! i have a file that look like this:

LOCUS PPI3_SYNY3 379 aa linear BCT 1 +3-NOV-2019 DEFINITION RecName: Full=Putative thylakoid lumen peptidyl-prolyl cis +-trans isomerase sll0408; Short=Putative thylakoid lumen PPIase; +AltName: Full=Putative thylakoid lumen rotamase; Flags: Precursor. ACCESSION Q55118 VERSION Q55118.2 REFERENCE 1 (residues 1 to 379) AUTHORS Kaneko,T., Sato,S., Kotani,H., Tanaka,A., Asamizu,E., Naka +mura,Y., Miyajima,N., Hirosawa,M., Sugiura,M., Sasamoto,S., Kimura, +T., Hosouchi,T., Matsuno,A., Muraki,A., Nakazaki,N., Naruo,K., Okumura,S., Shimpo,S., Takeuchi,C., Wada,T., Watanabe,A., Yamada,M., Yasuda,M. and Tabata,S. TITLE Sequence analysis of the genome of the unicellular cyano +bacterium Synechocystis sp. strain PCC6803. II. Sequence determinati +on of the entire genome and assignment of potential protein-coding r +egions JOURNAL DNA Res. 3 (3), 109-136 (1996) PUBMED 8905231 REMARK NUCLEOTIDE SEQUENCE [LARGE SCALE GENOMIC DNA].; STRAIN=PCC 6803 / Kazusa REFERENCE 2 (residues 1 to 379)

I want to extract only these information from the file "PPI3_SYNY3 " , "Putative thylakoid lumen peptidyl-prolyl cis-trans isomerase sll0408" and "Sequence analysis of the genome of the unicellular cyanobacterium Synechocystis sp. strain PCC6803. II. Sequence determination of the entire genome and assignment of potential protein-coding regions" Here is my code where i am only able to extract the locus id from the file please help me here

open(GENBANK, "/Users/ale/Desktop/seq.gp.txt") or die; # Opening file results.txt in write mode with the help of ">" operator +. open (FH, ">", "results.txt"); my $content = join("", <GENBANK>); close(GENBANK); $content =~ /LOCUS\s+([A-Z0-9_]+)/; my $locus = $1; $content =~ /DEFINITION\s+(\w+\W+); my $acids = $1 ; $content =~ /TITLE([a-z0-9+\s]+)/; my $aminoAcids = uc($1); print(FH "$locus\n$acids\n$aminoAcids\n");

Replies are listed 'Best First'.
Re: advance Regex
by hippo (Archbishop) on Apr 28, 2020 at 12:49 UTC

    Your second match has no captures and your third only matches lowercase. Regex might not be the best option for parsing this format in a robust fashion. That said:

    #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 3; my $content = join '', <DATA>; $content =~ /LOCUS\s+([A-Z0-9_]+)/; my $locus = $1; $content =~ /(Putative.*?);/s; my $acids = $1; $acids =~ s/\n\s+/ /g; $content =~ /TITLE\s*(.*?)\n\s+JOURNAL/s; my $aminoAcids = $1; $aminoAcids =~ s/\n\s+/ /g; is $locus, 'PPI3_SYNY3'; is $acids, 'Putative thylakoid lumen peptidyl-prolyl cis-trans isomera +se sll0408'; is $aminoAcids, 'Sequence analysis of the genome of the unicellular cy +anobacterium Synechocystis sp. strain PCC6803. II. Sequence determina +tion of the entire genome and assignment of potential protein-coding +regions'; __DATA__ LOCUS PPI3_SYNY3 379 aa linear BCT 1 +3-NOV-2019 DEFINITION RecName: Full=Putative thylakoid lumen peptidyl-prolyl cis +-trans isomerase sll0408; Short=Putative thylakoid lumen PPIase; +AltName: Full=Putative thylakoid lumen rotamase; Flags: Precursor. ACCESSION Q55118 VERSION Q55118.2 REFERENCE 1 (residues 1 to 379) AUTHORS Kaneko,T., Sato,S., Kotani,H., Tanaka,A., Asamizu,E., Naka +mura,Y., Miyajima,N., Hirosawa,M., Sugiura,M., Sasamoto,S., Kimura, +T., Hosouchi,T., Matsuno,A., Muraki,A., Nakazaki,N., Naruo,K., Okumura,S., Shimpo,S., Takeuchi,C., Wada,T., Watanabe,A., Yamada,M., Yasuda,M. and Tabata,S. TITLE Sequence analysis of the genome of the unicellular cyano +bacterium Synechocystis sp. strain PCC6803. II. Sequence determinati +on of the entire genome and assignment of potential protein-coding r +egions JOURNAL DNA Res. 3 (3), 109-136 (1996) PUBMED 8905231 REMARK NUCLEOTIDE SEQUENCE [LARGE SCALE GENOMIC DNA].; STRAIN=PCC 6803 / Kazusa REFERENCE 2 (residues 1 to 379)

      Works fantastic. but can you explain the second and third regex to me? it will be your huge favour on me. this one:

      $content =~ /(Putative.*?);/s; my $acids = $1; $acids =~ s/\n\s+/ /g;

      and this one

      $content =~ /TITLE\s*(.*?)\n\s+JOURNAL/s; my $aminoAcids = $1; $aminoAcids =~ s/\n\s+/ /g;

        The first one says to capture everything from the first P of Putative up to but not including the first following semi-colon. Subsequently the whitespace is collapsed.

        The second one says to capture everything after "TITLE" and its trailing space(s) up to but not including a newline followed by whitespace followed by "JOURNAL" (which is the following key). Again, subsequently the whitespace is collapsed.

        Both of these require the /s modifier. See perldoc perlre for details of the modifiers. You can also fall back on YAPE::Regex::Explain for any regexen that you are having trouble understanding, although it doesn't include support for more modern features, alas.