-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmany2sql.py
More file actions
185 lines (144 loc) · 5.44 KB
/
many2sql.py
File metadata and controls
185 lines (144 loc) · 5.44 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
import sqlite3
import warnings
import subprocess as sp
import os
import sys
import numpy as np
import pandas as pd
from pathlib import Path
from .pdb2sqlcore import pdb2sql
class many2sql(pdb2sql):
def __init__(self, pdbfiles, tablenames=None):
"""Create a sql database containing multiple pdbs.
Args:
pdbfiles(list): list of pdb files or data.
tablenames (list): list of table names in string
Defaults to None.
Raises:
TypeError: pdbfiles must be a list
TypeError: tablenames must be a list
TypeError: tablenames must be a list of strings
"""
if not isinstance(pdbfiles, list):
raise TypeError('pdbfiles must be a list')
if tablenames is not None:
if not isinstance(tablenames, list):
raise TypeError('tablenames must be a list')
else:
for i in tablenames:
if not isinstance(i, str):
raise TypeError(f'tablenames must be a list of strings,'
f' {i} is a type of {type(i)}')
self.npdb = len(pdbfiles)
self.tablenames = tablenames
if self.tablenames is None:
self.tablenames = ['ATOM']
for i in range(1, self.npdb):
self.tablenames.append('ATOM'+str(i))
super().__init__(self.convert_input(
pdbfiles[0]), tablename=self.tablenames[0])
for i in range(1, self.npdb):
self._create_table(
self.convert_input(pdbfiles[i]), tablename=self.tablenames[i])
def __call__(self, **kwargs):
"""Return a class instance containing the selection of each structure
Returns:
many2sql: class instance containing the selection of each structure
"""
names = self._get_table_names()
first = True
for n in names:
pdb_data = self.sql2pdb(tablename=n, **kwargs)
if first:
new_db = many2sql([pdb_data], tablenames=[n])
first = False
else:
new_db._create_table(pdb_data, tablename=n)
return new_db
def convert_input(self, pdb):
"""Converts the input in a format that pdb2sql accepts
Args:
pdb (str, list, pdb2sql): input data
Returns:
str, list: correct input
"""
if isinstance(pdb, pdb2sql):
return pdb.sql2pdb()
return pdb
def intersect(self, match=['name', 'resname', 'resSeq', 'chainID']):
"""Returns a many2sql instance containing the common part of all the structures.
Args:
match (list, optional): column name that must match in the intersection.
Defaults to ['name', 'resname', 'resSeq', 'chainID'].
Returns:
many2sql: a class instance containing the tables of the matchin structure
"""
all_data = self.get_intersection('*', match=match)
all_names = self._get_table_names()
first = True
for name, data in zip(all_names, all_data):
if first:
new_db = many2sql(
[self.data2pdb(data)], tablenames=[name])
first = False
else:
new_db._create_table(
self.data2pdb(data), tablename=name)
return new_db
def get_all(self, columns, **kwargs):
"""Returns the data from the selection of all table in the instance
Args:
columns (str): column name(s) to return
Returns:
list: data per structure
"""
names = self._get_table_names()
data = []
for n in names:
data.append(self.get(columns, tablename=n, **kwargs))
return data
def get_intersection(self, column, match=['name', 'resname', 'resSeq', 'chainID']):
"""Return the data of the interection
Args:
column (str): column table to return
match (list, optional): column name that must match in the intersection.
Defaults to ['name', 'resname', 'resSeq', 'chainID'].
Returns:
list: data per structure
"""
names = self._get_table_names()
ntable = len(names)
select = "select "
# column names
if column == '*':
column_list = list(self.col.keys())
else:
column_list = column.split(',')
ncol = len(column_list)
# fields to select
fields = ''
for n in names:
for c in column.split(','):
fields += n+'.'+c+', '
fields = fields[:-2]+' '
# join the table
from_join = 'from ' + ' INNER JOIN '.join(names) + ' '
# conditions
cond = 'on '
for attr in match:
for i1 in range(ntable-1):
table1 = names[i1]
for i2 in range(i1+1, ntable):
table2 = names[i2]
cond += table1+'.'+attr+'='+table2+'.'+attr+' and '
cond = cond[:-5]+';'
query = select+fields+from_join+cond
raw_data = self.conn.execute(query)
data = []
for i in range(ntable):
data.append([])
for x in raw_data:
for it in range(ntable):
s, e = it*ncol, (it+1)*ncol
data[it].append(list(x[s:e]))
return data