Hi
I have two CSV files as input, for example:
file1 :
AK163828 chr5 s1 + e1 cttt 4
AK163828 chr5 s2 + e2 gtca 4
AK168688 chr6 s3 + e3 ggcg 4
AK168688 chr6 s4 + e4 tctg 4
file2 :
chr6s3+e3 ggcg
chr5s1+e1 cttt
chr6s4+e4 tata
chr5s2+e2 ggcg
#as you can see the file2 is randomly sorted
I want to compare column 1 of file2 with column 2, 3, 4, 5 merged of file1 and at the same time the column 2 of file2 with column 6 of file 1, and select only the matching lines.
The desired output is
chr6s3+e3 ggcg
chr5s1+e1 cttt
I tried to use this code:
import csv
reader1 = csv.reader(open(file1), dialect='excel-tab' )
reader2 = csv.reader(open(file2), dialect='excel-tab' )
for row1, row2 in zip(reader1,reader2):
F1 = row1[1] + row1[2] + row1[3] + row1[4] + '\t' row1[5]
F2 = row2[0] + '\t' + row2[1]
print set(F1) & set(F2)
But it doesn’t work. Can you help me to fix my code or give me an other way to do it?
Thanks for your help!
Quick and dirty:
Output: