1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#!/usr/bin/python3
import sys, os
import string, re
use_message = '''
'''
def extract_pair(RNA):
read_dic = {}
pair_reported = set()
out_file = open("sim_paired.sam", "w")
hits_file = open("sim.sam")
for line in hits_file:
if line.startswith('@'):
continue
fields = line[:-1].split()
read_id, flag, chr1, pos1, mapQ, cigar1, chr2, pos2 = fields[:8]
flag = int(flag)
assert flag & 0x4 == 0
pos1, pos2 = int(pos1), int(pos2)
TI, NM1, Zs1 = "", "", ""
for field in fields[11:]:
if field.startswith("TI"):
TI = "\t" + field
elif field.startswith("NM"):
NM1 = "\t" + field
elif field.startswith("Zs"):
Zs1 = "\t" + field
assert NM1 != ""
assert chr1 == chr2
me = "%s\t%s\t%d" % (read_id, chr1, pos1)
partner = "%s\t%s\t%d" % (read_id, chr2, pos2)
if partner in read_dic:
maps = read_dic[partner]
for map in maps:
if map[0] == me:
cigar2, NM2, Zs2 = map[1:4]
if int(pos2) > int(pos1):
p_str = "%s\t%s\t%d\t%s\t%s\t%d\t%s%s%s%s%s%s" % \
(read_id, chr1, pos1, cigar1, chr2, pos2, cigar2, TI, NM1, NM2, Zs1, Zs2)
else:
p_str = "%s\t%s\t%d\t%s\t%s\t%d\t%s%s%s%s%s%s" % \
(read_id, chr2, pos2, cigar2, chr1, pos1, cigar1, TI, NM2, NM1, Zs2, Zs1)
if p_str not in pair_reported:
pair_reported.add(p_str)
print(p_str, file=out_file)
if not me in read_dic:
read_dic[me] = []
read_dic[me].append([partner, cigar1, NM1, Zs1])
hits_file.close()
out_file.close()
def init_reads(read_dir, RNA):
sim1_file = open("sim_1.sam", 'w')
sim2_file = open("sim_2.sam", 'w')
for line in open("sim.sam"):
if line.startswith('@'):
continue
fields = line[:-1].split('\t')
read_id, flag, chr, pos, _, cigar = fields[:6]
left_read = int(flag) < 128
NM, TI, Zs = "", "", ""
for field in fields:
if field.startswith("NM"):
NM = "\t" + field
elif field.startswith("TI"):
TI = "\t" + field
elif field.startswith("Zs"):
Zs = "\t" + field
print("%s\t%s\t%s\t%s%s%s%s" % (read_id, chr, pos, cigar, TI, NM, Zs), file=(sim1_file if left_read else sim2_file))
sim1_file.close()
sim2_file.close()
extract_pair(RNA)
def to_junction_str(junction):
return "%s-%d-%d" % (junction[0], junction[1], junction[2])
def to_junction(junction_str):
fields = junction_str.split("-")
if len(fields) > 3:
chr, left, right = "-".join(fields[:-2]), fields[-2], fields[-1]
else:
assert len(fields) == 3
chr, left, right = fields
return [chr, int(left), int(right)]
def junction_cmp(a, b):
if a[0] != b[0]:
if a[0] < b[0]:
return -1
else:
return 1
if a[1] != b[1]:
if a[1] < b[1]:
return -1
else:
return 1
if a[2] != b[2]:
if a[2] < b[2]:
return -1
else:
return 1
return 0
def classify_reads(RNA):
if RNA:
readtypes = ["all", "M", "2M_gt_15", "2M_8_15", "2M_1_7", "gt_2M"]
else:
readtypes = ["all" ,"M"]
readtype_order = {}
for i in range(len(readtypes)):
readtype = readtypes[i]
assert readtype not in readtype_order
readtype_order[readtype] = i
for paired in [False, True]:
for readtype in readtypes:
if paired:
base_fname = "sim_paired"
type_sam_fname = base_fname + "_" + readtype + ".sam"
type_read1_fname = base_fname + "_1_" + readtype + ".fa"
type_read2_fname = base_fname + "_2_" + readtype + ".fa"
type_junction_fname = base_fname + "_" + readtype + ".junc"
else:
base_fname = "sim_single"
type_sam_fname = base_fname + "_" + readtype + ".sam"
type_read1_fname = base_fname + "_" + readtype + ".fa"
type_read2_fname = ""
type_junction_fname = base_fname + "_" + readtype + ".junc"
if os.path.exists(type_sam_fname) and \
os.path.exists(type_junction_fname):
continue
read_ids = set()
junctions = []
type_sam_file = open(type_sam_fname, "w")
if paired:
sam_file = open("sim_paired.sam")
else:
sam_file = open("sim_1.sam")
cigar_re = re.compile('\d+\w')
def get_read_type(cigar):
cigars = cigar_re.findall(cigar)
def get_cigar_chars_MN(cigars):
cigars = cigar_re.findall(cigars)
cigar_chars = ""
for cigar in cigars:
cigar_op = cigar[-1]
if cigar_op in "MN":
if cigar_chars == "" or cigar_chars[-1] != cigar_op:
cigar_chars += cigar_op
return cigar_chars
cigar_str = get_cigar_chars_MN(cigar)
if cigar_str == "M":
return cigar_str
elif cigar_str == "MNM":
assert len(cigars) >= 3
left_anchor = 0
for ci in range(len(cigars)):
c = cigars[ci][-1]
c_len = int(cigars[ci][:-1])
if c in "MI":
left_anchor += c_len
else:
break
assert left_anchor > 0
right_anchor = 0
for ci in reversed(list(range(len(cigars)))):
c = cigars[ci][-1]
c_len = int(cigars[ci][:-1])
if c in "MI":
right_anchor += c_len
else:
break
assert right_anchor > 0
min_anchor = min(left_anchor, right_anchor)
if min_anchor > 15:
return "2M_gt_15"
elif min_anchor >= 8 and min_anchor <= 15:
return "2M_8_15"
elif min_anchor >= 1 and min_anchor <= 7:
return "2M_1_7"
else:
assert False
else:
assert cigar_str not in ["M", "MNM"]
return "gt_2M"
# chr and pos are assumed to be integers
def get_junctions(chr, pos, cigar_str):
junctions = []
right_pos = pos
cigars = cigar_re.findall(cigar_str)
cigars = [[int(cigars[i][:-1]), cigars[i][-1]] for i in range(len(cigars))]
for i in range(len(cigars)):
length, cigar_op = cigars[i]
if cigar_op == "N":
left, right = right_pos - 1, right_pos + length
if i > 0 and cigars[i-1][1] in "ID":
if cigars[i-1][1] == "I":
left += cigars[i-1][0]
else:
left -= cigars[i-1][0]
if i + 1 < len(cigars) and cigars[i+1][1] in "ID":
if cigars[i+1][1] == "I":
right -= cigars[i+1][0]
else:
right += cigars[i+1][0]
junctions.append([chr, left, right])
if cigar_op in "MND":
right_pos += length
return junctions
for line in sam_file:
fields = line[:-1].split()
if paired:
read_id, chr, pos, cigar, chr2, pos2, cigar2 = fields[:7]
else:
read_id, chr, pos, cigar = fields[:4]
read_id = int(read_id)
readtype2 = get_read_type(cigar)
if paired:
readtype3 = get_read_type(cigar2)
assert readtype2 in readtype_order
assert readtype3 in readtype_order
if readtype_order[readtype2] < readtype_order[readtype3]:
readtype2 = readtype3
if readtype == "all" or readtype == readtype2:
read_ids.add(read_id)
print(line[:-1], file=type_sam_file)
junctions += get_junctions(chr, int(pos), cigar)
if paired:
junctions += get_junctions(chr2, int(pos2), cigar2)
sam_file.close()
type_sam_file.close()
# make this set non-redundant
junction_set = set()
for junction in junctions:
junction_set.add(to_junction_str(junction))
junctions = []
for junction_str in junction_set:
junctions.append(to_junction(junction_str))
# sort the list of junctions
junctions = sorted(junctions, cmp=junction_cmp)
# write the junctions into a file
type_junction_file = open(type_junction_fname, "w")
for junction in junctions:
print("%s\t%d\t%d" % (junction[0], junction[1], junction[2]), file=type_junction_file)
type_junction_file.close()
def write_reads(read_fname, type_read_fname):
type_read_file = open(type_read_fname, "w")
read_file = open(read_fname)
write = False
for line in read_file:
if line[0] == ">":
read_id = int(line[1:-1])
write = read_id in read_ids
if write:
print(line[:-1], file=type_read_file)
read_file.close()
type_read_file.close()
if paired:
write_reads("sim_1.fa", type_read1_fname)
write_reads("sim_2.fa", type_read2_fname)
else:
write_reads("sim_1.fa", type_read1_fname)
def init():
read_dir_base = "../reads/simulation/"
read_dirs = os.listdir(read_dir_base)
for read_dir in read_dirs:
if os.path.exists(read_dir):
continue
if not os.path.exists(read_dir_base + read_dir + "/sim.sam") or \
not os.path.exists(read_dir_base + read_dir + "/sim_1.fa") or \
not os.path.exists(read_dir_base + read_dir + "/sim_2.fa"):
continue
print("Processing", read_dir, "...", file=sys.stderr)
os.mkdir(read_dir)
os.chdir(read_dir)
os.system("ln -s ../%s%s/* ." % (read_dir_base, read_dir))
os.system("ln -s sim_1.fa 1.fa")
os.system("ln -s sim_2.fa 2.fa")
RNA = (read_dir.find("RNA") != -1)
init_reads(read_dir, RNA)
classify_reads(RNA)
os.system("ln -s ../calculate_read_cost.py .")
os.chdir("..")
if __name__ == "__main__":
init()
|