-
Notifications
You must be signed in to change notification settings - Fork 0
/
builddb.pl
151 lines (132 loc) · 4.33 KB
/
builddb.pl
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
#!/usr/bin/perl -w
use DBI;
use Bio::DB::Taxonomy;
use Data::Dumper;
#abre arquivos de taxonomia
my $idx_dir = '/home/user/taxonomy/idx';
my $db = new Bio::DB::Taxonomy(-source => 'flatfile',
-nodesfile => '/home/user/taxonomy/nodes.dmp',
-namesfile => '/home/user/taxonomy/names.dmp',
-directory => $idx_dir);
#Abre o arquivo fasta para consulta
use Bio::SeqIO;
my $usage = "builddb.pl file format\n";
my $file = shift or die $usage;
my $format = shift or die $usage;
my $inseq = Bio::SeqIO->new(-file => "<$file",
-format => $format );
#Arquivo para gravar
my $seq_out = Bio::SeqIO->new('-file' => ">blast4.fasta",
'-format' => fasta);
#Conecta ao banco de dados SQLite
$dbh = DBI->connect("dbi:SQLite:dbname=/home/user/taxonomy/taxonomy.db", "", "",
{ RaiseError => 1, AutoCommit => 0 });
sub classifica
{
my ($a,$seq) = @_;
my $node = $db->get_Taxonomy_Node(-taxonid => $a);
# Se tem tax id no banco
if ($node)
{
#se sim
#print $node->rank,"\t", $node->scientific_name, "\n";
#Marca o primeiro parent_id
$parent = $node->parent_id;
#faz looping até final da classificação
while( (defined $parent) or ($node->rank eq "class")) {
my $node = $db->get_Taxonomy_Node(-taxonid => $parent);
################################################################################################
#
# Procura pelos taxons desejados
#
################################################################################################
if (!$node->rank eq "")
{
if (($node->rank eq "superkingdom") && ($node->scientific_name eq "Bacteria"))
{
#print $node->rank,"\t", $node->scientific_name, "\n";
# grava no texto
$seq_out->write_seq($seq);
###Coloca no array para contagem
push(@in, $node->scientific_name);
} elsif (($node->rank eq "kingdom") && ($node->scientific_name eq "Viridiplantae"))
{
#print $node->rank,"\t", $node->scientific_name, "\n";
# grava no texto
$seq_out->write_seq($seq);
###Coloca no array para contagem
push(@in, $node->scientific_name);
} elsif (($node->rank eq "kingdom") && ($node->scientific_name eq "Fungi"))
{
#print $node->rank,"\t", $node->scientific_name, "\n";
# grava no texto
$seq_out->write_seq($seq);
###Coloca no array para contagem
push(@in, $node->scientific_name);
} elsif (($node->rank eq "superkingdom") && ($node->scientific_name eq "Archaea"))
{
#print $node->rank,"\t", $node->scientific_name, "\n";
# grava no texto
$seq_out->write_seq($seq);
###Coloca no array para contagem
push(@in, $node->scientific_name);
}
}
################################################################################################
#
# Final Procura pelos taxons desejados
#
################################################################################################
$parent = $node->parent_id;
}
#Se não tem no banco de taxid
} else {
print "\nNão TEM\n";
}
}
#variável da barra de progresso
$anda = 1;
$conta = 0;
while (my $seq = $inseq->next_seq) { #abre loop dentro do arquivo fasta
print ".";
#separa GI
@vai = split('\|+', $seq->id);
# print ">",$vai[1],"\n";
#Checa no banco de dados gi2tax e taxonimia
my $all = $dbh->selectall_arrayref("SELECT * FROM gi2tax WHERE gi = $vai[1]");
foreach my $row (@$all) {
my ($gi, $taxid) = @$row;
# print "$gi refere ao taxid $taxid\n";
&classifica($taxid,$seq);
$anda++;
$conta++;
if ($anda eq 51) {
$anda = 1;
$nova = $conta+1;
print "$conta \n $nova";
}
}
} #Fecha loop dentro do arquivo
print "\nTerminado!!!\n";
####### Mostra a contagem
#################################################################################
#
# Gera o array para contagem, coloca os dados no array com o mesmo nome
#
#################################################################################
foreach $names(@in) {
push(@$names, $names);
}
#################################################################################
#
# Gera o array unico e faz a contagem
#
#################################################################################
undef %saw;
@out = grep(!$saw{$_}++, @in);
print "Foram selecionadas: ",scalar(@in)," sequencias\n";
print "Taxa únicos: ",scalar(@out),"\n";
foreach $names(@out) {
print "$names:\t",scalar(@$names),"\n";
}
exit;