2017-09-25 101 views
0

用我的代碼,我遍歷文件並計算文件中的模式。我的代碼如下python;如何將輸出寫入文本文件

from collections import defaultdict 
import csv, os, re 
from itertools import groupby 
import glob 


    def count_kmers(read, k): 
     counts = defaultdict(list) 
     num_kmers = len(read) - k + 1 
     for i in range(num_kmers): 
      kmer = read[i:i+k] 
      if kmer not in counts: 
       counts[kmer] = 0 
      counts[kmer] += 1 
     for item in counts: 
      return(basename, sequence, item, counts[item]) 

    for fasta_file in glob.glob('*.fasta'): 
     basename = os.path.splitext(os.path.basename(fasta_file))[0] 
     with open(fasta_file) as f_fasta: 
      for k, g in groupby(f_fasta, lambda x: x.startswith('>')): 
       if k: 
        sequence = next(g).strip('>\n') 
       else: 
        d1 = list(''.join(line.strip() for line in g)) 
        d2 = ''.join(d1) 
        complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'} 
        reverse_complement = "".join(complement.get(base, base) for base in reversed(d1)) 
        d3 = list(''.join(line.strip() for line in reverse_complement)) 
        d4 = ''.join(d3) 
        d5 = (d2+d4) 
        counting = count_kmers(d5, 5) 
        with open('kmer.out', 'a') as text_file: 
         text_file.write(counting) 

而且我的輸出看起來像這樣

1035 1 GAGGA 2 
1035 1 CGCAT 1 
1035 1 TCCCG 1 
1035 1 CTCAT 2 
1035 1 CCTGG 2 
1035 1 GTCCA 1 
1035 1 CATGG 1 
1035 1 TAGCC 2 
1035 1 GCTGC 7 
1035 1 TGCAT 1 

的代碼工作正常,但我不能寫我的輸出到文件。我得到以下錯誤:

TypeError         Traceback (most recent call last) 
<ipython-input-190-89e3487da562> in <module>() 
    37     counting = count_kmers(d5, 5) 
    38     with open('kmer.out', 'w') as text_file: 
---> 39      text_file.write(counting) 

TypeError: write() argument must be str, not tuple 

什麼我做錯了,我怎麼能解決這個問題,以確保我的代碼輸出寫入到一個txt文件?

+1

你不會從你的函數返回任何東西,只是將東西打印到屏幕上。如果沒有顯式的'return'語句,Python函數會返回'None',這就是'count = count_kmers(d5,5)'會做的事情,而當您嘗試'''「.join(None)時,您會得到該錯誤 –

+0

我改變了我的代碼(使用返回而不是打印),但我得到的錯誤,我的寫參數必須是str而不是元組? – Gravel

+0

似乎是一個非常簡單的錯誤調試,然後... –

回答

5

原始版本count_kmers()不包含return聲明,這意味着它有一個隱含的return None

當您將此分配給counting時,您的所有錯誤都變得不言自明。

您的編輯後,函數的末尾是這樣的:

for item in counts: 
    return(basename, sequence, item, counts[item]) 

它會返回四個值的元組。它也會在第一次通過循環時退出函數。

+0

我改變了我的代碼(使用返回而不是打印),但比我得到的錯誤是我的寫參數必須是str而不是元組? – Gravel

+0

因爲你現在正在返回一個'tuple' - 並且你在循環的第一遍時退出 – kdopen

相關問題