Python:
C = []
with open('coordinates.txt') as fh:
fh.readline() # Skip header
for line in fh:
sl = line.strip().split()
C.append((sl[0], int(sl[1]), int(sl[2]), sl[3]))
A = []
with open('gene_final_anno.csv') as fh:
fh.readline()
for line in fh:
sl = line.strip().split(',')
A.append((sl[0], int(sl[1]), int(sl[2]), sl[3], sl[4], sl[5]))
for cchr, cst, cen, cstr in C:
for achr, ast, aen, _, gid, gname in A:
if cchr == achr:
l = sorted([(cst, cen), (ast, aen)])
if l[1][0] <= l[0][1]: # Overlap
st, en = [cst, cen] if cstr == '+' else [cen, cst]
output = [cchr, st, en, cstr, gid, gname]
print('{}\t{}\t{}\t{}\t{}\t{}'.format(*output))
coordinates.txt
chrom start end strand
17 71223692 71274336 +
17 71223692 71274336 -
1 4247322 4247912 -
1 4427449 4432604 +
1 4763414 4764404 -
gene_final_anno.csv
chrom,start,end,strand,gene_id,gene_name
17,71223692,71274336,-,ENSMUSG00000085299,Gm16627
17,18186448,18211184,+,ENSMUSG00000067978,Vmn2r-ps113
11,84645863,84684319,-,ENSMUSG00000020530,Ggnbp2
7,51097639,51106551,+,ENSMUSG00000074155,Klk5
13,31711037,31712238,+,ENSMUSG00000087276,Gm11378
9,44887266,44916613,+,ENSMUSG00000048534,Amica1
13,31889579,31892618,+,ENSMUSG00000086144,Gm11379
19,33534099,33547681,-,ENSMUSG00000086875,Gm8975
7,51136530,51141174,+,ENSMUSG00000006948,Klk4
13,31976094,31977093,+,ENSMUSG00000083149,Gm11380
11,3883639,3899329,+,ENSMUSG00000049721,Gal3st1
12,86426764,86447381,-,ENSMUSG00000042320,Prox2
13,32471776,32475557,-,ENSMUSG00000085770,Gm11381
19,33572527,33592270,-,ENSMUSG00000079344,Lipo4
Output:
17 71274336 71223692 + ENSMUSG00000085299 Gm16627
17 71223692 71274336 - ENSMUSG00000085299 Gm16627
Why are you using enumerate? I don't see where you use those
indexandindex2variables.It's to me not really clear what you aim to achieve, but I have the idea that more appropriate tools exist rather than rolling your own solution.
@WouterDeCoster upstream and downstream of this code is in python hence..
There might be solution using the htseq-count python module, or similar.
htseq count uses sorted bam (by name) and gtf right? how can I use it for such operations?
I don't have code ready for that, but have a look at http://www-huber.embl.de/HTSeq/doc/tour.html#genomic-intervals-and-genomic-arrays
In your script, you forgot to convert start and end coordinates into integer numbers. If you show us a sample of your query files (i.e. coordinates, gene_final_anno.csv) we would probably be more helpful.
sorry should have done this when I posted the question gene_final_anno.csv (head)
coordinates head
gene_final_anno, is nothing but annotation file from biomart, for mm9.37.67, and coordinates file is from tophat junction bed representing junctions need to annotate each junction with gene id and name.
As written by combio.pl
That's important.
Ok will change that, any more pointers?