Did you see what I had in my comment below? Does this one make more sense??
I currently have a list of genes in a file. Each line has a chromosome with it's information. Such an entry appears as:
NM_198212 chr7 + 115926679 115935830 115927071 11593344 2 115926679,115933260, 115927221,115935830,
The sequence for the chromosome starts at base 115926679 and continues up to (but not including) base 115935830
If we want the spliced sequence, we use the exons.The first extends from 115926679 to 155927221, and the second goes from 115933260 to 115935830
However, I have run across a problem when on a complementary sequence such as:
NM_001005286 chr1 - 245941755 245942680 245941755 245942680 1 245941755, 245942680
Since column 3 is a '-', these coordinates are in reference to the anti-sense strand (the complement to the strand). The first base (in bold) matches the last base on the sense strand (in italics). Since the file only has the sense stand, I need to try to translate coordinates on the anti-sense strand to the sense strand, pick out the right sequence and then reverse-complement it.
That said, I have only been programming for about half a year and and not sure how to starts going about doing this.
I have written a regular expression:
'(NM_\d+)\s+(chr\d+)([(\+)|(-)])\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+),(\d+),s+(\d+),(\d+),'
but am now unsure as to how to start this function...
If anyone can help me get started at all on this, perhaps making me see how to do this, I would very much appreciate it.
3 answers
First of all, where are you data come from? It seems inconsistent! Why the coordinates are noted as strings?
NM_198212 chr7 + 115926679 115935830 115927071 11593344 2 115926679,'115933260', 115927221,'115935830',
Nevertheless, in python you can split every string (as suggested by Zev) and the get your data of interest from the list
for line in open( fname ):
dataList = line.split('\t')
name,chrom,strand,start,stop = dataList[:5]
print name,chrom,strand,start,stop
If you have GenBank file as input, I recommend Biopython!
Try to do it in ipython - you can test your code:)
I got an headache just by looking at your regular expression ;) .
Seriously: You are over complicating. I suggest the following:
- Remove commas and apostrophes. Use, say str.replace(",", "")
- Split on space (or eventually tab) to get the tokens toks = str.split(" ")
- On toks[2] you have the strand, toks[3] and [4] have start and end
- toks[7] gives you the number of exons, and from there you can extract initial and starting positions and go on with strand logic.
Seasoned programmers struggle with writing (and reading) reg exps like that. Furthermore you would have to make it more complex than it is currently: you only support two exons as it stands, you need to repeat the whole group (+ operator).
Replace the reg ex with split logic. I recommend to make your like less complicated
OK: suppose this is chromsome 25:
AAAAAAAAAACCCCCCCCCCTTTTTTTTTTGGGGGGGGGG
(there are 10 of each character).
Now: if I am looking for an unspliced gene on: <blah> chr25 + 10 20 <blah>
Then the gene starts on position 10 (starting from 0), and goes up to but not including position 20. So its:
CCCCCCCCCC
This is easy. It matches python string slicing really well.
Its more confusing if I give you:
<blah> chr25 - 10 20 <blah>
What you have is the positive strand. But this gene is on the negative (complementary) strand. Remember what the chromosome looks like as a souble-strand:
AAAAAAAAAACCCCCCCCCCTTTTTTTTTTGGGGGGGGGG TTTTTTTTTTGGGGGGGGGGAAAAAAAAAACCCCCCCCCC
We are looking for the gene on the bottom strand. Meaning we count from 0 starting on the right. Number the top strand from the left, and the bottom strand from the right. So what I want here is AAAAAAAAAA.
The catch is that I'm only giving you the top strand. I'm not giving you the bottom strand. (You could generate yourself from the top strand — but given how large it is, I advise against that.)
So you need to convert coordinates. On the bottom strand, base 0 (the right-most C) is opposed to base 39 on the top strand. Base 1 is against base 38. Base 2 is against case 37. (Important point: notice what happens when you add these two numbers up — every time.) So base 10 is against base 29, and base 19 is against base 20.
So: if I want to find base 10-20 on the bottom strand, I can look at base 20-29 on the top (and then reverse-complement it).
I need to figure out how to translate to coordinates on the bottom strand to the equivalent coordinates on the bottom strand. Yes: it is very confusing
I have tried:
fields = line.split(' \t')
geneID, chr, strand = fields[:2]
start = int(fields[3])
end = int(fields[4])
if strand == '-':
newstart,newend = -(start +1), -(end +1)
Now I have to combine this with the function below to get the reverse compliment, right?
which is on the right track, yet but its not enough. This would take the 10 and 20, and turn it into a 20 and 10.
And I know I can reverse complement the string by doing this:
r = s[::-1]
bc = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
l = list(r)
o = [bc[base] for base in l]
return ''.join(o)
But, from here I am lost.
You should probably just edit your question to add this info, rather than submitting it as an answer (since it's not a working answer). Unless Biostar doesn't let you edit your own questions?
>>What you have is the positive strand. But this gene is on the negative (complementary) strand. Remember what the chromosome looks like as a souble-strand:
AAAAAAAAAACCCCCCCCCCTTTTTTTTTTGGGGGGGGGG TTTTTTTTTTGGGGGGGGGGAAAAAAAAAACCCCCCCCCC
We are looking for the gene on the bottom strand. Meaning we count from 0 starting on the right. Number the top strand from the left, and the bottom strand from the right. So what I want here is AAAAAAAAAA.<<
You seem to got confused here. All annotation formats (Genpred, RefFlat, GTF, GFF & many more) with genomic regions are always marked (or started) from left to right ie from left to right irrespective of strand. So here in this case you will be looking for sequence "GGGGGGGGGG" . Strand will decide only the reverse complementarity of the final sequence. And you are seem to be working with refFlat (UCSC) format.
Log in to answer this question.
I am not a python user, but it seems like splitting the line into a data structure such as an array would make your task much easier.
Welcome to BioStar
It's a bit unclear what you're trying to accomplish: whether you want the full gene sequence or only exons, and whether you only don't know how to get the coordinates on correct order, or if you need help getting the sequence after that too. Could you post an example of what output you want, in addition to the example input? Also, what filetype is your input file? Presumably it's NOT fasta, or else you would have the sequence already... (by the way, hi there! I'm the person who pointed you to Biostar on SO)
Just to clarify, even though a gene may be on the reverse strand, the reported coordinates are from the forward strand so you only need to reverse-complement the sequence from that location.