dimanche 29 mars 2020

How to extract IDs from a matched string in FASTA files using Perl script?

I am new in Perl programming and stuck with my script.

I am trying to search for a motif in a FASTA file and if found print out the ID of the proteins containing it.

I can load my file but after putting my motif, nothing happens. I get the following error: Use of uninitialized value $data[0] in concatenation (.) or string at test.pl line 36, line 2.

Here is my code:

#!/usr/bin/perl -w
# Searching for motifs
print "Please type the filename of the protein sequence data: ";
$proteinfilename = <STDIN>;
# Remove the newline from the protein filename
chomp $proteinfilename;
# open the file, or exit
unless ( open(FA, $proteinfilename) ) {
print "Cannot open file \"$proteinfilename\"\n\n";
exit;
}
@protein = <FA>; # Read the protein sequence data from the file, and store it into the array variable @protein

my (@description, @ID, @data);

while (my $protein = <FA>) {
    chomp($protein);
    @description = split (/\s+/, $protein);
    push (@ID, $description[0]);   
}
# Close the file 
close FA;
my %params = map { $_ => 1 } @ID;
# Put the protein sequence data into a single string, as it's easier to search for a motif in a string than in an array of lines
$protein = join( '', @protein);
# Remove whitespace
$protein =~ s/\s//g;

# ask for a motif or exit if no motif is entered.
do {
print "Enter a motif to search for: ";
$motif = <STDIN>;
# Remove the newline at the end of $motif
chomp $motif;

# Look for the motif
@data = split (/\s+/, $protein);

if ( $protein =~ /$motif/ ) {
    print $description[0]."\n" if(exists($params{$data[0]}));
}
# exit on an empty user input
} until ( $motif =~ /^\s*$/ );
# exit the program
exit;

The example of the input is:

sp|O60341|KDM1A_HUMAN Lysine-specific histone demethylase 1A OS=Homo sapiens OX=9606 GN=KDM1A PE=1 SV=2 MLSGKKAAAAAAAAAAAATGTEAGPGTAGGSENGSEVAAQPAGLSGPAEVGPGAVGERTP RKKEPPRASPPGGLAEPPGSAGPQAGPTVVPGSATPMETGIAETPEGRRTSRRKRAKVEY

Let's say I would like to find motif 'PMET' in a given sequence. If it exists, I want to get an ID as an output -> O60341

Thank you very much in advance!

Any feedback very much appreciated!

Aucun commentaire:

Enregistrer un commentaire