File: hits.cpp

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (1097 lines) | stat: -rw-r--r-- 29,032 bytes parent folder | download
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
/*
 *  hits.cpp
 *  Cufflinks
 *
 *  Created by Cole Trapnell on 3/23/09.
 *  Copyright 2009 Cole Trapnell. All rights reserved.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

#include "common.h"
#include "hits.h"
#include "tokenize.h"

using namespace std;

#if ENABLE_THREADS
boost::mutex RefSequenceTable::table_lock;
#endif

int num_deleted = 0;

void ReadHit::trim(int trimmed_length)
{
    bool antisense_aln = _sam_flag & 0x10;
    
    vector<CigarOp> new_cigar;
    int new_left = 0;
    
    if (!antisense_aln)
    {
        int pos = _left;
        new_left = _left;
        int length = 0;
		for (vector<CigarOp>::iterator i = _cigar.begin(); i < _cigar.end(); ++i)
		{
			const CigarOp& op = *i;
            
            if (length < trimmed_length)
            {
                switch(op.opcode)
                {
                    case REF_SKIP:
                        //gaps_out.push_back(make_pair(pos, pos + op.length - 1));
                        pos += op.length;
                        new_cigar.push_back(op);
                        break;
                    case SOFT_CLIP:
                        assert(false); // not sure if this case is right
                        pos += op.length;
                        length += op.length;
                        new_cigar.push_back(op);
                        break;
                    case HARD_CLIP:
                        new_cigar.push_back(op);
                        break;
                    case MATCH:
                        if (length + op.length < trimmed_length)
                        {
                            pos += op.length;
                            length += op.length;
                            new_cigar.push_back(op);
                        }
                        else
                        {
                            new_cigar.push_back(CigarOp(MATCH, trimmed_length - length));
                            pos += trimmed_length - length;
                            length += trimmed_length - length;
                        }
                        break;
                    case INS:
                        assert(false); // not sure if this case is right
                        pos -= op.length;
                        length -= op.length;
                        new_cigar.push_back(op);
                        break;
                    case DEL:
                        assert(false); // not sure if this case is right
                        pos += op.length;
                        length += op.length;
                        new_cigar.push_back(op);
                        break;
                    default:
                        break;
                }
            }
		}
    }
    else
    {
        int pos = _right;
        int length = 0;
		for (vector<CigarOp>::reverse_iterator i = _cigar.rbegin(); i < _cigar.rend(); ++i)
		{
			const CigarOp& op = *i;
            
            if (length < trimmed_length)
            {
                switch(op.opcode)
                {
                    case REF_SKIP:
                        //gaps_out.push_back(make_pair(pos, pos + op.length - 1));
                        pos -= op.length;
                        new_cigar.push_back(op);
                        break;
                    case SOFT_CLIP:
                        assert(false); // not sure if this case is right
                        pos -= op.length;
                        length += op.length;
                        new_cigar.push_back(op);
                        break;
                    case HARD_CLIP:
                        new_cigar.push_back(op);
                        break;
                    case MATCH:
                        if (length + op.length < trimmed_length)
                        {
                            pos -= op.length;
                            length += op.length;
                            new_cigar.push_back(op);
                        }
                        else
                        {
                            new_cigar.push_back(CigarOp(MATCH, trimmed_length - length));
                            pos -= trimmed_length - length;
                            length += trimmed_length - length;
                        }
                        break;
                    case INS:
                        assert(false); // not sure if this case is right
                        pos += op.length;
                        length -= op.length;
                        new_cigar.push_back(op);
                        break;
                    case DEL:
                        assert(false); // not sure if this case is right
                        pos -= op.length;
                        length += op.length;
                        new_cigar.push_back(op);
                        break;
                    default:
                        break;
                }
            }
		}
        _left = pos;
    }
    _cigar = new_cigar;
    _right = get_right();
    assert (trimmed_length == read_len());
}

//static const int max_read_length = 1024;

bool hit_insert_id_lt(const ReadHit& h1, const ReadHit& h2)
{
	return h1.insert_id() < h2.insert_id();
}

bool hits_eq_mod_id(const ReadHit& lhs, const ReadHit& rhs)
{
	return (lhs.ref_id() == rhs.ref_id() &&
			lhs.antisense_align() == rhs.antisense_align() &&
			lhs.left() == rhs.left() && 
			lhs.source_strand() == rhs.source_strand() &&
			lhs.cigar() == rhs.cigar());
}

// Compares for structural equality, but won't declare multihits equal to one another
bool hits_eq_non_multi(const MateHit& lhs, const MateHit& rhs)
{
	if ((lhs.is_multi() || rhs.is_multi() ) && lhs.insert_id() != rhs.insert_id())
		return false;
	return hits_equals(lhs, rhs);
}

// Compares for structural equality, but won't declare multihits equal to one another
// and won't return true for hits from different read groups (e.g. replicate samples)
bool hits_eq_non_multi_non_replicate(const MateHit& lhs, const MateHit& rhs)
{
	if ((lhs.is_multi() || rhs.is_multi() || lhs.read_group_props() != rhs.read_group_props()) && lhs.insert_id() != rhs.insert_id())
		return false;
	return hits_equals(lhs, rhs);
}
    
// Does NOT care about the read group this hit came from.
bool hits_equals(const MateHit& lhs, const MateHit& rhs) 
{
	if (lhs.ref_id() != rhs.ref_id())
		return false;
	
	if ((lhs.left_alignment() == NULL) != (rhs.left_alignment() == NULL))
		return false;
	if ((lhs.right_alignment() == NULL) != (rhs.right_alignment() == NULL))
		return false;
	if (lhs.left_alignment())
	{
		if (!(hits_eq_mod_id(*lhs.left_alignment(),*(rhs.left_alignment()))))
			return false;
	}
	if (lhs.right_alignment())
	{
		if (!(hits_eq_mod_id(*lhs.right_alignment(),*(rhs.right_alignment()))))
			return false;
	}
	return true;
}

bool has_no_collapse_mass(const MateHit& hit)
{
	return hit.collapse_mass() == 0;
}

// Assumes hits are sorted by mate_hit_lt
// Does not collapse hits that are multi-reads
void collapse_hits(const vector<MateHit>& hits,
				   vector<MateHit>& non_redundant)
{
	copy(hits.begin(), hits.end(), back_inserter(non_redundant));
	vector<MateHit>::iterator new_end = unique(non_redundant.begin(), 
											   non_redundant.end(), 
											   hits_eq_non_multi_non_replicate);
	non_redundant.erase(new_end, non_redundant.end());
    non_redundant.resize(non_redundant.size());
	
	foreach(MateHit& hit, non_redundant)
		hit.collapse_mass(0);
	
	size_t curr_aln = 0;
	size_t curr_unique_aln = 0;
	while (curr_aln < hits.size())
	{
		if (hits_eq_non_multi_non_replicate(non_redundant[curr_unique_aln], hits[curr_aln]) || hits_eq_non_multi_non_replicate(non_redundant[++curr_unique_aln], hits[curr_aln]))
		{
            double more_mass = hits[curr_aln].common_scale_mass();
			//assert(non_redundant[curr_unique_aln].collapse_mass() == 0 || !non_redundant[curr_unique_aln].is_multi());
			non_redundant[curr_unique_aln].incr_collapse_mass(more_mass);
		}
		else
			assert(false);
		
		++curr_aln;
	}
	
	//foreach(MateHit& hit, non_redundant)
		//assert(hit.collapse_mass() <= 1 || !hit.is_multi());
	
	//non_redundant.erase(remove_if(non_redundant.begin(),non_redundant.end(),has_no_collapse_mass), non_redundant.end()); 
	
}

// Places multi-reads to the right of reads they match
bool mate_hit_lt(const MateHit& lhs, const MateHit& rhs)
{
	if (lhs.left() != rhs.left())
		return lhs.left() < rhs.left();
	if (lhs.right() != rhs.right())
		return lhs.right() > rhs.right();
	
	if ((lhs.left_alignment() == NULL) != (rhs.left_alignment() == NULL))
		return (lhs.left_alignment() == NULL) < (rhs.left_alignment() == NULL);
	
	if ((lhs.right_alignment() == NULL) != (rhs.right_alignment() == NULL))
		return (lhs.right_alignment() == NULL) < (rhs.right_alignment() == NULL);
	
	assert ((lhs.right_alignment() == NULL) == (rhs.right_alignment() == NULL));
	assert ((lhs.left_alignment() == NULL) == (rhs.left_alignment() == NULL));
	
	const ReadHit* lhs_l = lhs.left_alignment();
	const ReadHit* lhs_r = lhs.right_alignment();
	
	const ReadHit* rhs_l = rhs.left_alignment();
	const ReadHit* rhs_r = rhs.right_alignment();
	
	if (lhs_l && rhs_l)
	{
		if (lhs_l->cigar().size() != rhs_l->cigar().size())
			return lhs_l->cigar().size() < rhs_l->cigar().size(); 
		for (size_t i = 0; i < lhs_l->cigar().size(); ++i)
		{
			if (lhs_l->cigar()[i].opcode != rhs_l->cigar()[i].opcode)
				return lhs_l->cigar()[i].opcode < rhs_l->cigar()[i].opcode;
			if (lhs_l->cigar()[i].length != rhs_l->cigar()[i].length)
				return lhs_l->cigar()[i].length < rhs_l->cigar()[i].length;
		}
	}
	
	if (lhs_r && rhs_r)
	{
		if (lhs_r->cigar().size() != rhs_r->cigar().size())
			return lhs_r->cigar().size() < rhs_r->cigar().size(); 
		for (size_t i = 0; i < lhs_r->cigar().size(); ++i)
		{
			if (lhs_r->cigar()[i].opcode != rhs_r->cigar()[i].opcode)
				return lhs_r->cigar()[i].opcode < rhs_r->cigar()[i].opcode;
			if (lhs_r->cigar()[i].length != rhs_r->cigar()[i].length)
				return lhs_r->cigar()[i].length < rhs_r->cigar()[i].length;
		}
	}
	
	if (lhs.is_multi() != rhs.is_multi())
	{
		return rhs.is_multi();
	}
	
	return false;
}


ReadHit HitFactory::create_hit(const string& insert_name, 
							   const string& ref_name,
							   int left,
							   const vector<CigarOp>& cigar,
							   CuffStrand source_strand,
							   const string& partner_ref,
							   int partner_pos, 
							   unsigned int edit_dist,
							   int num_hits,
                               float base_mass,
                               uint32_t sam_flag)
{
	InsertID insert_id = _insert_table.get_id(insert_name);
	RefID reference_id = _ref_table.get_id(ref_name, NULL);
	RefID partner_ref_id = _ref_table.get_id(partner_ref, NULL);
	
	return ReadHit(reference_id,
				   insert_id,
				   left, 
				   cigar, 
				   source_strand,
				   partner_ref_id,
				   partner_pos,
				   edit_dist,
				   num_hits,
                   base_mass,
                   sam_flag);	
}

ReadHit HitFactory::create_hit(const string& insert_name, 
							   const string& ref_name,
							   uint32_t left,
							   uint32_t read_len,
							   CuffStrand source_strand,
							   const string& partner_ref,
							   int partner_pos,
							   unsigned int edit_dist,
							   int num_hits,
                               float base_mass,
                               uint32_t sam_flag)
{
	InsertID insert_id = _insert_table.get_id(insert_name);
	RefID reference_id = _ref_table.get_id(ref_name, NULL);
	RefID partner_ref_id = _ref_table.get_id(partner_ref, NULL);
	
	return ReadHit(reference_id,
				   insert_id, 
				   left,
				   read_len,
				   source_strand,
				   partner_ref_id,
				   partner_pos,
				   edit_dist,
				   num_hits,
                   base_mass,
                   sam_flag);	
}

// populate a bam_t This will 
bool BAMHitFactory::next_record(const char*& buf, size_t& buf_size)
{
    if (_next_hit.data)
    {
        free(_next_hit.data);
        _next_hit.data = NULL;
    }
    
    if (records_remain() == false)
        return false;
    
	mark_curr_pos();
    
    memset(&_next_hit, 0, sizeof(_next_hit));
    
	int bytes_read = samread(_hit_file, &_next_hit);
	if (bytes_read < 0)
    {
        _eof_encountered = true;
		return false;
    }
	buf = (const char*)&_next_hit;
	buf_size = bytes_read;
	
	return true;
}

CuffStrand use_stranded_protocol(uint32_t sam_flag,  MateStrandMapping msm)
{
    bool antisense_aln = sam_flag & 0x10;
	if (((sam_flag & BAM_FPAIRED) && (sam_flag & BAM_FREAD1)) || !(sam_flag & BAM_FPAIRED)) // first-in-pair or single-end
	{
		switch(msm)
		{
			case FF:
			case FR:
				return (antisense_aln) ? CUFF_REV : CUFF_FWD;
				break;
			case RF:
			case RR:
				return (antisense_aln) ? CUFF_FWD : CUFF_REV;
				break;
		}
	}
	else // second-in-pair read
	{
		switch (msm)
		{
			case FF:
			case RF:
				return (antisense_aln) ? CUFF_REV : CUFF_FWD;
				break;
			case FR:
			case RR:
				return (antisense_aln) ? CUFF_FWD : CUFF_REV;
				break;
		}
	}
	assert(false);
    return CUFF_STRAND_UNKNOWN;
}


bool BAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf, 
									 ReadHit& bh,
									 bool strip_slash,
									 char* name_out,
									 char* name_tags)
{
	const bam1_t* hit_buf = (const bam1_t*)orig_bwt_buf;
	
	uint32_t sam_flag = hit_buf->core.flag;
	
	int text_offset = hit_buf->core.pos;
	int text_mate_pos = hit_buf->core.mpos;
	int target_id = hit_buf->core.tid;
	int mate_target_id = hit_buf->core.mtid;
	
	vector<CigarOp> cigar;
	bool spliced_alignment = false;
	int num_hits = 1;
	
	//header->target_name[c->tid]
	
	if (sam_flag & 0x4 || target_id < 0)
	{
		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
		bh = create_hit(bam1_qname(hit_buf),
						"*",
						0, // SAM files are 1-indexed
						0,
						CUFF_STRAND_UNKNOWN,
						"*",
						0,
						0,
						1,
                        1.0,
                        sam_flag);
		return true;
	}
	if (target_id >= _hit_file->header->n_targets)
    {
        fprintf (stderr, "BAM error: file contains hits to sequences not in header SQ records (%s)\n", bam1_qname(hit_buf));
        return false;
    }
    
	string text_name = _hit_file->header->target_name[target_id];
	
	for (int i = 0; i < hit_buf->core.n_cigar; ++i) 
	{
		//char* t;

		int length = bam1_cigar(hit_buf)[i] >> BAM_CIGAR_SHIFT;
		if (length <= 0)
		{
		  fprintf (stderr, "BAM error: CIGAR op has zero length (%s)\n", bam1_qname(hit_buf));
			return false;
		}
		
		CigarOpCode opcode;
		switch(bam1_cigar(hit_buf)[i] & BAM_CIGAR_MASK)
		{
			case BAM_CMATCH: opcode  = MATCH; break; 
			case BAM_CINS: opcode  = INS; break;
			case BAM_CDEL: opcode  = DEL; break; 
			case BAM_CSOFT_CLIP: opcode  = SOFT_CLIP; break;
			case BAM_CHARD_CLIP: opcode  = HARD_CLIP; break;
			case BAM_CPAD: opcode  = PAD; break; 
			case BAM_CREF_SKIP:
                opcode = REF_SKIP;
				spliced_alignment = true;
				if (length > (int)max_intron_length)
				{
					//fprintf(stderr, "Encounter REF_SKIP > max_gene_length, skipping\n");
					return false;
				}
				break;
			default:
				//fprintf (stderr, "SAM error on line %d: invalid CIGAR operation\n", _line_num);
				return false;
		}
		if (opcode != HARD_CLIP)
			cigar.push_back(CigarOp(opcode, length));
	}
	
	string mrnm;
	if (mate_target_id >= 0)
	{
		if (mate_target_id == target_id)
		{
			mrnm = _hit_file->header->target_name[mate_target_id];
//			if (abs((int)text_mate_pos - (int)text_offset) > (int)max_intron_length)
//			{
//				//fprintf (stderr, "Mates are too distant, skipping\n");
//				return false;
//			}
		}
		else
		{
			//fprintf(stderr, "Trans-spliced mates are not currently supported, skipping\n");
			return false;
		}
	}
	else
	{
		text_mate_pos = 0;
	}
	
	CuffStrand source_strand = CUFF_STRAND_UNKNOWN;
	unsigned char num_mismatches = 0;

	uint8_t* ptr = bam_aux_get(hit_buf, "XS");
	if (ptr)
	{
		char src_strand_char = bam_aux2A(ptr);
		if (src_strand_char == '-')
			source_strand = CUFF_REV;
		else if (src_strand_char == '+')
			source_strand = CUFF_FWD;
	}
	
	ptr = bam_aux_get(hit_buf, "NM");
	if (ptr)
	{
		num_mismatches = bam_aux2i(ptr);
	}

	ptr = bam_aux_get(hit_buf, "NH");
	if (ptr)
	{
		num_hits = bam_aux2i(ptr);
	}
    
    double mass = 1.0;
    ptr = bam_aux_get(hit_buf, "ZF");
	if (ptr)
	{
		mass = bam_aux2i(ptr);
        if (mass <= 0.0)
            mass = 1.0;
	}
	    
    if (_rg_props.strandedness() == STRANDED_PROTOCOL && source_strand == CUFF_STRAND_UNKNOWN)
		source_strand = use_stranded_protocol(sam_flag, _rg_props.mate_strand_mapping());
    
	if (!spliced_alignment)
	{
		//assert(_rg_props.strandedness() == STRANDED_PROTOCOL || source_strand == CUFF_STRAND_UNKNOWN);

		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
		bh = create_hit(bam1_qname(hit_buf),
						text_name,
						text_offset,  // BAM files are 0-indexed
						cigar,
						source_strand,
						mrnm,
						text_mate_pos,
						num_mismatches,
						num_hits,
                        mass,
                        sam_flag);
		return true;
		
	}
	else
	{	
		if (source_strand == CUFF_STRAND_UNKNOWN)
		{
			fprintf(stderr, "BAM record error: found spliced alignment without XS attribute\n");
		}
		
		bh = create_hit(bam1_qname(hit_buf),
						text_name,
						text_offset,  // BAM files are 0-indexed
						cigar,
						source_strand,
						mrnm,
						text_mate_pos,
						num_mismatches,
						num_hits,
                        mass,
                        sam_flag);
		return true;
	}
	
	
	return true;
}



Platform str_to_platform(const string pl_str)
{
    if (pl_str == "SOLiD")
    {
        return SOLID;
    }
    else if (pl_str == "Illumina") 
    {
        return ILLUMINA;
    }
    else 
    {
        return UNKNOWN_PLATFORM;
    }
}

// Parses the header to determine platform and other properties
bool HitFactory::parse_header_string(const string& header_rec,
                                     ReadGroupProperties& rg_props)
{
    vector<string> columns;
    tokenize(header_rec, "\t", columns); 
    
    if (columns[0] == "@RG")
    {
        for (size_t i = 1; i < columns.size(); ++i)
        {
            vector<string> fields;
            tokenize(columns[i], ":", fields);
            if (fields[0] == "PL")
            {
                if (rg_props.platform() == UNKNOWN_PLATFORM)
                {
                    Platform p = str_to_platform(fields[1]);
                    rg_props.platform(p);
                }
                else 
                {
                    Platform p = str_to_platform(fields[1]);
                    if (p != rg_props.platform())
                    {
                        fprintf(stderr, "Error: Processing reads from different platforms is not currently supported\n");
                        return false;
                    }
                }

            }
        }
    }
    else if (columns[0] == "@SQ")
    {
        _num_seq_header_recs++;
        for (size_t i = 1; i < columns.size(); ++i)
        {
            vector<string> fields;
            tokenize(columns[i], ":", fields);
            if (fields[0] == "SN")
            {
                // Populate the RefSequenceTable with the sequence dictionary, 
                // to ensure that (for example) downstream GTF files are sorted
                // in an order consistent with the header, and to enforce that
                // BAM records appear in the order implied by the header
                RefID _id = _ref_table.get_id(fields[1], NULL);

                const RefSequenceTable::SequenceInfo* info = _ref_table.get_info(_id);
				
                if (info->observation_order != _num_seq_header_recs)
                {
                    if (info->name != fields[1])
                    {
                        fprintf(stderr, "Error: Hash collision between references '%s' and '%s'.\n", info->name, fields[1].c_str());
                    }
                    else
                    {
                        fprintf(stderr, "Error: sort order of reads in BAMs must be the same\n");
					}
                    exit(1);
                }
            }
        }
    }

    return true;
}

void HitFactory::finalize_rg_props()
{
    if (_rg_props.platform() == SOLID)
    {
        _rg_props.strandedness(STRANDED_PROTOCOL);
        _rg_props.std_mate_orientation(MATES_POINT_TOWARD);
    }
    else
    {
        // Default to Illumina's unstranded protocol params for strandedness and
        // mate orientation
        _rg_props.strandedness(UNSTRANDED_PROTOCOL);
        _rg_props.std_mate_orientation(MATES_POINT_TOWARD);
    }
}

static const unsigned MAX_HEADER_LEN = 4 * 1024 * 1024; // 4 MB

bool BAMHitFactory::inspect_header()
{
    bam_header_t* header = _hit_file->header;
    
    if (header == NULL)
    {
        fprintf(stderr, "Warning: No BAM header\n");
        return false;
    }
    
    if (header->l_text >= MAX_HEADER_LEN)
    {
        fprintf(stderr, "Warning: BAM header too large\n");
        return false;
    }

	if (header->l_text == 0)
	{
		fprintf(stderr, "Warning: BAM header has 0 length or is corrupted.  Try using 'samtools reheader'.\n");
        return false;
	}
	
	
    if (header->text != NULL)
    {
        char* h_text = strdup(header->text);
        char* pBuf = h_text;
        while(pBuf - h_text < header->l_text)
        {
            char* nl = strchr(pBuf, '\n');
            if (nl) 
            {
                *nl = 0; 
                parse_header_string(pBuf, _rg_props);
                pBuf = ++nl;
            }
            else 
            {
                pBuf = h_text + header->l_text;
            }
        }
        
        free(h_text);
    }
    
    finalize_rg_props();
    return true;
}


bool SAMHitFactory::next_record(const char*& buf, size_t& buf_size)
{
	mark_curr_pos();
	
	bool new_rec = fgets(_hit_buf,  _hit_buf_max_sz - 1, _hit_file);
	if (!new_rec)
		return false;
	++_line_num;
	char* nl = strrchr(_hit_buf, '\n');
	if (nl) *nl = 0;
	buf = _hit_buf;
	buf_size = _hit_buf_max_sz - 1;
	return true;
}

bool SAMHitFactory::get_hit_from_buf(const char* orig_bwt_buf, 
									 ReadHit& bh,
									 bool strip_slash,
									 char* name_out,
									 char* name_tags)
{	
	char bwt_buf[10*2048];

	strcpy(bwt_buf, orig_bwt_buf);
	// Are we still in the header region?
	if (bwt_buf[0] == '@')
		return false;
	
	const char* buf = bwt_buf;
	const char* _name = strsep((char**)&buf,"\t");
	if (!_name)
		return false;
	char name[2048];
	strncpy(name, _name, 2047); 
    
	const char* sam_flag_str = strsep((char**)&buf,"\t");
	if (!sam_flag_str)
		return false;
	
	const char* text_name = strsep((char**)&buf,"\t");
	if (!text_name)
		return false;
	
	const char* text_offset_str = strsep((char**)&buf,"\t");
	if (!text_offset_str)
		return false;
	
	const char* map_qual_str =  strsep((char**)&buf,"\t");
	if (!map_qual_str)
		return false;
	
	const char* cigar_str = strsep((char**)&buf,"\t");
	if (!cigar_str)
		return false;
	
	const char* mate_ref_name =  strsep((char**)&buf,"\t");
	if (!mate_ref_name)
		return false;
	
	const char* mate_pos_str =  strsep((char**)&buf,"\t");
	if (!mate_pos_str)
		return false;
	
	const char* inferred_insert_sz_str =  strsep((char**)&buf,"\t");
	if (!inferred_insert_sz_str)
		return false;
	
	const char* seq_str =  strsep((char**)&buf,"\t");
	if (!seq_str)
		return false;
	
	const char* qual_str =  strsep((char**)&buf,"\t");
	if (!qual_str)
		return false;
	
	
	int sam_flag = atoi(sam_flag_str);
	int text_offset = atoi(text_offset_str);
	int text_mate_pos = atoi(mate_pos_str);
	
	// Copy the tag out of the name field before we might wipe it out
	char* pipe = strrchr(name, '|');
	if (pipe)
	{
		if (name_tags)
			strcpy(name_tags, pipe);
		*pipe = 0;
	}
	// Stripping the slash and number following it gives the insert name
	char* slash = strrchr(name, '/');
	if (strip_slash && slash)
		*slash = 0;
	
	const char* p_cig = cigar_str;
	//int len = strlen(sequence);
	vector<CigarOp> cigar;
	bool spliced_alignment = false;
	int num_hits = 1;
	    
    if ((sam_flag & 0x4) ||!strcmp(text_name, "*"))
	{
		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
		bh = create_hit(name,
						"*",
						0, // SAM files are 1-indexed
						0,
						CUFF_STRAND_UNKNOWN,
						"*",
						0,
						0,
						1,
                        1.0,
                        sam_flag);
		return true;
	}
	// Mostly pilfered direct from the SAM tools:
	while (*p_cig) 
	{
		char* t;
		int length = (int)strtol(p_cig, &t, 10);
		if (length <= 0)
		{
			fprintf (stderr, "SAM error on line %d: CIGAR op has zero length\n", _line_num);
			return false;
		}
		char op_char = toupper(*t);
		CigarOpCode opcode;
		if (op_char == 'M') 
		{
			/*if (length > max_read_length)
			 {
			 fprintf(stderr, "SAM error on line %d:  %s: MATCH op has length > %d\n", line_num, name, max_read_length);
			 return false;
			 }*/
			opcode = MATCH;
		}
		else if (op_char == 'I') opcode = INS;
		else if (op_char == 'D') 
        {
            opcode = DEL;
        }
		else if (op_char == 'N')
		{
			opcode = REF_SKIP;
			spliced_alignment = true;
			if (length > (int)max_intron_length)
			{
				//fprintf(stderr, "Encounter REF_SKIP > max_gene_length, skipping\n");
				return false;
			}
		}
		else if (op_char == 'S') opcode = SOFT_CLIP;
		else if (op_char == 'H') opcode = HARD_CLIP;
		else if (op_char == 'P') opcode = PAD;
		else
		{
			fprintf (stderr, "SAM error on line %d: invalid CIGAR operation\n", _line_num);
			return false;
		}
		p_cig = t + 1;
		//i += length;
		if (opcode != HARD_CLIP)
			cigar.push_back(CigarOp(opcode, length));
	}
	if (*p_cig)
	{
		fprintf (stderr, "SAM error on line %d: unmatched CIGAR operation\n", _line_num);
		return false;
	}
    
	string mrnm;
	if (strcmp(mate_ref_name, "*"))
	{
		if (!strcmp(mate_ref_name, "=") || !strcmp(mate_ref_name, text_name))
		{
			mrnm = text_name;
//			if (abs((int)text_mate_pos - (int)text_offset) > (int)max_intron_length)
//			{
//				//fprintf (stderr, "Mates are too distant, skipping\n");
//				return false;
//			}
		}
		else
		{
			//fprintf(stderr, "Trans-spliced mates are not currently supported, skipping\n");
			return false;
		}
	}
	else
	{
		text_mate_pos = 0;
	}
	
	CuffStrand source_strand = CUFF_STRAND_UNKNOWN;
	unsigned char num_mismatches = 0;
	
	const char* tag_buf = buf;
	
    double mass = 1.0;
    
	while((tag_buf = strsep((char**)&buf,"\t")))
	{
		
		char* first_colon = (char*)strchr(tag_buf, ':');
		if (first_colon)
		{
			*first_colon = 0;
			++first_colon;
			char* second_colon = strchr(first_colon, ':');
			if (second_colon)
			{
				*second_colon = 0;
				++second_colon;
				const char* first_token = tag_buf;
				//const char* second_token = first_colon;
				const char* third_token = second_colon;
				if (!strcmp(first_token, "XS"))
				{				
					if (*third_token == '-')
						source_strand = CUFF_REV;
					else if (*third_token == '+')
						source_strand = CUFF_FWD;
				}
				else if (!strcmp(first_token, "NM"))
				{
					num_mismatches = atoi(third_token);
				}
				else if (!strcmp(first_token, "NH"))
				{
                    num_hits = atoi(third_token);
				}
                else if (!strcmp(first_token, "ZF"))
				{
					mass = atof(third_token);
                    if (mass <= 0.0)
                        mass = 1.0;
				}
				else 
				{
					
				}
			}
		}
	}
        
    // Don't let the protocol setting override explicit XS tags
	if (_rg_props.strandedness() == STRANDED_PROTOCOL && source_strand == CUFF_STRAND_UNKNOWN)
		source_strand = use_stranded_protocol(sam_flag, _rg_props.mate_strand_mapping());
	
	if (!spliced_alignment)
	{		
		//assert(cigar.size() == 1 && cigar[0].opcode == MATCH);
		bh = create_hit(name,
						text_name,
						text_offset - 1,
						cigar,
						source_strand,
						mrnm,
						text_mate_pos - 1,
						num_mismatches,
						num_hits,
                        mass,
                        sam_flag);
		return true;
		
	}
	else
	{	
		if (source_strand == CUFF_STRAND_UNKNOWN)
		{
			fprintf(stderr, "SAM error on line %d: found spliced alignment without XS attribute\n", _line_num);
		}
		
		bh = create_hit(name,
						text_name,
						text_offset - 1,
						cigar,
						source_strand,
						mrnm,
						text_mate_pos - 1,
						num_mismatches,
						num_hits,
                        mass,
                        sam_flag);
		return true;
	}
	return false;
}

bool SAMHitFactory::inspect_header()
{
    char pBuf[10 * 1024];
    
    off_t curr_pos = ftello(_hit_file);
    rewind(_hit_file);
    
    while (fgets(pBuf, 10*1024, _hit_file))
    {
        if (pBuf[0] != '@')
        {
            break; // done with the header.
        }
        char* nl = strchr(pBuf, '\n');
        if (nl) 
        {
            *nl = 0; 
            parse_header_string(pBuf, _rg_props);
        }
    }
    
    fseek(_hit_file, curr_pos, SEEK_SET);
    
    finalize_rg_props();
    return true;
}