Repository navigation
Expand file tree
/
Copy pathDatabases.pm
More file actions
executable file
·254 lines (179 loc) · 7.57 KB
/
Copy pathDatabases.pm
File metadata and controls
executable file
·254 lines (179 loc) · 7.57 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package Databases;
use Exporter qw(import);
our @ISA = qw(Exporter);
our @EXPORT = qw(startMongoDB insertData updateData readData removeData); #functions exported by default
use warnings; use strict; use diagnostics; use feature qw(say);
use Carp;
use MongoDB; use MongoDB::OID;
use MyConfig;
# =============================================
#
# CAPITAN: Andres Breton, http://andresbreton.com
# FILE: Databases.pm
#
# =============================================
=head1 NAME
Databases - package to handle database connections
=head1 SYNOPSIS
Creation:
use Databases;
=head1 DESCRIPTION
=head1 EXPORTS
=head2 Default Behaviors
Exports startMongoDB, insertData, updateData, readData, removeData subroutines by default
use Databases;
=head1 FUNCTIONS
=cut
my @dataFields = qw(_id accession sequence version locus organism seqLength gene proteinID translation);
#-------------------------------------------------------------------------------
# MAIN
=head2 startMongoDB
Example : $PID = startMongoDB($MONGODB, $outDir);
Description : Start Mongo database service and return process ID
Returntype : Scalar
Status : Stable
=cut
sub startMongoDB {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($MONGODB, $outDir)';
@_ == 2 or croak wrongNumberArguments(), $filledUsage;
my ($MONGODB, $outDir) = @_;
my $pid;
my $dbDir = $outDir."/db";
`mkdir $dbDir` unless (-e $dbDir);
my $mongoLog = $dbDir."/mongo.log";
my $command = "mongod --dbpath $dbDir --logpath $mongoLog --fork";
say "\nStarting MongoDB server...";
my @result = `$command`; #get shell results
if ($? == 0) { #Check return value
$pid = $result[1] =~ /.+:\s(\d+)$/; $pid = $1; #get child PID
say "MongoDB successfully started.\n";
return $pid;
} elsif ($? == 25600) { #Possible mongd already running
say "*********FAILED";
say "Could not fork. This was most likely caused by an instance of [mongod] already running.";
# Check for Currently Running MongoDB Server
my @mongdPS = `ps -e -o pid,args | grep \"mongod\"`;
if ($mongdPS[0] =~ /^\s?(\d+)\s+mongod.*/) {
$pid = $1;
say "YES! Found running process: $mongdPS[0]";
print "Would you like to continue (y/n)? ";
my $response = lc <>; chomp $response;
if ($response eq "yes" || $response eq "y") {
return $pid;
} else {
exit;
}
} else {
croak "Sorry, could not find instance of mongod running on system. Please check processes.", $!;
}
} else {
croak "ERROR: Failed to execute $command\n Something happened that did not allow MongoDB server to start!", $!;
}
}
=head2 insertData
Example : insertData($MONGODB, $COLLECTION, $id, $gi, $accession, $version, $locus, $organism, $sequence, $seqLen, $gene, $proteinID, $translation)
Description : Insert document record in Mongo database
Returntype : Scalar List
Status : Stable
=cut
sub insertData {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($MONGODB, $COLLECTION, $id, $gi, $accession, $version, $locus, $organism, $sequence, $seqLen, $gene, $proteinID, $translation)';
@_ == 13 or croak wrongNumberArguments(), $filledUsage;
my ($MONGODB, $COLLECTION, $id, $gi, $accession, $version, $locus, $organism, $sequence, $seqLen, $gene, $proteinID, $translation) = @_;
my $collectionObj = databaseConnection($MONGODB, $COLLECTION);
say "Storing data for ID ($id) into database $MONGODB";
$collectionObj->insert({_id => $gi, #GI stored as Mongo UID
"accession" => $accession,
"version" => $version,
"locus" => $locus,
"organism" => $organism,
"seqLength" => $seqLen,
"sequence" => $sequence,
"gene" => $gene,
"proteinID" => $proteinID,
"translation" => $translation
})
}
=head2 updateData
Example : updateData($field, $value, $MONGODB, $COLLECTION);
Description : Update document record in Mongo database
Returntype : Scalar List
Status : Stable
=cut
sub updateData {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($field, $value, $MONGODB, $COLLECTION)';
@_ == 4 or croak wrongNumberArguments(), $filledUsage;
my ($field, $value, $MONGODB, $COLLECTION) = @_;
say "\nUPDATING $field record [$value] in database...";
say "Available fields are:\t@dataFields\n";
print "What field do you want? ";
my $fieldUpdate = <>; chomp $fieldUpdate;
print "What is the NEW value for $fieldUpdate field? ";
my $newValue = <>; chomp $newValue;
my $collectionObj = databaseConnection($MONGODB, $COLLECTION);
$collectionObj->update({$field => $value}, {'$set' => {$fieldUpdate => $newValue}});
say "Document $value updated. $fieldUpdate field changed to $newValue.";
}
=head2 readData
Example : readData($field, $value, $MONGODB, $COLLECTION);
Description : Read document record in Mongo database
Returntype : Scalar List
Status : Stable
=cut
sub readData {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($field, $value, $MONGODB, $COLLECTION)';
@_ == 4 or croak wrongNumberArguments(), $filledUsage;
my ($field, $value, $MONGODB, $COLLECTION) = @_;
my $collectionObj = databaseConnection($MONGODB, $COLLECTION);
say "\nREADING field \"$field\" value \"$value\" from database...";
my $cursor = $collectionObj->find({$field => $value});
while (my $obj = $cursor->next) {
say "Available fields are:\t@dataFields\n";
print "What field do you want? ";
my $response = <>; chomp $response;
say "Here you go [$response]:\n", $obj->{$response};
}
}
=head2 removeData
Example : removeData($field, $value, $MONGODB, $COLLECTION);
Description : Remove document record in Mongo database
Returntype : Scalar List
Status : Stable
=cut
sub removeData {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($field, $value, $MONGODB, $COLLECTION)';
@_ == 4 or croak wrongNumberArguments(), $filledUsage;
my ($field, $value, $MONGODB, $COLLECTION) = @_;
say "REMOVING $field record [$value] in database...";
my $collectionObj = databaseConnection($MONGODB, $COLLECTION);
$collectionObj->remove({$field => $value});
}
#-------------------------------------------------------------------------------
# HELPERS
=head2 databaseConnection
Arg [1] : No arguments
Example : databaseConnection($MONGODB, $COLLECTION)
Description : Start connection to Mongo database server
Returntype : Scalar List
Status : Stable
=cut
sub databaseConnection {
my $filledUsage = 'Usage: ' . (caller(0))[3] . '($MONGODB, $COLLECTION)';
@_ == 2 or croak wrongNumberArguments(), $filledUsage;
my ($MONGODB, $COLLECTION) = @_;
my $client = MongoDB::MongoClient->new; #connect to local db server
my $db = $client->get_database($MONGODB); #get MongoDB database
my $collectionObj = $db->get_collection($COLLECTION); #get collection
return $collectionObj;
}
=head1 COPYRIGHT AND LICENSE
Andres Breton (C) 2016
[LICENSE]
=head1 CONTACT
Please email comments or questions to Andres Breton, me@andresbreton.com
=head1 SETTING PATH
If PERL5LIB was not set, do something like this:
use FindBin; use lib "$FindBin::RealBin/lib";
This finds and uses subdirectory 'lib' in current directoy as library location
=cut
1;