I am a relative python newbie trying to efficiently look through a large csv file of ~8 million rows.
I have a csv of 6 columns:
+-------+-------+--------+-------+--------+----------+
| Gene1 | Start | End | Gene2 | Start | End |
+-------+-------+--------+-------+--------+----------+
| gyrA | 33 | 193 | dnaB | 844 | 965 |
| rpoS | 152 | 190 | ldh | 200 | 264 |
| gbpC | 456 | 500 | bgl | 1222 | 14567 |
+-------+-------+--------+-------+--------+----------+
.....etc for 8,000,000 rows
What I want to do is compare if the start and end of Gene2 is within a certain range +/- the start and end of Gene1. For example, I want to see if the end of Gene1 is within 20 of the start of Gene2. I then want to write all rows where this is true to a new csv file.
Code: If the end of Gene1 is within +/- 20 of the start of Gene2, append that entire row to a new file
Output:
+-------+-------+--------+-------+-------+--------+
| Gene1 | Start | End | Gene2 | Start | End |
+-------+-------+--------+-------+-------+--------+
| rpoS | 152 | 190 | ldh | 200 | 264 |
+-------+-------+--------+-------+-------+--------+
What would be the most efficient way to do this that is comprehendible for a python novice? I will have to run the code multiple times on multiple csv's so I'm concerned about speed. I tried messing around with pandas a bit but it seems like the itterrows() function is not the suggested way to go for efficiency. I think I can accomplish this pretty easily with csv reader, but I'm not sure about how long that might take to run.
Thanks for your help in solving this problem!