How to over come "TypeError: argument of type 'int' is not iterable" in python?
2
0
Entering edit mode
7.5 years ago
murali ▴ 100

Hi,

I am working on variant calling. My goal is to get the chromosome positions from vcf file and coverage on positive and negative strands. I am using pysam and pyvcf modules in python. While executing the python script it throws error TypeError: argument of type 'int' is not iterable.

I understand what it means. How can I overcome this error?

I need suggestions to succeed in dealing with this problem?

import vcf, glob, pysam, csv
from os.path import basename

pyvcf pysam python next-gen • 20k views
1
Entering edit mode

Can you give a traceback of the error or at least the line number where it is occurring? It would be faster than to go through hundred-odd rows of your code manually.

P.S. you do not need semicolons at the end of the lines in python. in fact, that is considered bad style.

0
Entering edit mode

I still occasionally do that myself. I blame C :)

0
Entering edit mode

The error takes place at line 74.

elif ( i not in vcf_pos and i in pos):


Thanks for notifying me that about semicolon(;) at the end of line and I'll follow this suggestion from now on.

1
Entering edit mode

Can you highlight the line where the error is happening ?

0
Entering edit mode

The line number 74

elif ( i not in vcf_pos and i in pos):


is the line throws error.

0
Entering edit mode

I've put your code in a code block, which will hopefully help formatting a bit.

0
Entering edit mode

Thanks @Devon Ryan

3
Entering edit mode
7.5 years ago

Note that originally pos is a list, but later on you make it an integer (pos = 0)...

Edit: I'll add that modifying something that you're iterating over is generally a bad idea.

Edit2: You could just change the instances of pos that relate to positive counts to positive.

0
Entering edit mode

Thanks @ Devon Ryan. Hurry, It worked. I have been working on this couple of days, finally find out the bug.

3
Entering edit mode
7.5 years ago

The problem is that the variable named pos is really an integer, as you define it in line 51:

pos = 0


This variable pos should be a list to make this if statement (elif ( I not in vcf_pos and I in pos):) to work. You mistakenly overwritten the variable pos from line 20.

pos = list(set(all_pos))