File: Parser.pyx

package info (click to toggle)
macs 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 378,732 kB
  • sloc: ansic: 5,879; python: 4,342; sh: 451; makefile: 86
file content (1481 lines) | stat: -rw-r--r-- 52,432 bytes parent folder | download | duplicates (2)
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
# cython: language_level=3
# cython: profile=True
# cython: linetrace=True
# Time-stamp: <2023-08-02 14:49:59 Tao Liu>

"""Module for all MACS Parser classes for input. Please note that the
parsers are for reading the alignment files ONLY.

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD License (see the file LICENSE included with
the distribution).

"""

# ------------------------------------
# python modules
# ------------------------------------
import struct
from struct import unpack
from re import findall
import gzip
import io
import sys

import logging
import MACS3.Utilities.Logger

logger = logging.getLogger(__name__)
debug   = logger.debug
info    = logger.info
# ------------------------------------
# MACS3 modules
# ------------------------------------

from MACS3.Utilities.Constants import *
from MACS3.Signal.FixWidthTrack import FWTrack
from MACS3.Signal.PairedEndTrack import PETrackI

# ------------------------------------
# Other modules
# ------------------------------------
from cpython cimport bool

import numpy as np
cimport numpy as np
from numpy cimport uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float32_t, float64_t

is_le = sys.byteorder == "little"

cdef extern from "stdlib.h":
    ctypedef uint32_t size_t
    size_t strlen(char *s)
    void *malloc(size_t size)
    void *calloc(size_t n, size_t size)
    void free(void *ptr)
    int32_t strcmp(char *a, char *b)
    char * strcpy(char *a, char *b)
    int64_t atol(char *str)
    int32_t atoi(char *str)

# ------------------------------------
# constants
# ------------------------------------
__version__ = "Parser $Revision$"
__author__ = "Tao Liu <taoliu@jimmy.harvard.edu>"
__doc__ = "All Parser classes"

# ------------------------------------
# Misc functions
# ------------------------------------

cpdef guess_parser ( fname, int64_t buffer_size = 100000 ):
    # Note: BAMPE and BEDPE can't be automatically detected.
    ordered_parser_dict = {"BAM":BAMParser,
                           "BED":BEDParser,
                           "ELAND":ELANDResultParser,
                           "ELANDMULTI":ELANDMultiParser,
                           "ELANDEXPORT":ELANDExportParser,
                           "SAM":SAMParser,
                           "BOWTIE":BowtieParser}

    for f in ordered_parser_dict:
        p = ordered_parser_dict[ f ]
        t_parser = p( fname, buffer_size = buffer_size )
        debug( "Testing format %s" % f )
        s = t_parser.sniff()
        if s:
            info( "Detected format is: %s" % ( f ) )
            if t_parser.is_gzipped():
                info( "* Input file is gzipped." )
            return t_parser
        else:
            t_parser.close()
    raise Exception( "Can't detect format!" )

cdef tuple bam_fw_binary_parse_le ( const unsigned char * data ):
    """Parse a BAM SE entry in little endian system
    """
    cdef:
        int32_t thisref, thisstart, thisstrand
        uint16_t bwflag
        uint8_t l_read_name
        uint16_t n_cigar_op
        int32_t cigar_code
        uint8_t *ui8
        int32_t *i32
        uint16_t *ui16
        uint32_t *ui32

    # we skip lot of the available information in data (i.e. tag name, quality etc etc)
    # no data, return, does it really happen without raising struct.error?
    if not data: return ( -1, -1, -1 )

    ui16 = <uint16_t *>data
    bwflag = ui16[7]

    # we filter out unmapped sequence or bad sequence or  secondary or supplementary alignment
    # we filter out 2nd mate, not a proper pair, mate is unmapped
    if (bwflag & 2820) or (bwflag & 1 and (bwflag & 136 or not bwflag & 2)): return ( -1, -1, -1 )

    i32 = <int32_t *>data
    thisref = i32[0]
    thisstart = i32[1]
    n_cigar_op = ui16[6]
    # In case of paired-end we have now skipped all possible "bad" pairs
    # in case of proper pair we have skipped the rightmost one... if the leftmost pair comes
    # we can treat it as a single read, so just check the strand and calculate its
    # start position... hope I'm right!
    if bwflag & 16:
        # read mapped to minus strand; then we have to compute cigar to find the rightmost position
        ui8 = <uint8_t *>data
        l_read_name = ui8[8]
        # need to decipher CIGAR string
        ui32 = <uint32_t *>(data + 32 + l_read_name) # move pointer to cigar_code
        for cigar_code in ui32[:n_cigar_op]:#unpack( '<%dI' % (n_cigar_op) , data[ 32 + l_read_name : 32 + l_read_name + n_cigar_op*4 ] ):
            if cigar_code & 15 in [ 0, 2, 3, 7, 8 ]:   # they are CIGAR op M/D/N/=/X
                thisstart += cigar_code >> 4
        thisstrand = 1
    else:
        thisstrand = 0

    return ( thisref, thisstart, thisstrand )

cdef tuple bam_fw_binary_parse_be ( const unsigned char * data ):
    """Big endian version. We need byte swap.
    """
    cdef:
        int32_t thisref, thisstart, thisstrand
        uint16_t bwflag
        uint8_t l_read_name
        uint16_t n_cigar_op
        int32_t cigar_code
        uint8_t *ui8                      # we will only cast 1 byte at a time
        int32_t i
        uint32_t shift0, shift


    # we skip lot of the available information in data (i.e. tag name, quality etc etc)
    # no data, return, does it really happen without raising struct.error?
    if not data: return ( -1, -1, -1 )

    ui8 = <uint8_t *>data
    bwflag = ui8[15] << 8 | ui8[14]       # it works as bwflag = ui16[7] in little-endian

    # we filter out unmapped sequence or bad sequence or  secondary or supplementary alignment
    # we filter out 2nd mate, not a proper pair, mate is unmapped
    if (bwflag & 2820) or (bwflag & 1 and (bwflag & 136 or not bwflag & 2)): return ( -1, -1, -1 )

    # the following three lins are for little-endian
    #thisref = i32[0]
    #thisstart = i32[1]
    #n_cigar_op = ui16[6]

    # to simplify the byte swap, we pretend all original numbers (thisref, pos, nextpos) positive
    thisref = ui8[3] << 24 | ui8[2] << 16 | ui8[1] << 8 | ui8[0]
    thisstart = ui8[7] << 24 | ui8[6] << 16 | ui8[5] << 8 | ui8[4]
    n_cigar_op = ui8[13] << 8 | i8[12]

    # In case of paired-end we have now skipped all possible "bad" pairs
    # in case of proper pair we have skipped the rightmost one... if the leftmost pair comes
    # we can treat it as a single read, so just check the strand and calculate its
    # start position... hope I'm right!
    if bwflag & 16:
        # read mapped to minus strand; then we have to compute cigar to find the rightmost position
        l_read_name = ui8[8]
        # need to decipher CIGAR string
        # move pointer to cigar_code
        shift0 = 32 + l_read_name
        for i in range(n_cigar_op):
            shift = shift0 + i*4          # move 32bit at a time
            cigar_code = ui8[shift0+3] << 24 | ui8[shift0+2] << 16 | ui8[shift0+1] << 8 | ui8[shift0] # it works like cigar_code = ui32[...] in little-endian
            if cigar_code & 15 in [ 0, 2, 3, 7, 8 ]:   # they are CIGAR op M/D/N/=/X
                thisstart += cigar_code >> 4
        thisstrand = 1
    else:
        thisstrand = 0

    return ( thisref, thisstart, thisstrand )

cdef tuple bampe_pe_binary_parse_le (const unsigned char * data):
    """Parse a BAMPE record in little-endian system.
    """
    cdef:
        int32_t thisref, thisstart, thistlen
        int32_t nextpos, pos
        uint16_t bwflag
        uint8_t *ui8
        int32_t *i32
        uint16_t *ui16

    # we skip lot of the available information in data (i.e. tag name, quality etc etc)
    if not data:
        return ( -1, -1, -1 )

    ui16 = <uint16_t *>data
    bwflag = ui16[7]
    # we filter out unmapped, bad sequence, secondary/supplementary alignment
    # we filter out other mate of paired reads, not a proper pair, or mate is unmapped
    if (bwflag & 2820) or (bwflag & 1 and (bwflag & 136 or not bwflag & 2)):
        return ( -1, -1, -1 )

    i32 = <int32_t *>data
    ui8 = <uint8_t *>data
    thisref = i32[0]

    pos = i32[1]
    nextpos = i32[6]
    thistlen = i32[7]
    thisstart = min(pos, nextpos) # we keep only the leftmost
                                  # position which means this must
                                  # be at + strand. So we don't
                                  # need to decipher CIGAR string.
    thistlen = abs( thistlen )                    # Actually, if
    #                                             # the value
    #                                             # unpacked is
    #                                             # negative, then
    #                                             # nextpos is the
    #                                             # leftmost
    #                                             # position.

    return ( thisref, thisstart, thistlen )

cdef tuple bampe_pe_binary_parse_be (const unsigned char * data):
    """Parse a BAMPE record in big-endian system. And we need byte swap.
    """
    cdef:
        int32_t thisref, thisstart, thistlen
        uint32_t tmp_thistlen
        int32_t nextpos, pos
        uint16_t bwflag
        uint8_t *ui8                      # we will only cast 1 byte at a time

    # we skip lot of the available information in data (i.e. tag name, quality etc etc)
    if not data:
        return ( -1, -1, -1 )

    ui8 = <uint8_t *>data

    bwflag = ui8[15] << 8 | ui8[14]       # as le: bwflag = ui16[7]
    # we filter out unmapped, bad sequence, secondary/supplementary alignment
    # we filter out other mate of paired reads, not a proper pair, or mate is unmapped
    if (bwflag & 2820) or (bwflag & 1 and (bwflag & 136 or not bwflag & 2)):
        return ( -1, -1, -1 )

    i8 = <int8_t *>data

    # the following three lins are for little-endian



    # to simplify the byte swap, we pretend all original numbers (thisref, pos, nextpos) positive
    thisref = ui8[3] << 24 | ui8[2] << 16 | ui8[1] << 8 | ui8[0]   # as le:thisref = i32[0]
    pos = ui8[7] << 24 | ui8[6] << 16 | ui8[5] << 8 | ui8[4]       # as le:pos = i32[1]
    nextpos = ui8[27] << 24 | ui8[26] << 16 | ui8[25] << 8 | ui8[24] # as le:nextpos = i32[6]

    # thistlen can be negative, so we byte swap it then convert to int32_t then take abs (maybe there is more effecient way?)
    tmp_thistlen = ui8[31] << 24 | ui8[30] << 16 | ui8[29] << 8 | ui8[28] # as le:tmp_thistlen = ui32[7]
    thistlen = abs(<int32_t> tmp_thistlen)

    # position which means this must
    # be at + strand. So we don't
    # need to decipher CIGAR string.
    thisstart = pos if nextpos > pos else nextpos #min(pos, nextpos) # we keep only the leftmost

    return ( thisref, thisstart, thistlen )


# choose a parser according to endian
if is_le:
    bam_se_entry_parser = bam_fw_binary_parse_le
    bampe_pe_entry_parser = bampe_pe_binary_parse_le
else:
    bam_se_entry_parser = bam_fw_binary_parse_be
    bampe_pe_entry_parser = bampe_pe_binary_parse_be

# ------------------------------------
# Classes
# ------------------------------------
class StrandFormatError( BaseException ):
    """Exception about strand format error.

    Example:
    raise StrandFormatError('Must be F or R','X')
    """
    def __init__ ( self, string, strand ):
        self.strand = strand
        self.string = string

    def __str__ ( self ):
        return repr( "Strand information can not be recognized in this line: \"%s\",\"%s\"" % ( self.string, self.strand ) )

cdef class GenericParser:
    """Generic Parser class.

    Inherit this class to write your own parser. In most cases, you need to override:

    1. tlen_parse_line which returns tag length of a line
    2.  fw_parse_line which returns tuple of ( chromosome, 5'position, strand )
    """
    cdef:
        str filename
        bool gzipped
        int32_t tag_size
        object fhd
        int64_t buffer_size

    def __init__ ( self, str filename, int64_t buffer_size = 100000 ):
        """Open input file. Determine whether it's a gzipped file.

        'filename' must be a string object.

        This function initialize the following attributes:

        1. self.filename: the filename for input file.
        2. self.gzipped: a boolean indicating whether input file is gzipped.
        3. self.fhd: buffered I/O stream of input file
        """
        self.filename = filename
        self.gzipped = True
        self.tag_size = -1
        self.buffer_size = buffer_size
        # try gzip first
        f = gzip.open( filename )
        try:
            f.read( 10 )
        except IOError:
            # not a gzipped file
            self.gzipped = False
        f.close()
        if self.gzipped:
            # open with gzip.open, then wrap it with BufferedReader!
            self.fhd = io.BufferedReader( gzip.open( filename, mode='rb' ), buffer_size = READ_BUFFER_SIZE ) # buffersize set to 10M
        else:
            self.fhd = io.open( filename, mode='rb' ) # binary mode! I don't expect unicode here!
        self.skip_first_commentlines()

    cdef void skip_first_commentlines ( self ):
        """Some parser needs to skip the first several comment lines.

        Redefine this if it's necessary!
        """
        return

    cpdef int32_t tsize( self ):
        """General function to detect tag size.

        * Although it can be used by most parsers, it must be
          rewritten by BAMParser!
        """
        cdef:
            int32_t s = 0
            int32_t n = 0     # number of successful/valid read alignments
            int32_t m = 0     # number of trials
            int32_t this_taglength
            bytes thisline

        if self.tag_size != -1:
            # if we have already calculated tag size (!= -1),  return it.
            return self.tag_size

        # try 10k times or retrieve 10 successfule alignments
        while n < 10 and m < 10000:
            m += 1
            thisline = self.fhd.readline()
            this_taglength = self.tlen_parse_line( thisline )
            if this_taglength > 0:
                # this_taglength == 0 means this line doesn't contain
                # successful alignment.
                s += this_taglength
                n += 1
        # done
        self.fhd.seek( 0 )
        self.skip_first_commentlines()
        if n != 0:              # else tsize = -1
            self.tag_size = <int32_t>(s/n)
        return self.tag_size

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Abstract function to detect tag length.

        """
        raise NotImplementedError

    cpdef build_fwtrack ( self ):
        """Generic function to build FWTrack object. Create a new
        FWTrack object. If you want to append new records to an
        existing FWTrack object, try append_fwtrack function.

        * BAMParser for binary BAM format should have a different one.
        """
        cdef:
            int64_t i, fpos, strand
            bytes chromosome
            bytes tmp = b""

        fwtrack = FWTrack( buffer_size = self.buffer_size )
        i = 0
        while True:
            # for each block of input
            tmp += self.fhd.read( READ_BUFFER_SIZE )
            if not tmp:
                break
            lines = tmp.split(b"\n")
            tmp = lines[ -1 ]
            for thisline in lines[ :-1 ]:
                ( chromosome, fpos, strand ) = self.fw_parse_line( thisline )
                if fpos < 0 or not chromosome:
                    # normally fw_parse_line will return -1 if the line
                    # contains no successful alignment.
                    continue
                i += 1
                if i % 1000000 == 0:
                    info( " %d reads parsed" % i )
                fwtrack.add_loc( chromosome, fpos, strand )
        # last one
        if tmp:
            ( chromosome, fpos, strand ) = self.fw_parse_line( tmp )
            if fpos >= 0 and chromosome:
                i += 1
                fwtrack.add_loc( chromosome, fpos, strand )
        # close file stream.
        self.close()
        return fwtrack

    cpdef append_fwtrack ( self, fwtrack ):
        """Add more records to an existing FWTrack object.

        """
        cdef:
            int64_t i, fpos, strand
            bytes chromosome
            bytes tmp = b""
        i = 0
        while True:
            # for each block of input
            tmp += self.fhd.read( READ_BUFFER_SIZE )
            if not tmp:
                break
            lines = tmp.split(b"\n")
            tmp = lines[ -1 ]
            for thisline in lines[ :-1 ]:
                ( chromosome, fpos, strand ) = self.fw_parse_line( thisline )
                if fpos < 0 or not chromosome:
                    # normally fw_parse_line will return -1 if the line
                    # contains no successful alignment.
                    continue
                i += 1
                if i % 1000000 == 0:
                    info( " %d reads parsed" % i )
                fwtrack.add_loc( chromosome, fpos, strand )

        # last one
        if tmp:
            ( chromosome, fpos, strand ) = self.fw_parse_line( tmp )
            if fpos >= 0 and chromosome:
                i += 1
                fwtrack.add_loc( chromosome, fpos, strand )
        # close file stream.
        self.close()
        return fwtrack

    cdef tuple fw_parse_line ( self, bytes thisline ):
        """Abstract function to parse chromosome, 5' end position and
        strand.

        """
        cdef bytes chromosome = b""
        cdef int32_t fpos = -1
        cdef int32_t strand = -1
        return ( chromosome, fpos, strand )

    cpdef sniff ( self ):
        """Detect whether this parser is the correct parser for input
        file.

        Rule: try to find the tag size using this parser, if error
        occurs or tag size is too small or too big, check is failed.

        * BAMParser has a different sniff function.
        """
        cdef int32_t t

        t = self.tsize()
        if t <= 10 or t >= 10000: # tsize too small or too big
            self.fhd.seek( 0 )
            return False
        else:
            self.fhd.seek( 0 )
            self.skip_first_commentlines()
            return True

    cpdef close ( self ):
        """Run this when this Parser will be never used.

        Close file I/O stream.
        """
        self.fhd.close()

    cpdef bool is_gzipped ( self ):
        return self.gzipped

cdef class BEDParser( GenericParser ):
    """File Parser Class for BED File.

    """
    cdef void skip_first_commentlines ( self ):
        """BEDParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and ( thisline[ :5 ] != b"track" ) \
               and ( thisline[ :7 ] != b"browser" ) \
               and ( thisline[ 0 ] != 35 ): # 35 is b"#"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse 5' and 3' position, then calculate frag length.

        """
        thisline = thisline.rstrip()
        if not thisline:
            return 0

        thisfields = thisline.split( b'\t' )
        return atoi( thisfields[ 2 ] )-atoi( thisfields[ 1 ] )

    cdef tuple fw_parse_line ( self, bytes thisline ):
        #cdef list thisfields
        cdef:
            bytes chromname
            list thisfields

        thisline = thisline.rstrip()
        thisfields = thisline.split( b'\t' )
        chromname = thisfields[ 0 ]
        try:
            #if not strcmp(thisfields[ 5 ],b"+"):
            if thisfields[5] == b"+":
                return ( chromname,
                         atoi( thisfields[ 1 ] ),
                         0 )
            #elif not strcmp(thisfields[ 5 ], b"-"):
            elif thisfields[5] == b"-":
                return ( chromname,
                         atoi( thisfields[ 2 ] ),
                         1 )
            else:
                raise StrandFormatError( thisline, thisfields[ 5 ] )
        except IndexError:
            # default pos strand if no strand
            # info can be found
            return ( chromname,
                     atoi( thisfields[ 1 ] ),
                     0 )

cdef class BEDPEParser(GenericParser):
    """Parser for BED format file containing PE information, and also
    can be used for the cases when users predefine the fragment
    locations by shifting/extending by themselves.

    Format:

    chromosome_name	frag_leftend	frag_rightend


    Note: Only the first columns are used!
    """

    cdef public int32_t n
    cdef public float32_t d

    cdef void skip_first_commentlines ( self ):
        """BEDPEParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and ( thisline[ :5 ] != b"track" ) \
               and ( thisline[ :7 ] != b"browser" ) \
               and ( thisline[ 0 ] != 35 ): # 35 is b"#"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef pe_parse_line ( self, bytes thisline ):
        """ Parse each line, and return chromosome, left and right positions

        """
        cdef:
            list thisfields

        thisline = thisline.rstrip()

        # still only support tabular as delimiter.
        thisfields = thisline.split( b'\t' )
        try:
            return ( thisfields[ 0 ],
                     atoi( thisfields[ 1 ] ),
                     atoi( thisfields[ 2 ] ) )
        except IndexError:
            raise Exception("Less than 3 columns found at this line: %s\n" % thisline)

    cpdef build_petrack ( self ):
        """Build PETrackI from all lines.

        """
        cdef:
            bytes chromname
            int32_t left_pos
            int32_t right_pos
            int64_t i = 0          # number of fragments
            int64_t m = 0          # sum of fragment lengths
            bytes tmp = b""

        petrack = PETrackI( buffer_size = self.buffer_size )
        add_loc = petrack.add_loc

        while True:
            # for each block of input
            tmp += self.fhd.read( READ_BUFFER_SIZE )
            if not tmp:
                break
            lines = tmp.split(b"\n")
            tmp = lines[ -1 ]
            for thisline in lines[ :-1 ]:
                ( chromosome, left_pos, right_pos ) = self.pe_parse_line( thisline )
                if left_pos < 0 or not chromosome:
                    continue
                assert right_pos > left_pos, "Right position must be larger than left position, check your BED file at line: %s" % thisline
                m += right_pos - left_pos
                i += 1
                if i % 1000000 == 0:
                    info( " %d fragments parsed" % i )
                add_loc( chromosome, left_pos, right_pos )
        # last one
        if tmp:
            ( chromosome, left_pos, right_pos ) = self.pe_parse_line( thisline )
            if left_pos >= 0 and chromosome:
                assert right_pos > left_pos, "Right position must be larger than left position, check your BED file at line: %s" % thisline
                i += 1
                m += right_pos - left_pos
                add_loc( chromosome, left_pos, right_pos )
                
        self.d = <float32_t>( m ) / i
        self.n = i
        assert self.d >= 0, "Something went wrong (mean fragment size was negative)"

        self.close()
        petrack.set_rlengths( {"DUMMYCHROM":0} )
        return petrack

    cpdef append_petrack (self, petrack):
        """Build PETrackI from all lines, return a PETrackI object.
        """
        cdef:
            bytes chromname
            int32_t left_pos
            int32_t right_pos
            int64_t i = 0          # number of fragments
            int64_t m = 0          # sum of fragment lengths
            bytes tmp = b""

        add_loc = petrack.add_loc
        while True:
            # for each block of input
            tmp += self.fhd.read( READ_BUFFER_SIZE )
            if not tmp:
                break
            lines = tmp.split(b"\n")
            tmp = lines[ -1 ]
            for thisline in lines[ :-1 ]:
                ( chromosome, left_pos, right_pos ) = self.pe_parse_line( thisline )
                if left_pos < 0 or not chromosome:
                    continue
                assert right_pos > left_pos, "Right position must be larger than left position, check your BED file at line: %s" % thisline
                m += right_pos - left_pos
                i += 1
                if i % 1000000 == 0:
                    info( " %d fragments parsed" % i )
                add_loc( chromosome, left_pos, right_pos )
        # last one
        if tmp:
            ( chromosome, left_pos, right_pos ) = self.pe_parse_line( thisline )
            if left_pos >= 0 and chromosome:
                assert right_pos > left_pos, "Right position must be larger than left position, check your BED file at line: %s" % thisline
                i += 1
                m += right_pos - left_pos
                add_loc( chromosome, left_pos, right_pos )

        self.d = ( self.d * self.n + m ) / ( self.n + i )
        self.n += i

        assert self.d >= 0, "Something went wrong (mean fragment size was negative)"
        self.close()
        petrack.set_rlengths( {"DUMMYCHROM":0} )
        return petrack

cdef class ELANDResultParser( GenericParser ):
    """File Parser Class for tabular File.

    """
    cdef void skip_first_commentlines ( self ):
        """ELANDResultParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and thisline[ 0 ] != 35: # 35 is b"#"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse tag sequence, then tag length.

        """
        thisline = thisline.rstrip()
        if not thisline: return 0
        thisfields = thisline.split( b'\t' )
        if thisfields[1].isdigit():
            return 0
        else:
            return len( thisfields[ 1 ] )

    cdef tuple fw_parse_line ( self, bytes thisline ):
        cdef:
            bytes chromname, strand
            int32_t thistaglength
        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )

        thisfields = thisline.split( b'\t' )
        thistaglength = strlen( thisfields[ 1 ] )

        if len( thisfields ) <= 6:
            return ( b"", -1, -1 )

        try:
            chromname = thisfields[ 6 ]
            chromname = chromname[ :chromname.rindex( b".fa" ) ]
        except ValueError:
            pass

        if thisfields[ 2 ] == b"U0" or thisfields[ 2 ] == b"U1" or thisfields[ 2 ] == b"U2":
            # allow up to 2 mismatches...
            strand = thisfields[ 8 ]
            if strand == b"F":
                return ( chromname,
                         atoi( thisfields[ 7 ] ) - 1,
                         0 )
            elif strand == b"R":
                return ( chromname,
                         atoi( thisfields[ 7 ] ) + thistaglength - 1,
                         1 )
            else:
                raise StrandFormatError( thisline, strand )
        else:
            return ( b"", -1, -1 )

cdef class ELANDMultiParser( GenericParser ):
    """File Parser Class for ELAND multi File.

    Note this parser can only work for s_N_eland_multi.txt format.

    Each line of the output file contains the following fields:
    1. Sequence name
    2. Sequence
    3. Either NM, QC, RM (as described above) or the following:
    4. x:y:z where x, y, and z are the number of exact, single-error, and 2-error matches
    found
    5. Blank, if no matches found or if too many matches found, or the following:
    BAC_plus_vector.fa:163022R1,170128F2,E_coli.fa:3909847R1
    This says there are two matches to BAC_plus_vector.fa: one in the reverse direction
    starting at position 160322 with one error, one in the forward direction starting at
    position 170128 with two errors. There is also a single-error match to E_coli.fa.
    """
    cdef void skip_first_commentlines ( self ):
        """ELANDResultParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and thisline[ 0 ] != 35: # 35 is b"#"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse tag sequence, then tag length.

        """
        thisline = thisline.rstrip()
        if not thisline: return 0
        thisfields = thisline.split( b'\t' )
        if thisfields[1].isdigit():
            return 0
        else:
            return len( thisfields[ 1 ] )

    cdef tuple fw_parse_line ( self, bytes thisline ):
        cdef:
            list thisfields
            bytes thistagname, pos, strand
            int32_t thistaglength, thistaghits

        if not thisline: return ( b"", -1, -1 )
        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )

        thisfields = thisline.split( b'\t' )
        thistagname = thisfields[ 0 ]        # name of tag
        thistaglength = len( thisfields[ 1 ] ) # length of tag

        if len( thisfields ) < 4:
            return ( b"", -1, -1 )
        else:
            thistaghits = sum( [<int32_t>(x) for x in thisfields[ 2 ].split( b':' ) ] )
            if thistaghits > 1:
                # multiple hits
                return ( b"", -1, -1 )
            else:
                ( chromname, pos ) = thisfields[ 3 ].split( b':' )

                try:
                    chromname = chromname[ :chromname.rindex( b".fa" ) ]
                except ValueError:
                    pass

                strand  = pos[ -2 ]
                if strand == b"F":
                    return ( chromname,
                             <int32_t>( pos[ :-2 ] )-1,
                             0 )
                elif strand == b"R":
                    return ( chromname,
                             <int32_t>( pos[ :-2 ] ) + thistaglength - 1,
                             1 )
                else:
                    raise StrandFormatError( thisline,strand )


cdef class ELANDExportParser( GenericParser ):
    """File Parser Class for ELAND Export File.

    """
    cdef void skip_first_commentlines ( self ):
        """ELANDResultParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and thisline[ 0 ] != 35: # 35 is b"#"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse tag sequence, then tag length.

        """
        thisline = thisline.rstrip()
        if not thisline: return 0
        thisfields = thisline.split( b'\t' )
        if len( thisfields ) > 12 and thisfields[ 12 ]:
            # a successful alignment has over 12 columns
            return len( thisfields[ 8 ] )
        else:
            return 0

    cdef tuple fw_parse_line ( self, bytes thisline ):
        cdef:
            list thisfields
            bytes thisname, strand
            int32_t thistaglength

        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )

        thisfields = thisline.split( b"\t" )

        if len(thisfields) > 12 and thisfields[ 12 ]:
            thisname = b":".join( thisfields[ 0:6 ] )
            thistaglength = len( thisfields[ 8 ] )
            strand = thisfields[ 13 ]
            if strand == b"F":
                return ( thisfields[ 10 ], atoi( thisfields[ 12 ] ) - 1, 0 )
            elif strand == b"R":
                return ( thisfields[ 10 ], atoi( thisfields[ 12 ] ) + thistaglength - 1, 1 )
            else:
                raise StrandFormatError( thisline, strand )
        else:
            return ( b"", -1, -1 )

### Contributed by Davide, modified by Tao
cdef class SAMParser( GenericParser ):
    """File Parser Class for SAM File.

    Each line of the output file contains at least:
    1. Sequence name
    2. Bitwise flag
    3. Reference name
    4. 1-based leftmost position fo clipped alignment
    5. Mapping quality
    6. CIGAR string
    7. Mate Reference Name
    8. 1-based leftmost Mate Position
    9. Inferred insert size
    10. Query sequence on the same strand as the reference
    11. Query quality

    The bitwise flag is made like this:
    dec	meaning
    ---	-------
    1	paired read
    2	proper pair
    4	query unmapped
    8	mate unmapped
    16	strand of the query (1 -> reverse)
    32	strand of the mate
    64	first read in pair
    128	second read in pair
    256	alignment is not primary
    512	does not pass quality check
    1024	PCR or optical duplicate
    2048	supplementary alignment
    """

    cdef void skip_first_commentlines ( self ):
        """SAMParser needs to skip the first several comment lines.
        """
        cdef:
            int32_t l_line
            bytes this_line
        for thisline in self.fhd:
            l_line = len( thisline )
            if thisline and thisline[ 0 ] != 64: # 64 is b"@"
                break

        # rewind from SEEK_CUR
        self.fhd.seek( -l_line, 1 )
        return

    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse tag sequence, then tag length.

        """
        cdef:
            list thisfields
            int32_t bwflag

        thisline = thisline.rstrip()
        if not thisline: return 0
        thisfields = thisline.split( b'\t' )
        bwflag = atoi( thisfields[ 1 ] )
        if bwflag & 4 or bwflag & 512 or bwflag & 256 or bwflag & 2048:
            return 0       #unmapped sequence or bad sequence or 2nd or sup alignment
        if bwflag & 1:
            # paired read. We should only keep sequence if the mate is mapped
            # and if this is the left mate, all is within  the flag!
            if not bwflag & 2:
                return 0   # not a proper pair
            if bwflag & 8:
                return 0   # the mate is unmapped
            # From Benjamin Schiller https://github.com/benjschiller
            if bwflag & 128:
                # this is not the first read in a pair
                return 0
        return len( thisfields[ 9 ] )

    cdef tuple fw_parse_line ( self, bytes thisline ):
        cdef:
            list thisfields
            bytes thistagname, thisref
            int32_t bwflag, thisstrand, thisstart

        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )
        thisfields = thisline.split( b'\t' )
        thistagname = thisfields[ 0 ]         # name of tag
        thisref = thisfields[ 2 ]
        bwflag = atoi( thisfields[ 1 ] )
        CIGAR = thisfields[ 5 ]

        if (bwflag & 2820) or (bwflag & 1 and (bwflag & 136 or not bwflag & 2)): return ( b"", -1, -1 )

        #if bwflag & 4 or bwflag & 512 or bwflag & 256 or bwflag & 2048:
        #    return ( b"", -1, -1 )       #unmapped sequence or bad sequence or 2nd or sup alignment
        #if bwflag & 1:
        #    # paired read. We should only keep sequence if the mate is mapped
        #    # and if this is the left mate, all is within  the flag!
        #    if not bwflag & 2:
        #        return ( b"", -1, -1 )   # not a proper pair
        #    if bwflag & 8:
        #        return ( b"", -1, -1 )   # the mate is unmapped
        #    # From Benjamin Schiller https://github.com/benjschiller
        #    if bwflag & 128:
        #        # this is not the first read in a pair
        #        return ( b"", -1, -1 )
        #    # end of the patch
        # In case of paired-end we have now skipped all possible "bad" pairs
        # in case of proper pair we have skipped the rightmost one... if the leftmost pair comes
        # we can treat it as a single read, so just check the strand and calculate its
        # start position... hope I'm right!
        if bwflag & 16:
            # minus strand, we have to decipher CIGAR string
            thisstrand = 1
            thisstart = atoi( thisfields[ 3 ] ) - 1 + sum( [ <int32_t>(x) for x in findall(b"(\d+)[MDNX=]",CIGAR) ] )	#reverse strand should be shifted alen bp
        else:
            thisstrand = 0
            thisstart = atoi( thisfields[ 3 ] ) - 1

        try:
            thisref = thisref[ :thisref.rindex( b".fa" ) ]
        except ValueError:
            pass

        return ( thisref, thisstart, thisstrand )

cdef class BAMParser( GenericParser ):
    """File Parser Class for BAM File.

    File is gzip-compatible and binary.
    Information available is the same that is in SAM format.

    The bitwise flag is made like this:
    dec	meaning
    ---	-------
    1	paired read
    2	proper pair
    4	query unmapped
    8	mate unmapped
    16	strand of the query (1 -> reverse)
    32	strand of the mate
    64	first read in pair
    128	second read in pair
    256	alignment is not primary
    512	does not pass quality check
    1024	PCR or optical duplicate
    2048	supplementary alignment
    """
    def __init__ ( self, str filename, int64_t buffer_size = 100000 ):
        """Open input file. Determine whether it's a gzipped file.

        'filename' must be a string object.

        This function initialize the following attributes:

        1. self.filename: the filename for input file.
        2. self.gzipped: a boolean indicating whether input file is gzipped.
        3. self.fhd: buffered I/O stream of input file
        """
        self.filename = filename
        self.gzipped = True
        self.tag_size = -1
        self.buffer_size = buffer_size
        # try gzip first
        f = gzip.open( filename )
        try:
            f.read( 10 )
        except IOError:
            # not a gzipped file
            self.gzipped = False
        f.close()
        if self.gzipped:
            # open with gzip.open, then wrap it with BufferedReader!
            self.fhd = io.BufferedReader( gzip.open( filename, mode='rb' ), buffer_size = READ_BUFFER_SIZE) # buffersize set to 1M
        else:
            self.fhd = io.open( filename, mode='rb' ) # binary mode! I don't expect unicode here!

    cpdef sniff( self ):
        """Check the first 3 bytes of BAM file. If it's 'BAM', check
        is success.

        """
        magic_header = self.fhd.read( 3 )
        if magic_header == b"BAM":
            tsize  = self.tsize()
            if tsize > 0:
                self.fhd.seek( 0 )
                return True
            else:
                self.fhd.seek( 0 )
                raise Exception( "File is not of a valid BAM format! %d" % tsize )
        else:
            self.fhd.seek( 0 )
            return False

    cpdef int32_t tsize ( self ):
        """Get tag size from BAM file -- read l_seq field.

        Refer to: http://samtools.sourceforge.net/SAM1.pdf

        * This may not work for BAM file from bedToBAM (bedtools),
        since the l_seq field seems to be 0.
        """

        cdef:
            int32_t x, header_len, nc, nlength
            int32_t n = 0                   # successful read of tag size
            float64_t s = 0                # sum of tag sizes

        if self.tag_size != -1:
            # if we have already calculated tag size (!= -1),  return it.
            return self.tag_size

        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell
        # move to pos 4, there starts something
        fseek( 4 )
        header_len =  unpack( '<i', fread( 4 ) )[ 0 ]
        fseek( header_len + ftell() )
        # get the number of chromosome
        nc = unpack( '<i', fread( 4 ) )[ 0 ]
        for x in range( nc ):
            # read each chromosome name
            nlength = unpack( '<i' , fread( 4 ) )[ 0 ]
            # jump over chromosome size, we don't need it
            fread( nlength )
            fseek( ftell() + 4 )
        while n < 10:
            entrylength = unpack( '<i', fread( 4 ) )[ 0 ]
            data = fread( entrylength )
            a = unpack( '<i', data[16:20] )[ 0 ]
            s += a
            n += 1
        fseek( 0 )
        self.tag_size = <int32_t>(s/n)
        return self.tag_size

    cpdef tuple get_references( self ):
        """
        read in references from BAM header

        return a tuple (references (list of names),
                        rlengths (dict of lengths)
        """
        cdef:
            int32_t header_len, x, nc, nlength
            bytes refname
            list references = []
            dict rlengths = {}

        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell
        # move to pos 4, there starts something
        fseek(4)
        header_len =  unpack( '<i', fread( 4 ) )[ 0 ]
        fseek( header_len + ftell() )
        # get the number of chromosome
        nc = unpack( '<i', fread( 4 ) )[ 0 ]
        for x in range( nc ):
            # read each chromosome name
            nlength = unpack( '<i', fread( 4 ) )[ 0 ]
            refname = fread( nlength )[ :-1 ]
            references.append( refname )
            # don't jump over chromosome size
            # we can use it to avoid falling of chrom ends during peak calling
            rlengths[refname] = unpack( '<i', fread( 4 ) )[ 0 ]
        return (references, rlengths)

    cpdef build_fwtrack ( self ):
        """Build FWTrack from all lines, return a FWTrack object.

        Note only the unique match for a tag is kept.
        """
        cdef:
            int64_t i = 0                           #number of reads kept
            int32_t entrylength, fpos, strand, chrid
            list references
            dict rlengths

        fwtrack = FWTrack( buffer_size = self.buffer_size )
        references, rlengths = self.get_references() # after this, ptr at list of alignments
        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell

        while True:
            try:
                entrylength = unpack( "<i", fread( 4 ) )[0]
            except struct.error:
                break
            ( chrid, fpos, strand ) = bam_se_entry_parser( fread( entrylength ) )
            if chrid == -1: continue
            fwtrack.add_loc( references[ chrid ], fpos, strand )
            i += 1
            if i % 1000000 == 0:
                info( " %d reads parsed" % i )

        #print( f"{references[chrid]:},{fpos:},{strand:}" )
        info( "%d reads have been read." % i )
        self.fhd.close()
        fwtrack.set_rlengths( rlengths )
        return fwtrack

    cpdef append_fwtrack ( self, fwtrack ):
        """Build FWTrack from all lines, return a FWTrack object.

        Note only the unique match for a tag is kept.
        """
        cdef:
            int64_t i = 0                     #number of reads kept
            int32_t entrylength, fpos, strand, chrid
            list references
            dict rlengths

        references, rlengths = self.get_references()
        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell

        while True:
            try:
                entrylength = unpack( '<i', fread( 4 ) )[ 0 ]
            except struct.error:
                break
            ( chrid, fpos, strand ) = bam_se_entry_parser( fread( entrylength ) )
            if chrid == -1: continue
            fwtrack.add_loc( references[ chrid ], fpos, strand )
            i += 1
            if i % 1000000 == 0:
                info( " %d reads parsed" % i )

        info( "%d reads have been read." % i )
        self.fhd.close()
        #fwtrack.finalize()
        # this is the problematic part. If fwtrack is finalized, then it's impossible to increase the length of it in a step of buffer_size for multiple input files.
        fwtrack.set_rlengths( rlengths )
        return fwtrack

cdef class BAMPEParser(BAMParser):
    """File Parser Class for BAM File containing paired-end reads
    Only counts valid pairs, discards everything else
    Uses the midpoint of every read and calculates the average fragment size
    on the fly instead of modeling it

    File is gzip-compatible and binary.
    Information available is the same that is in SAM format.

    The bitwise flag is made like this:
    dec    meaning
    ---    -------
    1    paired read
    2    proper pair
    4    query unmapped
    8    mate unmapped
    16    strand of the query (1 -> reverse)
    32    strand of the mate
    64    first read in pair
    128    second read in pair
    256    alignment is not primary
    512    does not pass quality check
    1024    PCR or optical duplicate
    2048    supplementary alignment
    """
    cdef public int32_t n           # total number of fragments
    cdef public float32_t d         # the average length of fragments

    cpdef object build_petrack ( self ):
        """Build PETrackI from all lines, return a FWTrack object.
        """
        cdef:
            int64_t i          # number of fragments kept
            int64_t m          # sum of fragment lengths
            int32_t entrylength, fpos, chrid, tlen
            list references
            dict rlengths

        i = 0
        m = 0

        petrack = PETrackI( buffer_size = self.buffer_size )
        references, rlengths = self.get_references()
        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell

        # for convenience, only count valid pairs
        add_loc = petrack.add_loc
        err = struct.error

        while True:
            try:
                entrylength = unpack( '<i', fread(4) )[0]
            except err:
                #e1 += 1
                break
            ( chrid, fpos, tlen ) = bampe_pe_entry_parser( fread(entrylength) )
            if chrid == -1:
                #e2 += 1
                continue
            add_loc(references[ chrid ], fpos, fpos + tlen)
            m += tlen
            i += 1
            if i % 1000000 == 0:
                info( " %d fragments parsed" % i )

        #print( f"{references[chrid]:},{fpos:},{tlen:}" )
        info( "%d fragments have been read." % i )
        #debug( f" {e1} Can't identify the length of entry, it may be the end of file, stop looping..." )
        #debug( f" {e2} Chromosome name can't be found which means this entry is skipped ..." )
        #assert i > 0, "Something went wrong, no fragment has been read! Check input file!"
        self.d = m / i
        self.n = i
        #assert self.d >= 0, "Something went wrong (mean fragment size was negative: %d = %d / %d)" % (self.d, m, i)
        self.fhd.close()
        petrack.set_rlengths( rlengths )
        return petrack

    cpdef append_petrack (self, petrack):
        """Build PETrackI from all lines, return a PETrackI object.
        """
        cdef:
            int64_t i = 0          # number of fragments kept
            int64_t m = 0          # sum of fragment lengths
            int32_t entrylength, fpos, chrid, tlen
            list references
            dict rlengths

        references, rlengths = self.get_references()
        fseek = self.fhd.seek
        fread = self.fhd.read
        ftell = self.fhd.tell

        # for convenience, only count valid pairs
        add_loc = petrack.add_loc
        err = struct.error

        while True:
            try:
                entrylength = unpack('<i', fread(4))[0]
            except err:
                break
            ( chrid, fpos, tlen ) = bampe_pe_entry_parser( fread(entrylength) )
            if chrid == -1:
                continue
            add_loc(references[ chrid ], fpos, fpos + tlen)
            m += tlen
            i += 1
            if i % 1000000 == 0:
                info(" %d fragments parsed" % i)

        info( "%d fragments have been read." % i )
        self.d = ( self.d * self.n + m ) / ( self.n + i )
        self.n += i
        #assert self.d >= 0, "Something went wrong (mean fragment size was negative: %d = %d / %d)" % (self.d, m, i)
        self.fhd.close()
        # this is the problematic part. If fwtrack is finalized, then it's impossible to increase the length of it in a step of buffer_size for multiple input files.
        # petrack.finalize()
        petrack.set_rlengths( rlengths )
        return petrack

cdef class BowtieParser( GenericParser ):
    """File Parser Class for map files from Bowtie or MAQ's maqview
    program.

    """
    cdef int32_t tlen_parse_line ( self, bytes thisline ):
        """Parse tag sequence, then tag length.

        """
        cdef list thisfields

        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )
        if thisline[ 0 ] == b"#": return ( b"", -1 , -1 ) # comment line is skipped
        thisfields = thisline.split( b'\t' ) # I hope it will never bring me more trouble
        return len( thisfields[ 4 ] )

    cdef tuple fw_parse_line (self, bytes thisline ):
        """
        The following definition comes from bowtie website:

        The bowtie aligner outputs each alignment on a separate
        line. Each line is a collection of 8 fields separated by tabs;
        from left to right, the fields are:

        1. Name of read that aligned

        2. Orientation of read in the alignment, - for reverse
        complement, + otherwise

        3. Name of reference sequence where alignment occurs, or
        ordinal ID if no name was provided

        4. 0-based offset into the forward reference strand where
        leftmost character of the alignment occurs

        5. Read sequence (reverse-complemented if orientation is -)

        6. ASCII-encoded read qualities (reversed if orientation is
        -). The encoded quality values are on the Phred scale and the
        encoding is ASCII-offset by 33 (ASCII char !).

        7. Number of other instances where the same read aligns
        against the same reference characters as were aligned against
        in this alignment. This is not the number of other places the
        read aligns with the same number of mismatches. The number in
        this column is generally not a good proxy for that number
        (e.g., the number in this column may be '0' while the number
        of other alignments with the same number of mismatches might
        be large). This column was previously described as 'Reserved'.

        8. Comma-separated list of mismatch descriptors. If there are
        no mismatches in the alignment, this field is empty. A single
        descriptor has the format offset:reference-base>read-base. The
        offset is expressed as a 0-based offset from the high-quality
        (5') end of the read.

        """
        cdef:
            list thisfields
            bytes chromname

        thisline = thisline.rstrip()
        if not thisline: return ( b"", -1, -1 )
        if thisline[ 0 ] == b"#": return ( b"", -1, -1 ) # comment line is skipped
        thisfields = thisline.split( b'\t' ) # I hope it will never bring me more trouble

        chromname = thisfields[ 2 ]
        try:
            chromname = chromname[ :chromname.rindex( b".fa" ) ]
        except ValueError:
            pass

            if thisfields[ 1 ] == b"+":
                return ( chromname,
                         atoi( thisfields[ 3 ] ),
                         0 )
            elif thisfields[ 1 ] == b"-":
                return ( chromname,
                         atoi( thisfields[ 3 ] ) + strlen( thisfields[ 4 ] ),
                         1 )
            else:
                raise StrandFormatError( thisline, thisfields[ 1 ] )