Skip to content

Commit e51c84a

Browse files
committed
Update sql conversion to text
1 parent f10931c commit e51c84a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

beams/auxiliary.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# -*- coding: utf-8 -*-
33

44
from collections import OrderedDict
5+
import gzip
6+
import sqlite3
7+
import pandas as pd
58
from pyteomics import mass as pyteomics_mass
69
from beams.db_parsers import parse_nist_database
710

@@ -141,16 +144,20 @@ def add_record(r, nm):
141144
return OrderedDict((k, lib[k]) for k in es)
142145

143146

144-
def convert_sql_to_text(path_sql, table_name, path_out, sep):
145-
146-
with open(path_sql, "rb") as db_dump:
147-
conn = sqlite3.connect(":memory:")
148-
cursor = conn.cursor()
149-
cursor.executescript(db_dump.read().decode('utf-8'))
150-
conn.commit()
151-
152-
df = pd.read_sql_query("select * from " + table_name, conn)
153-
df.to_csv(path_out, sep="\t")
154-
155-
conn.close()
156-
return
147+
def convert_sql_to_text(path_sql, table_name, path_out, separator="\t"):
148+
149+
if path_sql.endswith(".gz"):
150+
db_dump = gzip.GzipFile(path_sql, mode='rb')
151+
else:
152+
db_dump = open(path_sql, mode='rb')
153+
154+
conn = sqlite3.connect(":memory:")
155+
cursor = conn.cursor()
156+
cursor.executescript(db_dump.read().decode('utf-8'))
157+
conn.commit()
158+
159+
df = pd.read_sql_query("select * from " + table_name, conn)
160+
df.to_csv(path_out, sep=separator)
161+
162+
conn.close()
163+
db_dump.close()

0 commit comments

Comments
 (0)