Perl: GenBank Handling (File, Tabulate), glob (File Array) - by Eun Bae Kim (08/21/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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  
use strict;
use Bio::SeqIO;
use LWP::Simple;

my $iCnt = 0;
# URLs for target files. qw --> no need to use quatation marks.
my @aUrls = qw (
	http://lab.gutdesigner.com/research_perl_data/AGRI01.1.gbk
	http://lab.gutdesigner.com/research_perl_data/ASJE01.1.gbk
	http://lab.gutdesigner.com/research_perl_data/AVAQ01.1.gbk
);

my $sDownFolder = "Download_GenBank";
system("mkdir ".$sDownFolder);
print "--------------------------------------------\n";
foreach my $sCurUrl (@aUrls) {
	$iCnt++;
	my $sLocalFile = $sDownFolder."/".funcFileNameFromPath($sCurUrl);
	print "Target ".$iCnt."\t".$sLocalFile."\n";

	my $oRespond = getstore($sCurUrl, $sLocalFile);    # If a url is not correct, no exception occurs.
	if (is_error($oRespond)) {
		print "    Downloading failed: ".$sCurUrl."\n";
		print "    Error Message: ".$oRespond."\n";
	}
}

sub funcFileNameFromPath {
	my $sFilePath = shift;
	my $sRevPath = reverse($sFilePath);
	my $sFileName = "";

	if ($sRevPath=~/\//){
		$sFileName = $`;
		$sFileName = reverse($sFileName);
	} else {
		$sFileName = $sFilePath;
	}
	return $sFileName;
}





# Loading each GenBank file one by one
my @aGenBankFiles = glob($sDownFolder."/*.gbk");
$iCnt = 0;

print "--------------------------------------------\n";
foreach my $sCurGbFile (@aGenBankFiles) {
	$iCnt++;
	my $sCnt = sprintf("%04d",$iCnt);
	print "Processing ".$iCnt.": ".$sCurGbFile."\n";
	funcGenBankProcessing($sCurGbFile, $sDownFolder);
} 
print "--------------------------------------------\n";
print "Finished!!\n";



sub funcGenBankProcessing {
	my $sGenBankFile = shift;
	my $sSaveFolder  = shift;
	my $sSaveFile    = $sSaveFolder."/".funcFileNameFromPath($sGenBankFile)."_Table.txt";

	open (hOut, ">".$sSaveFile) or die;
	print "    Saving: ".$sSaveFile."\n";
	my $oGbk = Bio::SeqIO->new(-file => $sGenBankFile, -format => 'genbank');

	print hOut "Strains"        ."\t";
	print hOut "Seq_IDs"        ."\t";
	print hOut "Description"    ."\t";
	print hOut "F\/R"           ."\t";
	print hOut "Symbols"        ."\t";
	print hOut "Products"       ."\t";
	print hOut "Protein_IDs"    ."\t";
	print hOut "A.A"            ."\t";
	print hOut "CDS_Length(bp)" ."\t";
	print hOut "DNA_Seq"        ."\t";
	print hOut "X_Ref"          ."\n";

	while(my $oSeq = $oGbk->next_seq()) {
		my @aStrain  = ();
		my $sCdsInfo = "";
		my $sSeqId   = "";
		my $sSeqdesc = "";

		foreach my $oCurFeat ( $oSeq->get_SeqFeatures() ) {
			# Get the Strain information
			if ($oCurFeat->primary_tag eq "source") {    
				##############################################################
				eval {
					@aStrain = $oCurFeat->get_tag_values("strain");
				};
				if ($@) {
					print $@."\n";
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aStrain[0] = "None";			
					}
				}
			}

			# Get a sequence ID for this sequence
			eval {
				$sSeqId = $oSeq->display_id();
			};
			if ($@) {
				if ($@ =~ m/asking for tag value that does not exist/) {
					$sSeqId = "None";
				}
			}

			# Get a sequence description for this sequence
			eval {
				$sSeqdesc = $oSeq->desc();
			};
			if ($@) {
				if ($@ =~ m/asking for tag value that does not exist/) {
					$sSeqdesc = "None";
				}
			}


			# Get each CDS information
			if ($oCurFeat->primary_tag eq "CDS") {
				$sCdsInfo = "";

				my $sSeqFinal     = "";
				my $sOrient       = "";
				my @aGeneName     = ();
				my @aProduct      = ();
				my @aProtId       = ();
				my @aAminoAcid    = ();
				my @aDbXrefs      = ();

				# Get a sequence for this CDS
				$sSeqFinal        = $oSeq->subseq($oCurFeat->start, $oCurFeat->end);
				if ($oCurFeat->strand == 1) {
					$sOrient = "+";
				} else {
					$sOrient = "-";
					$sSeqFinal = funcRevComp($sSeqFinal);
				}

				# Get a gene symbol or gene name for this CDS
				eval {	
					@aGeneName = $oCurFeat->get_tag_values("gene");
				};
				if ($@) {
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aGeneName[0]="None";
					}
				}

				# Get a product name for this CDS
				eval {
					@aProduct = $oCurFeat->get_tag_values("product");
				};
				if ($@) {
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aProduct[0]="None";
					}
				}

				# Get a protein ID for this CDS
				eval {#protein number id : 
					@aProtId = $oCurFeat->get_tag_values("protein_id");
				};
				if ($@) {
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aProtId[0]="None";
					}
				}

				# Get an amino acid sequence for this CDS
				eval {
					@aAminoAcid	= $oCurFeat->get_tag_values("translation");
				};
				if ($@) {
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aAminoAcid[0]="None";
					}
				}

				# Get external database IDs for this CDS
				eval {
					@aDbXrefs = $oCurFeat->get_tag_values("db_xref");
				};
				if ($@) {
					if ($@ =~ m/asking for tag value that does not exist/) {
						$aDbXrefs[0]="None";
					}
				}
				my $sDbXrefs = join(" \/ ", @aDbXrefs);

				$sCdsInfo = $sCdsInfo.$aStrain[0]        ."\t";
				$sCdsInfo = $sCdsInfo.$sSeqId            ."\t";
				$sCdsInfo = $sCdsInfo.$sSeqdesc          ."\t";
				$sCdsInfo = $sCdsInfo.$sOrient           ."\t";
				$sCdsInfo = $sCdsInfo.$aGeneName[0]      ."\t";
				$sCdsInfo = $sCdsInfo.$aProduct[0]       ."\t";
				$sCdsInfo = $sCdsInfo.$aProtId[0]        ."\t";
				$sCdsInfo = $sCdsInfo.$aAminoAcid[0]     ."\t";
				$sCdsInfo = $sCdsInfo.length($sSeqFinal) ."\t";
				$sCdsInfo = $sCdsInfo.$sSeqFinal         ."\t";
				$sCdsInfo = $sCdsInfo.$sDbXrefs;

				print hOut $sCdsInfo."\n";
			}
		}
	}
	close(hOut);
}

sub funcRevComp {
	my $sSeq = reverse uc($_[0]);
	$sSeq =~ tr/ATGCN/TACGN/;                     # A -> T, T ->A, G -> C, C -> G, N -> N
	return $sSeq;	
}



# For more information, refer to the following link
# https://www.tutorialspoint.com/perl
# https://metacpan.org/pod/Bio::DB::GenBank