annotate BSseeker2/bs_align/bs_rrbs.py @ 0:e6df770c0e58 draft

Initial upload
author weilong-guo
date Fri, 12 Jul 2013 18:47:28 -0400
parents
children 8b26adf64adc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1 import fileinput, random, math, os.path
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
2 from bs_index.rrbs_build import FWD_MAPPABLE_REGIONS, REV_MAPPABLE_REGIONS
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
3 from bs_utils.utils import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
4
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
5 from bs_align.bs_single_end import extract_mapping
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
6 from bs_align_utils import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
7
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
8 def my_mappable_region(chr_regions, mapped_location, FR): # start_position (first C), end_position (last G), serial, sequence
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
9 #print len(chr_regions)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
10 out_serial = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
11 out_start = -1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
12 out_end = -1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
13 #print "mapped_location:", mapped_location
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
14 if FR == "+FW" or FR == "-RC":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
15 my_location = str(mapped_location)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
16 if my_location in chr_regions:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
17 my_lst = chr_regions[my_location]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
18 out_start = int(my_location)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
19 out_end = my_lst[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
20 out_serial = my_lst[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
21 #else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
22 # print "[For debug]: +FW location %s cannot be found" % my_location
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
23 elif FR == "-FW" or FR == "+RC":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
24 my_location = str(mapped_location)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
25 if my_location in chr_regions:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
26 my_lst = chr_regions[my_location]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
27 out_end = int(my_location)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
28 out_start = my_lst[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
29 out_serial = my_lst[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
30 #else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
31 # print "[For debug]: -FW location %s cannot be found" % my_location
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
32
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
33 return out_serial, out_start, out_end
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
34
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
35
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
36 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
37
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
38 def bs_rrbs(main_read_file, asktag, adapter_file, cut_s, cut_e, no_small_lines, max_mismatch_no,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
39 aligner_command, db_path, tmp_path, outfile, XS_pct, XS_count, adapter_mismatch, cut_format="C-CGG",
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
40 show_multiple_hit=False):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
41 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
42 # For double enzyme: cut_format="C-CGG,A-CTG"; ApekI:"G^CWGC"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
43 #cut_context = re.sub("-", "", cut_format)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
44 # Ex. cut_format="C-CGG,AT-CG,G-CWGC"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
45 """
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
46
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
47 :param main_read_file:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
48 :param asktag:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
49 :param adapter_file:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
50 :param cut_s:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
51 :param cut_e:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
52 :param no_small_lines:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
53 :param max_mismatch_no:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
54 :param aligner_command:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
55 :param db_path:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
56 :param tmp_path:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
57 :param outfile:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
58 :param XS_pct:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
59 :param XS_count:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
60 :param adapter_mismatch:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
61 :param cut_format:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
62 """
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
63 cut_format_lst = EnumerateIUPAC(cut_format.upper().split(",")) # ['G-CAGC', 'AT-CG', 'C-CGG', 'G-CTGC']
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
64 cut_context = [i.replace("-","") for i in cut_format_lst] # ['GCAGC', 'ATCG', 'CCGG', 'GCTGC']
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
65 cut5_context = [re.match( r'(.*)\-(.*)', i).group(1) for i in cut_format_lst] # ['G', 'AT', 'C', 'G']
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
66 cut3_context = [re.match( r'(.*)\-(.*)', i).group(2) for i in cut_format_lst] # ['CAGC', 'CG', 'CGG', 'CTGC']
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
67 cut_len = [len(i) for i in cut_context] # [5, 4, 4, 5]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
68 min_cut5_len = min([len(i) for i in cut5_context])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
69 #print cut_format_lst
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
70 #print cut_format
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
71 #print cut5_context
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
72
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
73 cut_tag_lst = Enumerate_C_to_CT(cut_format_lst) # ['G-TTGC', 'AT-TG', 'G-CAGT', 'T-CGG', 'G-TAGC', 'C-TGG', 'G-CAGC', 'G-CTGC', 'AT-CG', 'T-TGG', 'G-TTGT', 'G-TAGT', 'C-CGG', 'G-CTGT']
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
74 cut5_tag_lst = [re.match(r'(.*)\-(.*)', i).group(1) for i in cut_tag_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
75 cut3_tag_lst = [re.match(r'(.*)\-(.*)', i).group(2) for i in cut_tag_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
76 check_pattern = [ i[-2:]+"_"+j for i,j in zip(cut5_tag_lst, cut3_tag_lst) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
77
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
78 #print "======="
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
79 #print cut_tag_lst
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
80 #print cut3_tag_lst
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
81 #print cut5_tag_lst
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
82 #print check_pattern
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
83
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
84 # set region[gx,gy] for checking_genome_context
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
85 gx = [ 0 if j>2 else 2-j for j in [len(i) for i in cut5_tag_lst] ] # [XC-CGG]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
86 gy = [ 3+len(i) for i in cut3_tag_lst ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
87
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
88
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
89 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
90
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
91 # helper method to join fname with tmp_path
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
92 tmp_d = lambda fname: os.path.join(tmp_path, fname)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
93 db_d = lambda fname: os.path.join(db_path, fname)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
94
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
95 MAX_TRY = 500 # For finding the serial_no
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
96 whole_adapter_seq = ""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
97 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
98 adapter_seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
99 if adapter_file:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
100 try :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
101 adapter_inf = open(adapter_file,"r")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
102 whole_adapter_seq = adapter_inf.readline().strip()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
103 adapter_seq = whole_adapter_seq[0:10] # only use first 10bp of adapter
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
104 adapter_inf.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
105 except IOError:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
106 print "[Error] Cannot find adapter file : %s !" % adapter_file
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
107 exit(-1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
108
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
109 logm("I Read filename: %s" % main_read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
110 logm("I The last cycle (for mapping): %d" % cut_e )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
111 logm("I Bowtie path: %s" % aligner_command )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
112 logm("I Reference genome library path: %s" % db_path )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
113 logm("I Number of mismatches allowed: %s" % max_mismatch_no)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
114 logm("I Adapter seq: %s" % whole_adapter_seq)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
115 logm("----------------------------------------------")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
116
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
117 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
118 all_raw_reads=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
119 all_tagged=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
120 all_tagged_trimmed=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
121 all_mapped=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
122 all_mapped_passed=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
123 n_cut_tag_lst={}
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
124 #print cut3_tag_lst
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
125 for x in cut3_tag_lst:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
126 n_cut_tag_lst[x]=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
127
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
128 mC_lst=[0,0,0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
129 uC_lst=[0,0,0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
130
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
131 no_my_files=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
132
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
133 num_mapped_FW_C2T = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
134 num_mapped_RC_C2T = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
135 num_mapped_FW_G2A = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
136 num_mapped_RC_G2A = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
137
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
138 #===============================================
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
139 # directional sequencing
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
140 #===============================================
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
141
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
142 if asktag=="N" :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
143 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
144 logm("== Start mapping ==")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
145
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
146 input_fname = os.path.split(main_read_file)[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
147 for read_file in isplit_file(main_read_file, tmp_d(input_fname)+'-s-', no_small_lines):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
148
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
149 logm("Processing read file: %s" % read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
150 original_bs_reads = {}
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
151 no_my_files+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
152 random_id = ".tmp-"+str(random.randint(1000000,9999999))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
153 outfile2=tmp_d('Trimmed_C2T.fa'+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
154
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
155 outf2=open(outfile2,'w')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
156
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
157 #--- Checking input format ------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
158 try :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
159 read_inf=open(read_file,"r")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
160 except IOError:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
161 print "[Error] Cannot open input file : %s" % read_file
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
162 exit(-1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
163
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
164 oneline=read_inf.readline()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
165 l=oneline.split()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
166 n_fastq=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
167 n_fasta=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
168 input_format=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
169 if oneline[0]=="@": # FastQ
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
170 input_format="fastq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
171 elif len(l)==1 and oneline[0]!=">": # pure sequences
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
172 input_format="seq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
173 elif len(l)==11: # Illumina qseq
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
174 input_format="qseq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
175 elif oneline[0]==">": # fasta
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
176 input_format="fasta"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
177 read_inf.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
178
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
179 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
180 seq_id=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
181 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
182 seq_ready=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
183 for line in fileinput.input(read_file):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
184 l=line.split()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
185
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
186 if input_format=="seq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
187 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
188 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
189 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
190 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
191 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
192 elif input_format=="fastq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
193 m_fastq=math.fmod(n_fastq,4)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
194 n_fastq+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
195 seq_ready="N"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
196 if m_fastq==0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
197 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
198 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
199 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
200 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
201 elif m_fastq==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
202 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
203 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
204 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
205 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
206 elif input_format=="qseq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
207 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
208 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
209 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
210 seq=l[8]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
211 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
212 elif input_format=="fasta":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
213 m_fasta=math.fmod(n_fasta,2)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
214 n_fasta+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
215 seq_ready="N"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
216 if m_fasta==0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
217 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
218 seq_id=l[0][1:]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
219 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
220 elif m_fasta==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
221 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
222 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
223 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
224 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
225 #---------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
226 if seq_ready=="Y":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
227 # Normalize the characters
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
228 seq=seq.upper().replace(".","N")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
229
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
230 read_tag = [ m for m,n in [ (i, len(i)) for i in uniq(cut3_tag_lst)] if seq[0:n] == m ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
231 if len(read_tag) > 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
232 all_tagged += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
233 for i in read_tag :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
234 n_cut_tag_lst[i] += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
235
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
236 seq = seq[(cut_s-1):cut_e] # cut_s start from 1 cycle by default
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
237
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
238 #-- Trimming adapter sequence ---
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
239 if adapter_seq != "" :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
240 new_read = RemoveAdapter(seq, adapter_seq, adapter_mismatch)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
241 if len(new_read) < len(seq) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
242 all_tagged_trimmed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
243 seq = new_read
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
244 if len(seq) <= 4 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
245 seq = "N" * (cut_e - cut_s)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
246
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
247 # all reads will be considered, regardless of tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
248 #--------- trimmed_raw_BS_read and qscore ------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
249 original_bs_reads[seq_id] = seq
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
250 #--------- FW_C2T ------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
251 outf2.write('>%s\n%s\n'%(seq_id, seq.replace('C', 'T')))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
252 fileinput.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
253
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
254 outf2.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
255
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
256 delete_files(read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
257 logm("Processing input is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
258 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
259
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
260 # mapping
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
261 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
262 WC2T=tmp_d("W_C2T_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
263 CC2T=tmp_d("C_C2T_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
264
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
265 run_in_parallel([ aligner_command % {'reference_genome' : os.path.join(db_path,'W_C2T'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
266 'input_file' : outfile2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
267 'output_file' : WC2T},
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
268 aligner_command % {'reference_genome' : os.path.join(db_path,'C_C2T'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
269 'input_file' : outfile2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
270 'output_file' : CC2T} ])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
271
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
272 logm("Aligning reads is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
273
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
274 delete_files(outfile2)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
275
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
276 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
277 # Post processing
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
278 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
279
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
280 FW_C2T_U,FW_C2T_R=extract_mapping(WC2T)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
281 RC_C2T_U,RC_C2T_R=extract_mapping(CC2T)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
282 logm("Extracting alignments is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
283
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
284 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
285 # get uniq-hit reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
286 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
287 Union_set=set(FW_C2T_U.iterkeys()) | set(RC_C2T_U.iterkeys())
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
288
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
289 Unique_FW_C2T=set() # +
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
290 Unique_RC_C2T=set() # -
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
291 Multiple_hits=set()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
292
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
293
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
294 for x in Union_set:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
295 _list=[]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
296 for dx in [FW_C2T_U, RC_C2T_U]:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
297 mis_lst=dx.get(x,[99])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
298 mis=int(mis_lst[0])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
299 _list.append(mis)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
300 for dx in [FW_C2T_R, RC_C2T_R]:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
301 mis=dx.get(x,99)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
302 _list.append(mis)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
303 mini=min(_list)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
304 if _list.count(mini)==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
305 mini_index=_list.index(mini)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
306 if mini_index==0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
307 Unique_FW_C2T.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
308 elif mini_index==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
309 Unique_RC_C2T.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
310 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
311 Multiple_hits.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
312 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
313 Multiple_hits.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
314 # write reads rejected by Multiple Hits to file
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
315 if show_multiple_hit :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
316 outf_MH=open("Multiple_hit.fa",'w')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
317 for i in Multiple_hits :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
318 outf_MH.write(">%s\n" % i)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
319 outf_MH.write("%s\n" % original_bs_reads[i])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
320 outf_MH.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
321
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
322 del Union_set
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
323 del FW_C2T_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
324 del RC_C2T_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
325
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
326 FW_uniq_lst=[[FW_C2T_U[u][1],u] for u in Unique_FW_C2T]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
327 RC_uniq_lst=[[RC_C2T_U[u][1],u] for u in Unique_RC_C2T]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
328 FW_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
329 RC_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
330 FW_uniq_lst=[x[1] for x in FW_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
331 RC_uniq_lst=[x[1] for x in RC_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
332
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
333 del Unique_FW_C2T
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
334 del Unique_RC_C2T
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
335
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
336 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
337 # Post-filtering reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
338
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
339 # ---- FW ----
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
340 FW_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
341 gseq = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
342 chr_length = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
343 for header in FW_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
344 _, mapped_chr, mapped_location, cigar = FW_C2T_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
345 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
346 if mapped_chr not in FW_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
347 FW_regions[mapped_chr] = deserialize(db_d(FWD_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
348 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
349 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
350 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
351
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
352 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
353 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
354 FR = "+FW"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
355 mapped_strand = "+"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
356 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
357 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
358 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
359 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
360 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
361 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
362
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
363 if len(r_aln) == len(g_aln) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
364 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
365 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
366 try_pos = [mapped_location - len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
367 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
368 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
369 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
370 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
371 # print "[For debug]: FW read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
372 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
373 try_pos = mapped_location - min_cut5_len + 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
374 while my_region_serial == 0 and try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
375 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
376 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
377 try_pos -= 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
378 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
379
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
380 #if my_region_serial == 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
381 # print "[For debug]: chr=", mapped_chr
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
382 # print "[For debug]: +FW read still can not find fragment serial"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
383 # Tip: sometimes "my_region_serial" is still 0 ...
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
384
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
385
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
386 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
387 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
388 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
389 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
390 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
391 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
392 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
393 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
394 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
395 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
396 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
397 num_mapped_FW_C2T += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
398 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
399 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
400 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
401 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
402 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
403 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
404 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
405 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
406 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
407
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
408 #print "start RC"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
409 # ---- RC ----
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
410 RC_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
411 for header in RC_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
412 _, mapped_chr, mapped_location, cigar = RC_C2T_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
413 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
414 if mapped_chr not in RC_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
415 RC_regions[mapped_chr] = deserialize(db_d(REV_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
416 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
417 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
418 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
419
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
420 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
421 mapped_location = chr_length[mapped_chr] - mapped_location - g_len
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
422 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
423 FR = "-FW"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
424 mapped_strand = "-"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
425 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
426 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
427 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
428 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
429 #checking_genome_context = (output_genome[gx:gy] == check_pattern)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
430 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
431 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
432
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
433 if len(r_aln) == len(g_aln) : # and checking_genome_context:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
434 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
435 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
436 try_pos = [mapped_location + g_len - 1 + len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
437 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
438 try_pos , FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
439 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
440 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
441 #print "[For debug]: RC Read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
442 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
443 try_pos = mapped_location + g_len + min_cut5_len - 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
444 while my_region_serial == 0 and try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
445 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
446 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
447 try_pos += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
448 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
449
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
450 #if my_region_serial == 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
451 # print "[For debug]: chr=", mapped_chr
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
452 # print "[For debug]: -FW read still cannot find fragment serial"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
453
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
454
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
455 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
456 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
457 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
458 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
459 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
460 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
461 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
462 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
463 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
464 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
465 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
466 num_mapped_RC_C2T += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
467 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
468 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
469 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
470 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
471 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
472 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
473 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
474 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
475 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
476
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
477
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
478 # Finished both FW and RC
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
479 logm("Done: %s (%d) \n" % (read_file, no_my_files))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
480 print "--> %s (%d) "%(read_file, no_my_files)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
481 del original_bs_reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
482 delete_files(WC2T, CC2T)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
483
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
484 # End of directional library
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
485
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
486
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
487 # ====================================================
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
488 # un-directional library
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
489 # ====================================================
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
490
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
491 elif asktag=="Y" :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
492 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
493 logm("== Start mapping ==")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
494
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
495 input_fname = os.path.split(main_read_file)[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
496 for read_file in isplit_file(main_read_file, tmp_d(input_fname)+'-s-', no_small_lines):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
497
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
498 logm("Processing read file: %s" % read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
499 original_bs_reads = {}
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
500 no_my_files+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
501 random_id = ".tmp-"+str(random.randint(1000000,9999999))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
502 outfile2=tmp_d('Trimmed_C2T.fa'+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
503 outfile3=tmp_d('Trimmed_G2A.fa'+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
504
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
505 outf2=open(outfile2,'w')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
506 outf3=open(outfile3,'w')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
507
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
508 #--- Checking input format ------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
509 try :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
510 read_inf=open(read_file,"r")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
511 except IOError:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
512 print "[Error] Cannot open input file : %s" % read_file
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
513 exit(-1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
514
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
515 oneline=read_inf.readline()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
516 l=oneline.split()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
517 n_fastq=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
518 n_fasta=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
519 input_format=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
520 if oneline[0]=="@": # FastQ
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
521 input_format="fastq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
522 elif len(l)==1 and oneline[0]!=">": # pure sequences
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
523 input_format="seq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
524 elif len(l)==11: # Illumina qseq
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
525 input_format="qseq"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
526 elif oneline[0]==">": # fasta
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
527 input_format="fasta"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
528 read_inf.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
529
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
530 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
531 seq_id = ""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
532 seq = ""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
533 seq_ready=0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
534 for line in fileinput.input(read_file):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
535 l=line.split()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
536
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
537 if input_format == "seq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
538 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
539 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
540 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
541 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
542 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
543 elif input_format=="fastq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
544 m_fastq=math.fmod(n_fastq,4)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
545 n_fastq+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
546 seq_ready="N"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
547 if m_fastq==0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
548 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
549 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
550 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
551 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
552 elif m_fastq==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
553 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
554 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
555 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
556 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
557 elif input_format=="qseq":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
558 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
559 seq_id=str(all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
560 seq_id=seq_id.zfill(12)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
561 seq=l[8]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
562 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
563 elif input_format=="fasta":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
564 m_fasta=math.fmod(n_fasta,2)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
565 n_fasta+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
566 seq_ready="N"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
567 if m_fasta==0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
568 all_raw_reads+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
569 seq_id=l[0][1:]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
570 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
571 elif m_fasta==1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
572 seq=l[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
573 seq_ready="Y"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
574 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
575 seq=""
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
576 #---------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
577 if seq_ready=="Y":
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
578 # Normalize the characters
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
579 seq=seq.upper().replace(".","N")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
580
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
581 read_tag = [ m for m,n in [ (i, len(i)) for i in uniq(cut3_tag_lst)] if seq[0:n] == m ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
582 if len(read_tag) > 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
583 all_tagged += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
584 for i in read_tag :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
585 n_cut_tag_lst[i] += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
586
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
587 seq = seq[(cut_s-1):cut_e] # cut_s start from 1 cycle by default
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
588
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
589 #-- Trimming adapter sequence ---
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
590 if adapter_seq != "" :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
591 new_read = RemoveAdapter(seq, adapter_seq, adapter_mismatch)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
592 if len(new_read) < len(seq) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
593 all_tagged_trimmed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
594 seq = new_read
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
595 if len(seq) <= 4 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
596 seq = "N" * (cut_e - cut_s)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
597
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
598 # all reads will be considered, regardless of tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
599 #--------- trimmed_raw_BS_read and qscore ------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
600 original_bs_reads[seq_id] = seq
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
601 #--------- FW_C2T ------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
602 outf2.write('>%s\n%s\n'%(seq_id, seq.replace('C', 'T')))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
603 #--------- RC_G2A ------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
604 outf3.write('>%s\n%s\n' % (seq_id, seq.replace("G","A")))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
605 fileinput.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
606
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
607 outf2.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
608
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
609 delete_files(read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
610 logm("Processing input is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
611 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
612
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
613 # mapping
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
614 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
615 WC2T=tmp_d("W_C2T_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
616 CC2T=tmp_d("C_C2T_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
617 WG2A=tmp_d("W_G2A_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
618 CG2A=tmp_d("C_G2A_m"+max_mismatch_no+".mapping"+random_id)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
619
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
620 run_in_parallel([ aligner_command % {'reference_genome' : os.path.join(db_path,'W_C2T'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
621 'input_file' : outfile2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
622 'output_file' : WC2T},
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
623 aligner_command % {'reference_genome' : os.path.join(db_path,'C_C2T'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
624 'input_file' : outfile2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
625 'output_file' : CC2T},
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
626 aligner_command % {'reference_genome' : os.path.join(db_path,'W_G2A'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
627 'input_file' : outfile3,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
628 'output_file' : WG2A},
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
629 aligner_command % {'reference_genome' : os.path.join(db_path,'C_G2A'),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
630 'input_file' : outfile3,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
631 'output_file' : CG2A} ])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
632
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
633 logm("Aligning reads is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
634
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
635 delete_files(outfile2)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
636
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
637 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
638 # Post processing
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
639 #--------------------------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
640
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
641 FW_C2T_U,FW_C2T_R=extract_mapping(WC2T)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
642 RC_G2A_U,RC_G2A_R=extract_mapping(CG2A)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
643
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
644 FW_G2A_U,FW_G2A_R=extract_mapping(WG2A)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
645 RC_C2T_U,RC_C2T_R=extract_mapping(CC2T)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
646
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
647 logm("Extracting alignments is done")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
648
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
649 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
650 # get unique-hit reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
651 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
652 Union_set=set(FW_C2T_U.iterkeys()) | set(RC_G2A_U.iterkeys()) | set(FW_G2A_U.iterkeys()) | set(RC_C2T_U.iterkeys())
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
653
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
654 Unique_FW_C2T=set() # +
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
655 Unique_RC_G2A=set() # +
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
656 Unique_FW_G2A=set() # -
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
657 Unique_RC_C2T=set() # -
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
658 Multiple_hits=set()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
659
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
660 for x in Union_set:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
661 _list=[]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
662 for dx in [FW_C2T_U, RC_G2A_U, FW_G2A_U, RC_C2T_U]:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
663 mis_lst=dx.get(x,[99])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
664 mis=int(mis_lst[0])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
665 _list.append(mis)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
666 for dx in [FW_C2T_R, RC_G2A_R, FW_G2A_R, RC_C2T_R]:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
667 mis=dx.get(x,99)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
668 _list.append(mis)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
669 mini=min(_list)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
670 if _list.count(mini) == 1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
671 mini_index=_list.index(mini)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
672 if mini_index == 0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
673 Unique_FW_C2T.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
674 elif mini_index == 1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
675 Unique_RC_G2A.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
676 elif mini_index == 2:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
677 Unique_FW_G2A.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
678 elif mini_index == 3:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
679 Unique_RC_C2T.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
680 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
681 Multiple_hits.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
682 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
683 Multiple_hits.add(x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
684 # write reads rejected by Multiple Hits to file
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
685 if show_multiple_hit :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
686 outf_MH=open("Multiple_hit.fa",'w')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
687 for i in Multiple_hits :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
688 outf_MH.write(">%s\n" % i)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
689 outf_MH.write("%s\n" % original_bs_reads[i])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
690 outf_MH.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
691
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
692 del Union_set
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
693 del FW_C2T_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
694 del FW_G2A_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
695 del RC_C2T_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
696 del RC_G2A_R
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
697
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
698 FW_C2T_uniq_lst=[[FW_C2T_U[u][1],u] for u in Unique_FW_C2T]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
699 FW_G2A_uniq_lst=[[FW_G2A_U[u][1],u] for u in Unique_FW_G2A]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
700 RC_C2T_uniq_lst=[[RC_C2T_U[u][1],u] for u in Unique_RC_C2T]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
701 RC_G2A_uniq_lst=[[RC_G2A_U[u][1],u] for u in Unique_RC_G2A]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
702 FW_C2T_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
703 RC_C2T_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
704 FW_G2A_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
705 RC_G2A_uniq_lst.sort()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
706 FW_C2T_uniq_lst=[x[1] for x in FW_C2T_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
707 RC_C2T_uniq_lst=[x[1] for x in RC_C2T_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
708 FW_G2A_uniq_lst=[x[1] for x in FW_G2A_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
709 RC_G2A_uniq_lst=[x[1] for x in RC_G2A_uniq_lst]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
710
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
711 del Unique_FW_C2T
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
712 del Unique_FW_G2A
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
713 del Unique_RC_C2T
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
714 del Unique_RC_G2A
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
715
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
716
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
717 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
718 # Post-filtering reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
719 # ---- FW_C2T ---- undirectional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
720 FW_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
721 gseq = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
722 chr_length = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
723 for header in FW_C2T_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
724 _, mapped_chr, mapped_location, cigar = FW_C2T_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
725 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
726 if mapped_chr not in FW_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
727 FW_regions[mapped_chr] = deserialize(db_d(FWD_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
728 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
729 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
730 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
731
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
732 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
733 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
734 FR = "+FW"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
735 mapped_strand = "+"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
736 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
737 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
738 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
739 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
740 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
741 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
742
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
743 if len(r_aln) == len(g_aln) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
744 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
745 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
746 try_pos = [mapped_location - len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
747 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
748 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
749 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
750 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
751 # print "[For debug]: FW read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
752 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
753 try_pos = mapped_location - min_cut5_len + 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
754 while my_region_serial == 0 and try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
755 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
756 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
757 try_pos -= 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
758 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
759
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
760 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
761 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
762 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
763 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
764 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
765 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
766 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
767 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
768 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
769 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
770 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
771 num_mapped_FW_C2T += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
772 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
773 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
774 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
775 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
776 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
777 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
778 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
779 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
780 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
781
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
782
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
783 # ---- RC_C2T ---- undirectional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
784 RC_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
785 for header in RC_C2T_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
786 _, mapped_chr, mapped_location, cigar = RC_C2T_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
787 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
788 if mapped_chr not in RC_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
789 RC_regions[mapped_chr] = deserialize(db_d(REV_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
790 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
791 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
792 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
793
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
794 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
795 mapped_location = chr_length[mapped_chr] - mapped_location - g_len
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
796 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
797 FR = "-FW"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
798 mapped_strand = "-"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
799 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
800 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
801 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
802 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
803 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
804 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
805
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
806 if len(r_aln) == len(g_aln) : # and checking_genome_context:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
807 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
808 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
809 try_pos = [mapped_location + g_len - 1 + len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
810 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
811 try_pos , FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
812 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
813 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
814 #print "[For debug]: RC Read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
815 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
816 try_pos = mapped_location + g_len + min_cut5_len - 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
817 while my_region_serial == 0 and try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
818 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
819 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
820 try_pos += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
821 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
822
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
823 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
824 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
825 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
826 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
827 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
828 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
829 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
830 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
831 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
832 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
833 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
834 num_mapped_RC_C2T += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
835 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
836 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
837 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
838 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
839 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
840 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
841 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
842
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
843 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
844 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
845
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
846
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
847 # ---- FW_G2A ---- undirectional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
848 FW_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
849 gseq = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
850 chr_length = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
851 for header in FW_G2A_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
852 _, mapped_chr, mapped_location, cigar = FW_G2A_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
853 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
854 if mapped_chr not in FW_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
855 FW_regions[mapped_chr] = deserialize(db_d(FWD_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
856 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
857 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
858 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
859 cigar = list(reversed(cigar))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
860
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
861 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
862 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
863 FR = "-RC"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
864 mapped_strand = "-"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
865 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
866 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
867 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
868 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
869 original_BS = reverse_compl_seq(original_BS) # for RC reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
870 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
871 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
872
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
873 if len(r_aln) == len(g_aln) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
874 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
875 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
876 try_pos = [mapped_location - len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
877 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
878 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
879 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
880 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
881 #print "[For debug]: FW read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
882 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
883 try_pos = mapped_location - min_cut5_len + 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
884 while my_region_serial == 0 and try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
885 my_region_serial, my_region_start, my_region_end = my_mappable_region(FW_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
886 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
887 try_pos += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
888 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
889 #if my_region_serial == 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
890 # print "[For debug]: chr=", mapped_chr
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
891 # print "[For debug]: FW_G2A read still can not find fragment serial"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
892 # Tip: sometimes "my_region_serial" is still 0 ...
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
893
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
894
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
895 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
896 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
897 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
898 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
899 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
900 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
901 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
902 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
903 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
904 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
905 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
906 num_mapped_FW_G2A += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
907 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
908 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
909 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
910 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
911 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
912 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
913 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
914 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
915 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
916
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
917
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
918 # ---- RC_G2A ---- undirectional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
919 RC_regions = dict()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
920 for header in RC_G2A_uniq_lst :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
921 _, mapped_chr, mapped_location, cigar = RC_G2A_U[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
922 original_BS = original_bs_reads[header]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
923 if mapped_chr not in RC_regions :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
924 RC_regions[mapped_chr] = deserialize(db_d(REV_MAPPABLE_REGIONS(mapped_chr)))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
925 if mapped_chr not in gseq :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
926 gseq[mapped_chr] = deserialize(db_d(mapped_chr))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
927 chr_length[mapped_chr] = len(gseq[mapped_chr])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
928 cigar = list(reversed(cigar))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
929
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
930 r_start, r_end, g_len = get_read_start_end_and_genome_length(cigar)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
931 mapped_location = chr_length[mapped_chr] - mapped_location - g_len
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
932 all_mapped+=1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
933 FR = "+RC"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
934 mapped_strand = "+"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
935 origin_genome, next2bp, output_genome = get_genomic_sequence(gseq[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
936 mapped_location,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
937 mapped_location + g_len,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
938 mapped_strand)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
939 original_BS = reverse_compl_seq(original_BS) # for RC reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
940 checking_genome_context = [output_genome[i:j] == k for i,j,k in zip(gx,gy,check_pattern) ]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
941 r_aln, g_aln = cigar_to_alignment(cigar, original_BS, origin_genome)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
942
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
943 if len(r_aln) == len(g_aln) : # and checking_genome_context:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
944 my_region_serial, my_region_start, my_region_end = [-1, 0, 0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
945 if True in checking_genome_context :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
946 try_pos = [mapped_location + g_len - 1 + len(i) for i,j in zip(cut5_tag_lst, checking_genome_context) if j][0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
947 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
948 mapped_location + g_len + min_cut5_len -1, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
949 if my_region_serial == 0 : # still be 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
950 # for some cases, read has no tags; searching the upstream sequence for tags
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
951 #print "[For debug]: RC Read has no tags"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
952 try_count = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
953 try_pos = mapped_location + g_len + min_cut5_len - 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
954 while try_count < MAX_TRY :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
955 my_region_serial, my_region_start, my_region_end = my_mappable_region(RC_regions[mapped_chr],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
956 try_pos, FR)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
957 try_pos += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
958 try_count += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
959
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
960 #if my_region_serial == 0 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
961 # print "[For debug]: chr=", mapped_chr
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
962 # print "[For debug]: RC_C2A read still cannot find fragment serial"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
963
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
964
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
965 N_mismatch = N_MIS(r_aln, g_aln)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
966 if N_mismatch <= int(max_mismatch_no) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
967 all_mapped_passed += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
968 methy = methy_seq(r_aln, g_aln + next2bp)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
969 mC_lst, uC_lst = mcounts(methy, mC_lst, uC_lst)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
970 #---XS FILTER----------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
971 XS = 0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
972 nCH = methy.count('y') + methy.count('z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
973 nmCH = methy.count('Y') + methy.count('Z')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
974 if( (nmCH>XS_count) and nmCH/float(nCH+nmCH)>XS_pct ) :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
975 XS = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
976 num_mapped_RC_G2A += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
977 outfile.store(header, N_mismatch, FR, mapped_chr, mapped_strand,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
978 mapped_location, cigar, original_BS, methy, XS,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
979 output_genome = output_genome,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
980 rrbs = True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
981 my_region_serial = my_region_serial,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
982 my_region_start = my_region_start,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
983 my_region_end = my_region_end)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
984 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
985 print "[For debug]: reads not in same lengths"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
986
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
987
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
988
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
989 # Finished both FW and RC
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
990 logm("Done: %s (%d) \n" % (read_file, no_my_files))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
991 print "--> %s (%d) "%(read_file, no_my_files)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
992 del original_bs_reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
993 delete_files(WC2T, CC2T, WG2A, CG2A)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
994
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
995
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
996
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
997 # End of un-directional library
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
998
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
999 delete_files(tmp_path)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1000
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1001
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1002 logm("O Number of raw reads: %d "% all_raw_reads)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1003 if all_raw_reads >0:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1004 logm("O Number of CGG/TGG tagged reads: %d (%1.3f)"%(all_tagged,float(all_tagged)/all_raw_reads))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1005 for kk in range(len(n_cut_tag_lst)):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1006 logm("O Number of raw reads with %s tag: %d (%1.3f)"%(cut3_tag_lst[kk],n_cut_tag_lst[cut3_tag_lst[kk]],float(n_cut_tag_lst[cut3_tag_lst[kk]])/all_raw_reads))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1007 logm("O Number of CGG/TGG reads having adapter removed: %d "%all_tagged_trimmed)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1008 logm("O Number of reads rejected because of multiple hits: %d\n" % len(Multiple_hits) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1009 logm("O Number of unique-hits reads for post-filtering: %d"%all_mapped)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1010
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1011 logm("O ------ %d uniquely aligned reads, passed fragment check, with mismatches <= %s"%(all_mapped_passed, max_mismatch_no))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1012 logm("O Mappability= %1.4f%%"%(100*float(all_mapped_passed)/all_raw_reads))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1013
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1014 if asktag=="Y": # undiretional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1015 logm(" ---- %7d FW reads mapped to Watson strand"%(num_mapped_FW_C2T) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1016 logm(" ---- %7d RC reads mapped to Watson strand"%(num_mapped_FW_G2A) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1017 logm(" ---- %7d FW reads mapped to Crick strand"%(num_mapped_RC_C2T) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1018 logm(" ---- %7d RC reads mapped to Crick strand"%(num_mapped_RC_G2A) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1019 # the variable name 'num_mapped_RC_G2A' seems not consistent with illustration
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1020 # according to literal meaning
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1021 elif asktag=="N": # directional
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1022 logm(" ---- %7d FW reads mapped to Watson strand"%(num_mapped_FW_C2T) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1023 logm(" ---- %7d FW reads mapped to Crick strand"%(num_mapped_RC_C2T) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1024
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1025 n_CG=mC_lst[0]+uC_lst[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1026 n_CHG=mC_lst[1]+uC_lst[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1027 n_CHH=mC_lst[2]+uC_lst[2]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1028
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1029 logm("----------------------------------------------")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1030 logm("M Methylated C in mapped reads ")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1031 logm("M mCG %1.3f%%"%((100*float(mC_lst[0])/n_CG) if n_CG != 0 else 0))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1032 logm("M mCHG %1.3f%%"%((100*float(mC_lst[1])/n_CHG) if n_CHG != 0 else 0))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1033 logm("M mCHH %1.3f%%"%((100*float(mC_lst[2])/n_CHH) if n_CHH != 0 else 0))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1034 logm("----------------------------------------------")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1035 logm("------------------- END ----------------------")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1036
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1037 elapsed(main_read_file)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1038
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1039 close_log()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1040
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1041