我目前正在使用bioinfomatics类的数据库,但我在格式化SQL输出时遇到了问题。我的python脚本查询这些元组。而不是我的输出:
CENPVP2 441495 9606 NR_033773.1 None NC_000023.11 None
CENPVP2 441495 9606 NR_033773.1 None NT_011630.15 None
CENPVP2 441495 9606 None None NG_022599.1 None
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NC_000023.11 12477932
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NC_000023.11 16382448
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NC_000023.11 18976975
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NG_027735.1 12477932
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NG_027735.1 16382448
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NG_027735.1 18976975
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NT_011786.17 12477932
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NT_011786.17 16382448
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NT_011786.17 18976975
CT47A11 255313 9606 None None NG_027735.1 12477932
CT47A11 255313 9606 None None NG_027735.1 16382448
CT47A11 255313 9606 None None NG_027735.1 18976975
如果每个字段由制表符分隔,我需要这个,其中每个字段仍然由制表符分隔,但现在有多值字段,其中值由管道分隔,空值由短划线表示:
CENPVP2 441495 9606 NR_033773.1 - NC_000023.11|NG_022599.1|NT_011630.15 -
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NC_000023.11|NG_027735.1|NT_011786.17 12477932|16382448|18976975
格式化输出以匹配第二个表格的最佳方法是什么?
这是我的python脚本:
import sys
import getopt
import psycopg2
def writerows(row, outFile):
outFile.write("%s\t" % row[1])
outFile.write("%s\t" % row[0])
outFile.write("%s\t" % row[2])
outFile.write("%s\t" % row[3])
outFile.write("%s\t" % row[5])
outFile.write("%s\t" % row[4])
outFile.write("%s\n" % row[6])
def usage(err):
print("I will handle this later")
def main():
inFile = sys.stdin
outFile = sys.stdout
try:
opts, args = getopt.getopt(sys.argv[1:], "i:o:")
except getopt.GetoptError as err:
usage(err)
sys.exit(2)
for (opt, arg) in opts:
if(opt == "-i"):
inFile = open(arg, "r")
if(opt == "-o"):
outFile = open(arg, "w")
line = inFile.readline()
line = line.replace("\n", "")
conn = psycopg2.connect("dbname=********* user=*********** "
"password=********** host=localhost")
cursor = conn.cursor()
while (line):
cursor.execute("SELECT DISTINCT geneinfo.gene_id, geneinfo.symbol, "
"geneinfo.tax_id, gene2refseq.rna_accession, "
"gene2refseq.gen_accession, "
"gene2refseq.pro_accession, gene2pubmed.pubmed_id FROM "
"geneinfo LEFT JOIN gene2refseq ON "
"geneinfo.gene_id = gene2refseq.gene_id LEFT JOIN "
"gene2pubmed ON geneinfo.gene_id = gene2pubmed.gene_id "
"WHERE geneinfo.symbol ILIKE '"+line+"' OR "
"geneinfo.synonyms ILIKE '%"+line+"%' ORDER BY "
"geneinfo.symbol ASC, geneinfo.tax_id ASC;")
result = cursor.fetchone()
if result:
while result:
writerows(result, outFile)
result = cursor.fetchone()
else:
outFile.write("\n")
line = inFile.readline()
line = line.replace("\n", "")
cursor.close()
conn.close()
if (__name__=='__main__'):
main()
答案 0 :(得分:0)
Parfait在评论中说了些什么。查询看起来像:
SELECT DISTINCT gi.gene_id, gi.symbol, gi.tax_id,
rs.rna_accession, rs.gen_accession,
array_to_string(array_agg(rs.pro_accession), '|')),
array_to_string(array_agg(pm.pubmed_id), '|'))
FROM geneinfo gi
LEFT JOIN gene2refseq rs ON gi.gene_id=rs.gene_id
LEFT JOIN gene2pubmed pm ON gi.gene_id=pm.gene_id
WHERE gi.symbol ILIKE 'SOMESTUFF' OR
gi.synonyms ILIKE 'OTHERSTUFF'
GROUP BY 1,2,3,4,5
ORDER BY gi.symbol ASC, gi.tax_id ASC;