-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_phamdb.pl
More file actions
124 lines (114 loc) · 2.93 KB
/
Copy pathbuild_phamdb.pl
File metadata and controls
124 lines (114 loc) · 2.93 KB
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
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Fetch;
#IMPORTANT: Needs BLAST loaded in order to run.
MAIN();
sub MAIN{
GET_PHAMS();
my @phams = COMBINE_PHAMS();
my %paired_metadata = GET_METADATA(@phams);
APPLY_METADATA(\%paired_metadata);
BLAST_DB();
}
sub GET_PHAMS{
mkdir("pham_fastas");
system("wget http://databases.hatfull.org/Actino_Draft/fastas.zip");
system("unzip fastas.zip -d pham_fastas");
}
sub COMBINE_PHAMS{
#combine pham files
my @fastafiles = glob"pham_fastas/fastas/*.fasta";
my @pham_numbers;
open(OUT, "+> allphams.faa");
foreach my $infile (@fastafiles){
my($pham)=($infile=~/.*\/(.*?)\_.*/);
push(@pham_numbers,$pham);
open(IN, "< $infile");
while(<IN>){
chomp;
if($_=~/\>/){
my($phage,$cluster)=($_=~/\>(.*?)\ .*?\[cluster\=(.*?)\].*/);
$phage = REMOVE_SPECIAL_CHARACTERS($phage);
$cluster = REMOVE_SPECIAL_CHARACTERS($cluster);
print OUT ">$phage $cluster $pham\n";
}
else{
$_=~s/\ /\_/g;
$_=~s/\-//g;
print OUT "$_\n";
}
}
close IN
}
close OUT;
return(@pham_numbers);
}
sub GET_METADATA{
#get pham numbers and other data
my @phams_to_process = @_;
mkdir("pham_csvs");
my %products;
foreach my $pham (@phams_to_process){
my $url = 'https://phagesdb.org/phams/genelist/'.$pham;
my $ff = File::Fetch->new(uri => $url);
my $file = $ff->fetch(to => 'pham_csvs');
open(IN, "< $file") or die "Error. Check pham $pham OR file $file.\n";
my $counter=0;
while(<IN>){
chomp;
$counter++;
next if($counter <= 1);
my($product)=($_=~/.*\t(.*)/);
next if(!defined $product);
if($product eq "b\'\'"){
$product = "product_unkown";
}
$product=~s/b"//g;
$product=~s/b'//g;
$product=~s/'//g;
$product = REMOVE_SPECIAL_CHARACTERS($product);
$products{$pham}=$product;
last;
}
close IN;
}
return(%products);
}
sub APPLY_METADATA{
my $hash_ref = shift;
my %metadata = %{$hash_ref};
my $counter = 0;
#apply pham numbers to database
open(OUT, "+> temp.txt");
open(PHAM, "< allphams.faa");
while(<PHAM>){
if($_=~/\>/){
chomp;
my($phage,$cluster,$pham)=($_=~/\>(.*?)\ (.*?)\ (.*)/);
if(!exists $metadata{$pham} || !defined $metadata{$pham}){
print OUT ">$phage\_UID\_$counter\ $cluster\ $pham\ "." product_unkown\n"
}
else{
print OUT ">$phage\_UID\_$counter\ $cluster\ $pham\ "." $metadata{$pham}\t$counter\n";
}
$counter++;
}
else{
print OUT $_;
}
}
close PHAM;
close OUT;
unlink "allphams.faa";
rename "temp.txt", "allphams.faa";
}
sub REMOVE_SPECIAL_CHARACTERS{
my $input_string = shift;
$input_string=~s/[\|\ \[\]\(\)\:\;\/\.\-\~\`\!\@\#\$\%\^\&\*\=\+\{\}\?]/\_/g;
return($input_string);
}
sub BLAST_DB{
#make blastdatabse
system("makeblastdb -in allphams.faa -dbtype prot -parse_seqids");
}