2014-05-09 5 views
-1
defaultdict(<type 'int'>, {'match': 1}) 
[(0, 0)] 
[] 
[] 
[] 
Traceback (most recent call last): 
    File "Joinomattic_1.py", line 409, in <module> 
    verboseprint (magic(matching)) 
    File "Joinomattic_1.py", line 408, in <lambda> 
    magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp. 
ValueError: invalid literal for int() with base 10: ''' 

위 코드는 아래 코드에서 계속 발생하지만, 큰 프로그램 인 경우 자체적으로 실행될 때 오류가 발생하지 않고 출력이 output_file에 기록됩니다. 이 오류는 무엇을 의미하며 왜 다른 코드에서만 발생합니까? 다음은 내 코드는 더 큰 프로그램의 경우 일부입니다ValueError : 밑이 10 인 int()에 대한 리터럴이 올바르지 않습니다. 큰 코드의 일부일 때는 오류가 발생합니다.

n = 0 
consec_matches = [] 
chars = defaultdict(int) 

for k, group in groupby(zip(line1_u_i, line2_u_rev_comp_i), class_chars): 
    elems = len(list(group)) 
    chars[k] += elems 
    if k == 'match': 
     consec_matches.append((n, n+elems-1)) 
    n += elems 

verboseprint (chars) 
verboseprint (consec_matches) 
verboseprint ([x for x in consec_matches if x[1]-x[0] >= 9]) 
list = [x for x in consec_matches if x[1]-x[0] >= 9] 
flatten_list= [x for y in list for x in y] 
verboseprint (flatten_list) 
matching=[y[1] for y in list for x in y if x ==0 ] 
verboseprint (matching) 
magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp. 
verboseprint (magic(matching)) 
line2_u_rev_comp_i_l = line2_u_rev_comp_i[magic(matching):] 
line3=line1_u_i+line2_u_rev_comp_i_l 
verboseprint (line3) 
if line3: 
    verboseprint(line3, file = output_file) 

그리고 여기 혼자 서있다 :

독립 혼자 출력이
from __future__ import print_function 
from collections import defaultdict 
from itertools import groupby 
import argparse #imports the argparse module so it can be used 
from itertools import izip 





parser = argparse.ArgumentParser() #simplifys the wording of using argparse as stated in the python tutorial 
parser.add_argument("-r1", type=str, action='store', dest='input1', help="input the forward read file") # allows input of the forward read 
parser.add_argument("-r2", type=str, action='store', dest='input2', help="input the reverse read file") # allows input of the reverse read 
parser.add_argument("-v", "--verbose", action="store_true", help=" Increases the output, only needs to be used to provide feedback to Tom for debugging") 
parser.add_argument("-n", action="count", default=0, help="Allows for up to 5 mismatches, however this will reduce accuracy of matching and cause mismatches. Default is 0") 
parser.add_argument("-fastq", action="store_true", help=" States your input as fastq format") 
parser.add_argument("-fasta", action="store_true", help=" States your input as fasta format") 
parser.add_argument("-o", "--output", help="Directs the output to a name of your choice") 
args = parser.parse_args() 

output = str(args.output) 


def class_chars(chrs): 
    if 'N' in chrs: 
     return 'unknown' 
    elif chrs[0] == chrs[1]: 
     return 'match' 
    else: 
     return 'not_match' 


#with open(args.output, 'w') as output_file: 

output_file= open(output, "w") 

s1 = 'aaaaaaaaaaN123bbbbbbbbbbQccc' 
s2 = 'aaaaaaaaaaN456bbbbbbbbbbPccc' 
n = 0 
consec_matches = [] 
chars = defaultdict(int) 

for k, group in groupby(zip(s1, s2), class_chars): 
    elems = len(list(group)) 
    chars[k] += elems 
    if k == 'match': 
     consec_matches.append((n, n+elems-1)) 
    n += elems 

print (chars) 
print (consec_matches) 
print ([x for x in consec_matches if x[1]-x[0] >= 9]) 
list = [x for x in consec_matches if x[1]-x[0] >= 9] 
flatten_list= [x for y in list for x in y] 
print (flatten_list) 
matching=[y[1] for y in list for x in y if x ==0 ] 
print (matching) 
magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp. 
print (magic(matching)) 
s2_l = s2[magic(matching):] 
line3=s1+s2_l 
print (line3) 
if line3: 
    print(line3, file = output_file) 

경우 : 당신의 출력에서 ​​볼 수 있듯이

defaultdict(<type 'int'>, {'not_match': 4, 'unknown': 1, 'match': 23}) 
[(0, 9), (14, 23), (25, 27)] 
[(0, 9), (14, 23)] 
[0, 9, 14, 23] 
[9] 
9 
aaaaaaaaaaN123bbbbbbbbbbQcccaN456bbbbbbbbbbPccc 

답변

0

당신, matching는 빈 목록입니다. 따라서 join 뒤에 빈 문자열 ''이 생기고 정수로 변환 할 수 없습니다. 시도 :

lambda matching: int(''.join(str(i) for i in matching) or 0) 

이 것 return 0matching == []합니다.

+0

예 그게 이제 다음 오류가 발생했습니다 그 문제를 해결하지만 것 같습니다하지만 또 다른 문제가 완전히 세 번째 줄 = line1_u_i + line2_u_rev_comp_i_l 형식 오류를 먹으 렴 : – Tom

+0

은 무엇 자세한 내용은 당신이 필요로 할 객체 'STR'과 '목록'연결할 수없는 이유는 무엇입니까? 'line1_u_i'는 문자열이고,'line2_u_rev_comp_i_l'은 목록이므로 함께 추가 할 수는 없습니다. 왜 문자열에 목록을 추가하고 있습니까? 아니면 다른 것으로 생각되는 항목은 무엇입니까? – jonrsharpe

+0

둘 다 문자열이라고 생각하여 목록에서 line2를 문자열로 변환하는 방법을 찾고 있습니다. – Tom