python - Python3 write selected zones in a new file -
i had browse dictionary gene's taxon , everytime encounter taxon, open .fasta
file same name , in fasta file had geneid encountered during taxon research. possible because in file made dictionary has "taxon1|geneid1, taxon1|geneid2, taxon2|geneid1,...". , so, when meet specific geneid in specific fasta file, had stock lines under specific geneid able write in new file. fasta file looks like:
>taxon|gene1 actgcatcgctagctagaaatcgcta tacgatcaaacctagcgatcttacga >taxon|gene2 tagctagctagctagaatatcccgat gctagcaatgctcttccggtagctat
so when meet right geneid in fasta file, lines have copy/stock following 2 lines atgc. did few functions make said, i'm stuck , write part of file datas stocked. here part of code :
def readfastafile(taxonomy, searchgeneid): fastafile = open(taxonomy + ".fasta", "r") banco = false geneidsequence = "" line in fastafile: if line[0] == ">": elements = line.split("|") taxonomy = elements[0] geneid = elements[1] if geneid == searchgeneid: banco = true else: banco = false else: if banco == true: geneidsequence += line fastafile.close() return geneidsequence def getsequencesfromfastasandwritetheminnewfastas(dictio): groupname in dictio: taxonandgene = dictio[groupname] groupfastafile = open(groupname + ".fasta", "w") taxon in taxonandgene: geneids = taxonandgene[taxon] #print("search" + str(geneids) + " in " + taxon + ".fasta") geneid in geneids: readfastafile(taxon, geneid) groupfastafile.write() #here part i'm stuck groupfastafile.write("\n") groupfastafile.close()
my problem in lasts lines (marked #here comment): don't know write between ()
write data fasta files new file.
thank answers.
anyway, found solution problem.
i added lines :
tempogeneid = elements[1] geneid = tempogeneid[0:-1]
in function :
def readfastafile(taxonomy, searchgeneid):
to remove "\n" @ end of geneids , added lines :
for geneid in geneids: groupfastafile.write(">" + taxon + "|" + geneid + "\n" + readfastafile(taxon, geneid))
in function :
def getsequencesfromfastasandwritetheminnewfastas(dictio):
so works expected. i'm sorry if post took time yo read. have great day.
Comments
Post a Comment