-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_to_DBfromFile.pl
More file actions
executable file
·95 lines (65 loc) · 1.95 KB
/
add_to_DBfromFile.pl
File metadata and controls
executable file
·95 lines (65 loc) · 1.95 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
#!/usr/local/bin/perl -w
use strict;
use Data::Dumper;
use DBI;
my $file = "/var/www/htdocs/RFmergedSVMpred_11nov2010_tab.txt";
open(PI, $file)
or die "Couldn't open file $file: $!";
print("file opened\n");
my @data=<PI>;
print("file read\n");
close(PI);
chomp(@data);
my $dbh = connect_to_DB('mysql','interactions','localhost','3306','','pintweb','pintpass');
print("database connected\n");
for my $line(1..10){
##data){
#print("$data[$line]\n");
my @array = split (/\t|\s+/, $data[$line]);
#print " pi terms\n@array\n";
insert_pi_terms($data[$line]);
if($line%3==0){
print ("Inserted line $line\n");
}
}
close(PI);
print("File closed\n");
disconnect_from_DB();
print ("DB disconnected");
sub connect_to_DB {
my ($driver, $instance, $host, $port, $cnf_file, $user, $password) = @_;
my $dbh = DBI->connect("DBI:${driver}:database=${instance};host=${host};port=$port;mysql_read_default_file=${cnf_file}",$user,$password)
or die "ERROR: $DBI::errstr\n";
if (!defined $dbh) {
die "Could not connect to database: $!";
}
return $dbh;
}
sub disconnect_from_DB {
$dbh->disconnect()
or die "Can't disconnect: $DBI::errstr\n";
}
sub insert_pi_terms {
my $line = shift;
my @array = split (/\t|\s+/, $line);
# print "Inside insert pi terms\n@array\n";
my $insert = qq(INSERT INTO proteins (id,
protein1,
protein2,
score1,
score2)
VALUES (?,?,?,?,?)
);
my $sth = $dbh->prepare($insert)
or die "Can't prepare: $DBI::errstr\n";
$array[1] = '-'
if not $array[1];
$array[6] = '-'
if not $array[6];
$sth->execute($array[0],
$array[1],
$array[2],
$array[3],
$array[4]);
#or warn "Can't execute: $DBI::errstr\n";
}