|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | from collections import OrderedDict
|
| 5 | +import gzip |
| 6 | +import sqlite3 |
| 7 | +import pandas as pd |
5 | 8 | from pyteomics import mass as pyteomics_mass
|
6 | 9 | from beams.db_parsers import parse_nist_database
|
7 | 10 |
|
@@ -141,16 +144,20 @@ def add_record(r, nm):
|
141 | 144 | return OrderedDict((k, lib[k]) for k in es)
|
142 | 145 |
|
143 | 146 |
|
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