forked from kkrizanovic/RNAseqEval
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Process_pbsim_data.py
executable file
·749 lines (620 loc) · 30 KB
/
Process_pbsim_data.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#! /usr/bin/python
import sys, os
import paramsparser
from datetime import datetime
# To enable importing from samscripts submodulew
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(SCRIPT_PATH, 'samscripts/src'))
import utility_sam
import Annotation_formats
import RNAseqEval
from report import EvalReport, ReportType
from RNAseq_benchmark import benchmark_params
from fastqparser import read_fastq
# Determines whether to check the strand whene analyzing data
# Due to complications in generating simulated RNA reads, this is False
P_CHECK_STRAND = False
# OLD: Predefined dictionaries for analyzing different datasets
# simFolderDict_d1 = {'SimG1' : 'group1'
# , 'SimG2' : 'group2'
# , 'SimG3' : 'group3'}
#
# simFolderDict_all = {'SimG1' : 'group1'
# , 'SimG2' : 'group2'
# , 'SimG3' : 'group3'
# , 'SimG1AS' : 'group1_AS'
# , 'SimG1SS' : 'group1_SS'
# , 'SimG2AS' : 'group2_AS'
# , 'SimG2SS' : 'group2_SS'
# , 'SimG3AS' : 'group3_AS'
# , 'SimG3SS' : 'group3_SS'}
# A dictionary connecting fasta/fastq header prefix with the folder with pbsim generated data
# Containing information for reads with each prefix
# This is used because data is simulated using several pbsim runs to get different
# coverages for different sets of references (in this case transcripts)
# NOTE: this should be changed for different simulations
simFolderDict = benchmark_params.simFolderDict
paramdefs = {'--version' : 0,
'-v' : 0,
'--split-qnames' : 1,
'-sqn' : 0,
'--save_query_names' : 0,
'--debug' : 0,
'--print_mapping' : 1,
'--alowed_inaccurycy' : 1,
'-ai' : 1,
'--min_overlap' : 1,
'-mo' : 1}
# Obsolete
def interval_equals(interval1, interval2, allowed_inacc = Annotation_formats.DEFAULT_ALLOWED_INACCURACY, min_overlap = Annotation_formats.DEFAULT_MINIMUM_OVERLAP):
if interval1[0] < interval2[0] - allowed_inacc:
return False
if interval1[0] > interval2[0] + allowed_inacc:
return False
if interval1[1] < interval2[1] - allowed_inacc:
return False
if interval1[1] > interval2[1] + allowed_inacc:
return False
return True
# Obsolete
def interval_overlaps(interval1, interval2, allowed_inacc = Annotation_formats.DEFAULT_ALLOWED_INACCURACY, min_overlap = Annotation_formats.DEFAULT_MINIMUM_OVERLAP):
if (interval1[1] <= interval2[0] + min_overlap) or (interval1[0] >= interval2[1] - min_overlap):
return False
else:
return True
def processData(datafolder, resultfile, annotationfile, paramdict):
split_qnames = False
filename = ''
if '--split-qnames' in paramdict:
split_qnames = True
filename = paramdict['--split-qnames'][0]
filename_correct = filename + '_correct.names'
filename_hitall = filename + '_hitall.names'
filename_hitone = filename + '_hitone.names'
filename_bad = filename + '_incorrect.names'
filename_unmapped = filename + '_unmapped.names'
printMap = False
filename_mapping = ''
if '--print_mapping' in paramdict:
filename_mapping = paramdict['--print_mapping'][0]
printMap = True
file_correct = None
file_hitall = None
file_hitone = None
file_bad = None
file_unmapped = None
folder = os.getcwd()
# If splittng qnames into files, have to open files first
if split_qnames:
file_correct = open(os.path.join(folder, filename_correct), 'w+')
file_hitall = open(os.path.join(folder, filename_hitall), 'w+')
file_hitone = open(os.path.join(folder, filename_hitone), 'w+')
file_bad = open(os.path.join(folder, filename_bad), 'w+')
# Loading results SAM file
report = EvalReport(ReportType.FASTA_REPORT) # not really needed, used for unmapped query names
# Have to preserve the paramdict
# paramdict = {}
sys.stderr.write('\n(%s) Loading and processing SAM file with mappings ... ' % datetime.now().time().isoformat())
all_sam_lines = RNAseqEval.load_and_process_SAM(resultfile, paramdict, report, BBMapFormat = True)
# Reading annotation file
annotations = Annotation_formats.Load_Annotation_From_File(annotationfile)
s_num_multiexon_genes = 0
mapfile = None
if printMap:
mapfile = open(filename_mapping, 'w+')
# Hashing annotations according to name
annotation_dict = {}
for annotation in annotations:
if annotation.genename in annotation_dict:
sys.stderr.write('\nWARNING: anotation with name %s already in the dictionary!' % annotation.genename)
else:
annotation_dict[annotation.genename] = annotation
if len(annotation.items) > 1:
s_num_multiexon_genes += 1
# Statistical information for evaluating the qualitiy of mapping
s_gene_hits = 0
s_gene_misses = 0
s_whole_alignment_hits = 0
s_whole_alignment_misses = 0
s_partial_alignment_hits = 0
s_partial_alignment_misses = 0
s_num_start_hits = 0
s_num_end_hits = 0
s_num_start_end_hits = 0
s_num_fw_strand = 0
s_num_rv_strand = 0
s_num_split_alignment = 0
s_num_oversplit_alignment = 0 # Alignments that have more parts than exons
s_num_good_alignments = 0
s_num_badchrom_alignments = 0
s_maf_suspicious_alignments = 0
s_maf_bad_alignments = 0
s_maf_good_alignments = 0
s_maf_split_reads = 0
s_maf_good_split_alignments = 0
s_maf_bad_split_alignments = 0
s_maf_hit_all_parts = 0
s_maf_hit_one_part = 0
s_maf_eq_one_part = 0
s_maf_multihit_parts = 0
s_maf_split_hit_all_parts = 0
s_maf_split_hit_one_part = 0
s_maf_split_eq_one_part = 0
s_maf_miss_alignment = 0
s_maf_too_many_alignments = 0
s_num_potential_bad_strand = 0
allowed_inacc = Annotation_formats.DEFAULT_ALLOWED_INACCURACY # Allowing some shift in positions
min_overlap = Annotation_formats.DEFAULT_MINIMUM_OVERLAP # Minimum overlap that is considered
# Setting allowed_inaccuracy from parameters
if '--allowed_inacc' in paramdict:
allowed_inacc = int(paramdict['--allowed_inacc'][0])
elif '-ai' in paramdict:
allowed_inacc = int(paramdict['-ai'][0])
# Setting minimum overlap from parameters
if '--allowed_inacc' in paramdict:
min_overlap = int(paramdict['--allowed_inacc'][0])
elif '-mo' in paramdict:
min_overlap = int(paramdict['-mo'][0])
# All samlines in a list should have the same query name
for samline_list in all_sam_lines:
qname = samline_list[0].qname
isSplitAlignment = False
if len(samline_list) > 1:
s_num_split_alignment += 1
isSplitAlignment = True
# Checking the SAM file if all samlines in a list have the same qname
for samline in samline_list[1:]:
if samline.qname != qname:
sys.stderr.write('\nWARNING: two samlines in the same list with different query names (%s/%s)' % (qname, samline.qname))
# Look for the first underscore in query name
# Everything before that is the simulation folder name
# Everything after that is simulated query name
pos = qname.find('_')
if pos < 0:
raise Exception('Invalid query name in results file (%s)!' % qname)
simFolderKey = qname[:pos]
if simFolderKey not in simFolderDict:
# import pdb
# pdb.set_trace()
raise Exception('Bad simulation folder short name (%s)!' % simFolderKey)
simFolder = simFolderDict[simFolderKey]
simQName = qname[pos+1:]
# Due to error in data preparation, have to make some extra processing
if simQName[:6] == 'SimG2_':
simQName = simQName[6:]
# if simFolderKey == 'SimG1':
# simFileSuffix = 'g1'
# elif simFolderKey == 'SimG2':
# simFileSuffix = 'g2'
# elif simFolderKey == 'SimG3':
# simFileSuffix = 'g3'
# else:
# simFileSuffix = 'sd'
simFileSuffix = 'sd'
pos = simQName.find('_')
pos2 = simQName.find('_part')
if pos < 0:
raise Exception('Invalid simulated query name in results file (%s)!' % simQName)
simQLetter = simQName[0] # Should always be S
# BBMap separates a query into smaller parts he can manage
# Extends query with '_part_#', which has to be ignored
if pos2 <> -1:
simQName = simQName[:pos2]
simRefNumber = int(simQName[1:pos])
simQNumber = int(simQName[pos+1:])
simFileName = simFileSuffix + '_%04d' % simRefNumber
simRefFileName = simFileName + '.ref'
simSeqFileName = simFileName + '.fastq'
simMafFileName = simFileName + '.maf'
simFilePath = os.path.join(datafolder, simFolder)
simRefFilePath = os.path.join(simFilePath, simRefFileName)
simSeqFilePath = os.path.join(simFilePath, simSeqFileName)
simMafFilePath = os.path.join(simFilePath, simMafFileName)
if not os.path.exists(simRefFilePath):
# import pdb
# pdb.set_trace()
raise Exception('Reference file for simulated read %s does not exist!' % qname)
if not os.path.exists(simSeqFilePath):
# import pdb
# pdb.set_trace()
raise Exception('Sequence file for simulated read %s does not exist!' % qname)
if not os.path.exists(simMafFilePath):
# import pdb
# pdb.set_trace()
raise Exception('Sequence alignment (MAF) for simulated read %s does not exist!' % qname)
# Reading reference file
[headers, seqs, quals] = read_fastq(simRefFilePath)
simGeneName = headers[0]
annotation = annotation_dict[simGeneName] # Getting the correct annotation
if len(samline_list) > len(annotation.items):
# sys.stderr.write('\nWARNING: A number of partial alignments exceeds the number of exons for query %s! (%d / %d)' % (qname, len(samline_list), len(annotation.items)))
s_num_oversplit_alignment += 1
# Reading MAF file to get original position and length of the simulated read
# Query name should be a second item
maf_startpos = maf_length = 0
maf_strand = '0'
maf_reflen = 0
i = 0
with open(simMafFilePath, 'rU') as maffile:
i += 1
for line in maffile:
if line[0] == 's':
elements = line.split()
maf_qname = elements[1]
if maf_qname == 'ref': # Have to remember data for the last reference before the actual read
maf_startpos = int(elements[2])
maf_length = int(elements[3])
maf_strand = elements[4]
maf_reflen = int(elements[5])
if maf_qname == simQName:
# maf_startpos = int(elements[2])
# maf_length = int(elements[3])
break
if maf_qname != simQName:
# import pdb
# pdb.set_trace()
raise Exception('ERROR: could not find query %s in maf file %s' % (qname, simMafFileName))
# IMPORTANT: If the reads were generated from an annotation on reverse strand
# expected partial alignments must be reversed
if annotation.strand == Annotation_formats.GFF_STRANDRV:
maf_startpos = maf_reflen - maf_length - maf_startpos
# Saving "maf_length" and "maf_startpos" to be able to check it later
t_maf_length = maf_length
t_maf_startpos = maf_startpos
# Calculating expected partial alignmetns from MAF and annotations
# 1. Calculating the index of the first exon
# i - the index of exon currently being considered
i = 0
while annotation.items[i].getLength() < maf_startpos:
maf_startpos -= annotation.items[i].getLength()
i += 1
# Calculating expected partial alignments by filling up exons using maf_length
expected_partial_alignments = []
while maf_length > 0:
start = annotation.items[i].start + maf_startpos
end = annotation.items[i].end
assert start <= end
# OLD: length = end-start+1
# KK: End is already indicating position after the last base, so adding one when callculating length is not correct
length = end - start
if length <= maf_length:
expected_partial_alignments.append((start, end))
maf_length -= length
i += 1
else:
expected_partial_alignments.append((start, start + maf_length))
maf_length = 0
i += 1
# Start position should only be considered for the first exon
maf_startpos = 0
# import pdb
# pdb.set_trace()
numparts = len(expected_partial_alignments)
# For each part of expected partial alignments, these maps will count
# how many real partial alignments overlap or equal it
parthitmap = {(i+1):0 for i in xrange(numparts)}
parteqmap = {(i+1):0 for i in xrange(numparts)}
isSplitRead = False
if len(expected_partial_alignments) > 1:
s_maf_split_reads += 1
isSplitRead = True
oneHit = False
allHits = False
oneEq = False
multiHit = False
good_alignment = False
has_miss_alignments = False
if RNAseqEval.getChromName(samline_list[0].rname) != RNAseqEval.getChromName(annotation.seqname):
# import pdb
# pdb.set_trace()
s_num_badchrom_alignments += 1
else:
if len(samline_list) != len(expected_partial_alignments):
# sys.stderr.write('\nWARNING: suspicious number of alignments for query %s!' % qname)
s_maf_suspicious_alignments += 1
# import pdb
# pdb.set_trace()
good_alignment = True
k = 0
for samline in samline_list:
# sl_startpos = samline.pos - 1 # SAM positions are 1-based
sl_startpos = samline.pos
reflength = samline.CalcReferenceLengthFromCigar()
sl_endpos = sl_startpos + reflength
# Comparing a samline to the corresponding expected partial alignment
if k < len(expected_partial_alignments):
expected_alignement = expected_partial_alignments[k]
maf_startpos = expected_alignement[0]
maf_endpos = expected_alignement[1]
if abs(sl_startpos - maf_startpos) > allowed_inacc or abs(sl_endpos - maf_endpos) > allowed_inacc:
good_alignment = False
else:
good_alignment = False
k += 1
# Comparing a samline to all expected partial alignments
for i in xrange(len(expected_partial_alignments)):
expected_alignement = expected_partial_alignments[i]
maf_startpos = expected_alignement[0]
maf_endpos = expected_alignement[1]
if interval_equals((sl_startpos, sl_endpos), (maf_startpos, maf_endpos), allowed_inacc, min_overlap):
parteqmap[i+1] += 1
if interval_overlaps((sl_startpos, sl_endpos), (maf_startpos, maf_endpos), allowed_inacc, min_overlap):
parthitmap[i+1] += 1
has_miss_alignments = False
for expected_alignement in expected_partial_alignments:
maf_startpos = expected_alignement[0]
maf_endpos = expected_alignement[1]
overlap = False
for samline in samline_list:
sl_startpos = samline.pos
reflength = samline.CalcReferenceLengthFromCigar()
sl_endpos = sl_startpos + reflength
if interval_overlaps((sl_startpos, sl_endpos), (maf_startpos, maf_endpos), allowed_inacc, min_overlap):
overlap = True
if not overlap:
has_miss_alignments = True
break
if len(samline_list) < len(expected_partial_alignments):
s_maf_too_many_alignments += 1
# Testing the evaluation process
# import pdb
# pdb.set_trace()
if len(samline_list) <> len(expected_partial_alignments):
good_alignment = False
if good_alignment:
s_maf_good_alignments += 1
# Writting qnames to files
if split_qnames:
file_correct.write(samline_list[0].qname + '\n')
if isSplitRead:
s_maf_good_split_alignments += 1
else:
# import pdb
# pdb.set_trace()
s_maf_bad_alignments += 1
if isSplitRead:
s_maf_bad_split_alignments += 1
# TODO: check which alignments are bad and why
# If the choromosome is different its obviously a bad alignment
if RNAseqEval.getChromName(samline.rname) == RNAseqEval.getChromName(annotation.seqname):
# import pdb
# pdb.set_trace()
pass
else:
s_num_badchrom_alignments += 1
# Analyzing parthitmap and parteqmap
oneHit = False
allHits = True
oneEq = False
multiHit = False
for i in xrange(numparts):
if parthitmap[i+1] > 0:
oneHit = True
if parthitmap[i+1] == 0:
allHits = False
if parthitmap[i+1] > 1:
multiHit = True
if parteqmap[i+1] > 0:
oneEq = True
if printMap:
status = 'INCORRECT'
if good_alignment:
status = 'CORRECT'
elif allHits:
status = 'HITALL'
elif oneHit:
status = 'HITONE'
mapfile.write('QNAME: %s, STATUS: %s\n\n' % (samline_list[0].qname, status))
mapfile.write('EXPECTED (%s, %s):\t' % (RNAseqEval.getChromName(annotation.seqname), annotation.strand))
for epa in expected_partial_alignments:
mapfile.write('(%d, %d)\t' % (epa[0], epa[1]))
mapfile.write('\n')
if samline_list[0].flag & 16 == 0:
readstrand = Annotation_formats.GFF_STRANDFW
else:
readstrand = Annotation_formats.GFF_STRANDRV
mapfile.write('ACTUAL (%s, %s):\t' % (RNAseqEval.getChromName(samline_list[0].rname), readstrand))
for samline in samline_list:
mapfile.write('(%d, %d)\t' % (samline.pos, samline.pos + samline.CalcReferenceLengthFromCigar()))
mapfile.write('\n\n')
if oneHit:
s_maf_hit_one_part += 1
if isSplitRead:
s_maf_split_hit_one_part += 1
# Writting qnames to files
if split_qnames:
file_hitone.write(samline_list[0].qname + '\n')
if not allHits:
if '--debug' in paramdict:
import pdb
pdb.set_trace()
# Misses are calculated only for alignments that have at least one hit
if has_miss_alignments:
s_maf_miss_alignment += 1
else:
# Writting qnames to files
if split_qnames:
file_bad.write(samline_list[0].qname + '\n')
# if '--debug' in paramdict:
# import pdb
# pdb.set_trace()
if allHits:
s_maf_hit_all_parts += 1
if isSplitRead:
s_maf_split_hit_all_parts += 1
# Writting qnames to files
if split_qnames:
file_hitall.write(samline_list[0].qname + '\n')
# Sanity check
if '--debug' in paramdict and good_alignment and not allHits:
import pdb
pdb.set_trace()
pass
if oneEq:
s_maf_eq_one_part += 1
if isSplitRead:
s_maf_split_eq_one_part += 1
if multiHit:
s_maf_multihit_parts += 1
num_start_hits = 0
num_end_hits = 0
num_hits = 0
num_partial_alignements = len(samline_list)
whole_alignment_hit = False
for samline in samline_list:
startpos = samline.pos - 1
reflength = samline.CalcReferenceLengthFromCigar()
endpos = startpos + reflength
if samline.flag & 16 == 0:
readstrand = Annotation_formats.GFF_STRANDFW
s_num_fw_strand += 1
else:
readstrand = Annotation_formats.GFF_STRANDRV
s_num_rv_strand += 1
chromname = RNAseqEval.getChromName(samline.rname)
if chromname == RNAseqEval.getChromName(annotation.seqname) and readstrand != annotation.strand and annotation.overlapsGene(startpos, endpos):
s_num_potential_bad_strand += 1
if chromname == RNAseqEval.getChromName(annotation.seqname) and annotation.overlapsGene(startpos, endpos) and (not P_CHECK_STRAND or readstrand == annotation.strand):
whole_alignment_hit = True
s_partial_alignment_hits += 1
else:
s_partial_alignment_misses +=1
# Checking how well partial alignments match exons
startsItem = False
endsItem = False
for item in annotation.items:
if item.overlapsItem(startpos, endpos):
num_hits += 1
if item.startsItem(startpos, endpos):
num_start_hits += 1
startsItem = True
if item.endsItem(startpos, endpos):
num_end_hits += 1
endsItem = True
if startsItem and endsItem:
s_num_start_end_hits += 1
s_num_start_hits += num_start_hits
s_num_end_hits += num_end_hits
# I'm allowing one start and one end not to match starts and ends of exons
if (num_hits == num_partial_alignements) and (num_start_hits + num_end_hits >= 2*num_partial_alignements - 2) :
s_num_good_alignments += 1
# else:
# if num_hits > 0:
# import pdb
# pdb.set_trace()
if whole_alignment_hit:
s_whole_alignment_hits += 1
else:
s_whole_alignment_misses += 1
if printMap:
mapfile.close()
# Writting unmapped query names to a file, if so specified
if split_qnames:
with open(filename_unmapped, 'w+') as file_unmapped:
file_unmapped.write(report.get_unmapped_names())
file_unmapped.close()
# Printing out results : NEW
# Variables names matching RNA benchmark paper
sys.stdout.write('\n\nAnalysis results:')
sys.stdout.write('\nOriginal Samlines: %d' % report.num_alignments)
sys.stdout.write('\nUsable whole alignments (with valid CIGAR string): %d' % len(all_sam_lines))
sys.stdout.write('\nAnnotations: %d' % len(annotation_dict))
sys.stdout.write('\nMultiexon genes: %d' % s_num_multiexon_genes)
sys.stdout.write('\nNumber of exon start hits: %d' % s_num_start_hits)
sys.stdout.write('\nNumber of exon end hits: %d' % s_num_end_hits)
sys.stdout.write('\nNumber of exon start and end hits: %d' % s_num_start_end_hits)
sys.stdout.write('\nNumber of good whole alignments: %d' % s_num_good_alignments)
sys.stdout.write('\nNumber of alignments mapped to an incorrect chromosome: %d' % s_num_badchrom_alignments)
sys.stdout.write('\nMAF: Correct alignment: %d' % s_maf_good_alignments)
sys.stdout.write('\nMAF: Hit all parts: %d' % s_maf_hit_all_parts)
sys.stdout.write('\nMAF: Hit at least one part: %d' % s_maf_hit_one_part)
sys.stdout.write('\nMAF: Equals at least one part: %d' % s_maf_eq_one_part)
sys.stdout.write('\nMAF: Number of split reads: %d' % s_maf_split_reads)
sys.stdout.write('\nMAF: Correct alignment, SPLIT read: %d' % s_maf_good_split_alignments)
sys.stdout.write('\nMAF: Hit all parts, SPLIT read: %d' % s_maf_split_hit_all_parts)
sys.stdout.write('\nMAF: Hit at least one part, SPLIT read: %d' % s_maf_split_hit_one_part)
sys.stdout.write('\nMAF: Equals at least one part, SPLIT read: %d' % s_maf_split_eq_one_part)
sys.stdout.write('\nMAF: Partial alignment that misses: %d' % s_maf_miss_alignment)
sys.stdout.write('\nMAF: More alignments than expected: %d' % s_maf_too_many_alignments)
sys.stdout.write('\nMAF: Multihit parts (fragmented) alignments: %d' % s_maf_multihit_parts)
sys.stdout.write('\nDone!\n')
# Closing file with names
if split_qnames:
file_correct.close()
file_hitall.close()
file_hitone.close()
file_bad.close()
# # Printing out results
# sys.stdout.write('\n\nAnalysis results:')
# sys.stdout.write('\nOriginal Samlines: %d' % report.num_alignments)
# sys.stdout.write('\nUsable whole alignments: %d' % len(all_sam_lines))
# sys.stdout.write('\nSplit alignments: %d' % s_num_split_alignment)
# sys.stdout.write('\nAnnotations: %d' % len(annotation_dict))
# sys.stdout.write('\nMultiexon genes: %d' % s_num_multiexon_genes)
# sys.stdout.write('\nPartial alignment hits: %d' % s_partial_alignment_hits)
# sys.stdout.write('\nPartial alignment misses: %d' % s_partial_alignment_misses)
# sys.stdout.write('\nWhole alignment hits: %d' % s_whole_alignment_hits)
# sys.stdout.write('\nWhole alignment misses: %d' % s_whole_alignment_misses)
# sys.stdout.write('\nNumber of oversplit alignments: %d' % s_num_oversplit_alignment)
# sys.stdout.write('\nNumber of exon start hits: %d' % s_num_start_hits)
# sys.stdout.write('\nNumber of exon end hits: %d' % s_num_end_hits)
# sys.stdout.write('\nNumber of exon start and end hits: %d' % s_num_start_end_hits)
# sys.stdout.write('\nNumber of good whole alignments: %d' % s_num_good_alignments)
# sys.stdout.write('\nNumber of alignments mapped to an incorrect chromosome: %d' % s_num_badchrom_alignments)
# sys.stdout.write('\nPartial alignments on strand (FW / RV): (%d / %d)' % (s_num_fw_strand, s_num_rv_strand))
# sys.stdout.write('\nPotential bad strand alignments: %d' % s_num_potential_bad_strand)
# sys.stdout.write('\nMAF: Suspicious alignments: %d' % s_maf_suspicious_alignments)
# sys.stdout.write('\nMAF: Hit both ends: %d' % s_maf_good_alignments)
# sys.stdout.write('\nMAF: Didn\'t hit both ends: %d' % s_maf_bad_alignments)
# sys.stdout.write('\nMAF: Hit all parts: %d' % s_maf_hit_all_parts)
# sys.stdout.write('\nMAF: Hit at least one part: %d' % s_maf_hit_one_part)
# sys.stdout.write('\nMAF: Equals at least one part: %d' % s_maf_eq_one_part)
# sys.stdout.write('\nMAF: Multihit parts (fragmented) alignments: %d' % s_maf_multihit_parts)
# sys.stdout.write('\nMAF: Number of split reads: %d' % s_maf_split_reads)
# sys.stdout.write('\nMAF: Hit both ends, SPLIT alignments: %d' % s_maf_good_split_alignments)
# sys.stdout.write('\nMAF: Didn\'t hit both ends, SPLIT alignments: %d' % s_maf_bad_split_alignments)
# sys.stdout.write('\nMAF: Hit all parts on split read: %d' % s_maf_split_hit_all_parts)
# sys.stdout.write('\nMAF: Hit at least one part on split read: %d' % s_maf_split_hit_one_part)
# sys.stdout.write('\nMAF: Equals at least one part on split read: %d' % s_maf_split_eq_one_part)
# sys.stdout.write('\nDone!\n')
def verbose_usage_and_exit():
sys.stderr.write('Process pbsim data - A tool for processing data generated by pbsim.\n')
sys.stderr.write(' - Collects data generated for multiple references.\n')
sys.stderr.write(' - And adjusts headers to reflect a reference of origin.\n')
sys.stderr.write('\n')
sys.stderr.write('Usage:\n')
sys.stderr.write('\t%s [mode]\n' % sys.argv[0])
sys.stderr.write('\n')
sys.stderr.write('\tmode:\n')
sys.stderr.write('\t\tprocess\n')
sys.stderr.write('\n')
exit(0)
if __name__ == '__main__':
if (len(sys.argv) < 2):
verbose_usage_and_exit()
mode = sys.argv[1]
if (mode == 'process'):
if (len(sys.argv) < 5):
sys.stderr.write('Processes a folder containing data generated by pbsim.\n')
sys.stderr.write('Joins all generated reads into a single FASTQ file.\n')
sys.stderr.write('Expands existing headers with the name of originating reference.\n')
sys.stderr.write('Usage:\n')
sys.stderr.write('%s %s <pbsim data folder> <results file> <annotations file> <options>\n'% (sys.argv[0], sys.argv[1]))
sys.stderr.write('\n')
sys.stderr.write('\noptions:\n')
sys.stderr.write('\t\t--split-qnames: while calculating the statistics also sorts query names\n')
sys.stderr.write('\t\t into four files - file_correct.names, file_hitall.names\n')
sys.stderr.write('\t\t file_hitone.names, file_bad.names\n')
sys.stderr.write('\t\t--print_mapping [filename]: Print information about actual and expected alignments\n')
sys.stderr.write('\t\t into a give text file.\n')
sys.stderr.write('\n')
exit(1)
datafolder = sys.argv[2]
resultfile = sys.argv[3]
annotationfile = sys.argv[4]
pparser = paramsparser.Parser(paramdefs)
paramdict = pparser.parseCmdArgs(sys.argv[5:])
paramdict['command'] = ' '.join(sys.argv)
processData(datafolder, resultfile, annotationfile, paramdict)
else:
print 'Invalid mode!'