-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseqsql2fa.py
More file actions
57 lines (52 loc) · 2.32 KB
/
seqsql2fa.py
File metadata and controls
57 lines (52 loc) · 2.32 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
#!/usr/bin/env python
# Copyright (C) 2015,2016 Mohammad Alanjary
# University of Tuebingen
# Interfaculty Institute of Microbiology and Infection Medicine
# Lab of Nadine Ziemert, Div. of Microbiology/Biotechnology
# Funding by the German Centre for Infection Research (DZIF)
#
# This file is part of FunARTS
# FunARTS is free software. you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version
#
# License: You should have received a copy of the GNU General Public License v3 with FunARTS
# A copy of the GPLv3 can also be found at: <http://www.gnu.org/licenses/>.
import argparse, setlog, datetime, os, sqlite3 as sql
global log
log = setlog.init(toconsole=True)
def writefasta(db,outfile,nuc=False,idprfx=""):
conn = sql.connect(db)
cur = conn.cursor()
fn,ext = os.path.splitext(outfile)
fname = fn+ext
cur.execute("SELECT * FROM Seqs")
rec = cur.fetchone()
if rec:
log.info("Writing sequences to disk...")
with open(fname,"w") as ofil:
r = rec
ofil.write(">%s|%s%s %s|source|%s|loc|%s_%s_%s\n%s\n" % (r[1], idprfx, r[0],r[2],r[4],r[5],r[6],r[7],r[-1-int(nuc)]))
while rec:
for r in cur:
ofil.write(">%s|%s%s %s|source|%s|loc|%s_%s_%s\n%s\n" % (r[1], idprfx, r[0],r[2],r[4],r[5],r[6],r[7],r[-1-int(nuc)]))
else:
rec=False
else:
log.warning("No sequences to export for database %s"%db)
#Update lastscan of exported seqs
# try:
# cur.execute("UPDATE Seqs SET lastscan=? WHERE lastscan <= ?",(timestamp,fus))
# except sql.OperationalError as ex:
# print "Error:",ex
conn.commit()
conn.close()
# Commandline Execution
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Write fasta file of sequence DB with format >ORG|dbkey")
parser.add_argument("input", help="sequence database")
parser.add_argument("outfile", help="Filename of for saved files. Number extension will be added for split files.")
parser.add_argument("-n","--nuc", help="Write DNA sequences instead of protein",action='store_true')
args = parser.parse_args()
writefasta(args.input,args.outfile,args.nuc)