This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Parsing Fasta Sequence From File

Hello,

I am using perl and not bioperl because my professor has not shown us how yet. I have to extract a FASTA sequence attached to a user accession number input. E.g, If the user enters YP_02030404.1, the script should get the sequence attached to it. like this:

>gi|170079675|ref|YP_001728995.1|  [Escherichia coli str. K-12 substr. DH10B]   
MRVSWLESKCDTPFANNLSFISSGSSSSSSFTLASTACRNSCLCSSSIFFQVLRRNCSSNCCSISNVDIS  
LSAFSFNRFETSSKMARYNLPCPRSLLAILSPPKCCNSPAISCQLRRCCSGCPSIDLNSSLRISTLERRV 
LPFSLWVSNRAKFANCSSLQC
fasta sequence homework

And where is this sequence to be "extracted" from? A local database? A remote database? I suggest you use your initiative and started reading the Bioperl HOW-TOs: http://www.bioperl.org/wiki/HOWTOs. What you want to do is very easy.

Can you only do what your prof. has shown you, or are you an independent mind?

2 answers

so you are asking us to do your homework? strange place to ask for it. but if you are really trying to learn perl, and as I hate discouraging people from starting off, then I'll suggest you try learning

  1. how to accept text input
  2. how to read lines
  3. how to store data in hashes (the accession line would be the key, the concatenated-lines-sequence would be the value), and finally
  4. how to iterate through hash keys doing some basic pattern matching.

so if accession entered matches any of the hash keys, then you return its value. this is the answer to your question, although you'll have to find out how to build up these 3 steps using perl.

or sorry..i have code down..im just struggling..

!/usr/bin/perl -w

use strict;

my $filename; my $accessionid; my $sequenceid;

print "Please enter in the filename: "; chomp($filename = <STDIN>);

print "Please enter in the accessionID: "; chomp($accessionid = <STDIN>);

open(FILENAME, $filename) or die "cannot open file: $! "; while(<FILENAME>){ if($_ =~ /$accessionid/i) { $sequenceid = $; print "$sequenceid\n"; } my $seq = ''; foreach($) { last if ($ =~ />gi/);

print "$seq\n"; } }

i have code..sorry!!

!/usr/bin/perl -w

use strict;

my $filename; my $accessionid; my $sequenceid;

print "Please enter in the filename: "; chomp($filename = <STDIN>);

print "Please enter in the accessionID: "; chomp($accessionid = <STDIN>);

open(FILENAME, $filename) or die "cannot open file: $! "; while(<FILENAME>){ if($_ =~ /$accessionid/i) { $sequenceid = $; print "$sequenceid\n"; } my $seq = ''; foreach($) { last if ($ =~ />gi/);

print "$seq\n"; } }

!/usr/bin/perl -w

use strict;

my $filename; my $accessionid; my $sequenceid;

print "Please enter in the filename: "; chomp($filename = <STDIN>);

print "Please enter in the accessionID: "; chomp($accessionid = <STDIN>);

open(FILENAME, $filename) or die "cannot open file: $! ";

while(<FILENAME>){

if($_ =~ /$accessionid/i) { $sequenceid = $; print "$sequenceid\n"; } my $seq = '';

foreach($) { last if ($ =~ />gi/);

print "$seq\n"; } }

i have code sorry..

#!/usr/bin/perl -w
use strict;

my $filename;
my $accession_id;
my $sequence_id;

print "Please enter in the filename: ";
chomp($filename = <STDIN>);
print "Please enter in the accession_ID: ";
chomp($accession_id = <STDIN>);

open(FILENAME, $filename) or die "cannot open file: $! ";
while(<FILENAME>){
  if($_ =~ /$accession_id/i) {
  $sequence_id = $_;
  print "$sequence_id\n";
}

my $seq = '';
foreach($_) {
  last if ($_ =~ />gi/);
  print "$seq\n";
}
}

Please indent code using 4 spaces to format it properly. I did it for you this time. Also, is this your answer? If not, remove it and add the code to your original question.

I'm so sorry. This is my first time on here. I apologize. Yes, that is my code. i posted.

Log in to answer this question.