Perl: Fasta Handling (Bio::SeqIO) - by Eun Bae Kim (10/29/2018)
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  
use strict;
use Bio::SeqIO;

# Making a file in the Fasta Format.
my $sFastaFile = "Fasta_Example.fasta";

open(hOut, ">".$sFastaFile) or die;
print hOut ">Seq1 Example Sequence 1\n";
print hOut "ACTGCATGAGTCGTAGTGTCGTACTATCGATGCAGTAGACTGATCGATCGTTACATTCAT\n";
print hOut "TCAGACTGTAGACAAAAAGGAGTTGGCAATCAAACCAGCTCCTTTTTACTATCCTTTGTG\n";
print hOut "TAAATTTGGAACAAAAAGC\n\n";
print hOut ">Seq2 Example Sequence 2\n";
print hOut "AACAAAGCGATTACACTGTTTAGTTTACTAACAGTCCTAATCGCTTTTTTTATTTCCCCT\n";
print hOut "ATTACTACATTCGCTGCTACAGTAAATTATGAAAAAGTAGCGAACTATATTTCCACATGG\n";
print hOut "CATGTTAAGTCATTAGGCGGTTTACACTGGACTGATGAAGGCATCCATATGATTAAAGCG\n\n";
print hOut ">Seq3 Example Sequence 3\n";
print hOut "ATGGAACACTTCTTACTGGTGGTTCTGGATTCACCCCATCTGAACTAACTATTGCAGA\n";
close(hOut);


# Reference: # https://metacpan.org/pod/Bio::SeqIO

my $oFasta  = Bio::SeqIO->new(-file => $sFastaFile, -format => 'fasta');	
my $iSeqCnt = 0;

print "--------------------------------------------\n";
print "Target File: ".$sFastaFile."\n";
print "--------------------------------------------\n";
while( my $oSeq = $oFasta->next_seq() ) {
	$iSeqCnt++;

	my $sSeqId 	= $oSeq->display_id();            # Sequence ID
	my $sDesc	= $oSeq->desc();                  # Sequence Description
	my $sSeq	= $oSeq->seq();                   # Sequence

	print "Sequenc Cnt     : ".$iSeqCnt."\n";
	print "Seq ID          : ".$sSeqId. "\n";
	print "Seq Description : ".$sDesc.  "\n";
	print "Seq             : ".$sSeq.   "\n";
	print "-----------------------------------\n";
}