forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertSize.cpp
82 lines (61 loc) · 1.59 KB
/
insertSize.cpp
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
/*
* failQualPair
* Date: Oct-10-2012
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include <iostream>
#include "api/BamMultiReader.h"
#include "api/BamReader.h"
#include "api/BamWriter.h"
#include "api/BamAux.h"
#include "utils.h"
using namespace std;
using namespace BamTools;
int main (int argc, char *argv[]) {
bool onlyMapped=false;
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ){
cout<<"Usage:"<<argv[0]<<"<options> [in bam]"<<endl<<"this program returns the insert size of reads"<<endl;
cerr<<"Options:"<<endl;
cerr<<"\t-m\t\t\t\tUse mapped reads only"<<endl;
return 1;
}
for(int i=1;i<(argc-1);i++){ //all but the last arg
if(string(argv[i]) == "-m" ){
onlyMapped=true;
continue;
}
cerr<<"Unknown option "<<argv[i] <<" exiting"<<endl;
return 1;
}
string bamfiletopen = string(argv[argc-1]);
// cout<<bamfiletopen<<endl;
BamReader reader;
// cout<<"ok"<<endl;
if ( !reader.Open(bamfiletopen) ) {
cerr << "Could not open input BAM files." << endl;
return 1;
}
BamAlignment al;
// cout<<"ok"<<endl;
while ( reader.GetNextAlignment(al) ) {
// cout<<al.Name<<endl;
if(!al.IsMapped())
continue;
if(al.IsPaired()){
if( al.IsFirstMate() ){
if(al.InsertSize > 0)
cout<<al.InsertSize<<endl;
else
cout<<-1.0*al.InsertSize<<endl;
}
}else{
cout<<al.Length<<endl;
}
} //while al
reader.Close();
return 0;
}