annotate mytools/revcompl.py @ 9:87eb5c5ddfe9

Uploaded
author xuebing
date Fri, 09 Mar 2012 20:01:43 -0500
parents f0dc65e7f6c0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
1 import sys
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
2
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
3 compldna = {'A':'T',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
4 'C':'G',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
5 'G':'C',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
6 'T':'A',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
7 'U':'A',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
8 'M':'K',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
9 'K':'M',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
10 'W':'W',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
11 'S':'S',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
12 'R':'Y',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
13 'Y':'R',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
14 'N':'N'}
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
15 complrna = {'A':'U',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
16 'C':'G',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
17 'G':'C',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
18 'T':'A',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
19 'U':'A',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
20 'M':'K',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
21 'K':'M',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
22 'W':'W',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
23 'S':'S',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
24 'R':'Y',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
25 'Y':'R',
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
26 'N':'N'}
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
27 def complement(seq,compl):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
28 complseq = [compl[base] for base in seq]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
29 return complseq
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
30
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
31 def reverse_complement(seq,compl):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
32 seq = list(seq)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
33 seq.reverse()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
34 return ''.join(complement(seq,compl))
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
35
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
36 def readFastaFile(infile,outfile,compl):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
37
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
38 fin = open(infile)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
39 out = open(outfile,'w')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
40
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
41 currSeq=''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
42 currSeqname=None
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
43 for line in fin:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
44 if '>' in line:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
45 if currSeqname !=None:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
46 out.write(currSeqname+reverse_complement(currSeq,compl)+'\n')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
47 currSeqname=None
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
48 currSeq=''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
49 currSeqname=line
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
50 else:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
51 currSeq=currSeq+line.strip().upper()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
52
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
53 if currSeqname!=None:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
54 out.write(currSeqname+reverse_complement(currSeq,compl)+'\n')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
55
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
56 fin.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
57 out.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
58
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
59 def readrawseq(infile,outfile,compl):
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
60 '''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
61 each line is a sequence
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
62 '''
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
63 fin = open(infile)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
64 out = open(outfile,'w')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
65 for line in fin:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
66 out.write(reverse_complement(line.strip().upper(),compl)+'\n')
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
67 fin.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
68 out.close()
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
69
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
70 def main():
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
71 seqfile = sys.argv[1]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
72 outfile = sys.argv[2]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
73 fasta = sys.argv[3]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
74 rna = sys.argv[4]
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
75 if rna == 'rna':
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
76 compl = complrna
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
77 else:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
78 compl = compldna
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
79 if fasta == 'fasta':
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
80 readFastaFile(seqfile,outfile,compl)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
81 else:
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
82 readrawseq(seqfile,outfile,compl)
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
83
f0dc65e7f6c0 Uploaded
xuebing
parents:
diff changeset
84 main()