1 | ## $Id: yat_check_libbam.m4 3306 2014-08-21 04:37:21Z peter $ |
---|
2 | # |
---|
3 | # serial 5 (yat 0.12.1) |
---|
4 | # |
---|
5 | # |
---|
6 | # Copyright (C) 2012, 2013, 2014 Peter Johansson |
---|
7 | # |
---|
8 | # This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
9 | # |
---|
10 | # The yat library is free software; you can redistribute it and/or |
---|
11 | # modify it under the terms of the GNU General Public License as |
---|
12 | # published by the Free Software Foundation; either version 3 of the |
---|
13 | # License, or (at your option) any later version. |
---|
14 | # |
---|
15 | # The yat library is distributed in the hope that it will be useful, |
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | # General Public License for more details. |
---|
19 | # |
---|
20 | # You should have received a copy of the GNU General Public License |
---|
21 | # along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | |
---|
23 | # YAT_CHECK_HEADER_BAM([action-if-found], [action-if-not-found]) |
---|
24 | # ============================================================== |
---|
25 | # |
---|
26 | AC_DEFUN([YAT_CHECK_HEADER_BAM], |
---|
27 | [ |
---|
28 | yat_bam_header=no |
---|
29 | # check how to #include <bam.h> |
---|
30 | m4_foreach_w([myheader], [bam.h bam/bam.h samtools/bam.h], |
---|
31 | [_YAT_CHECK_HEADER_BAM(myheader) |
---|
32 | ]) |
---|
33 | AS_IF([test x$yat_bam_header = xyes], [$1], [$2]) |
---|
34 | ]) # YAT_CHECK_HEADER_BAM |
---|
35 | |
---|
36 | |
---|
37 | # _YAT_CHECK_HEADER_BAM |
---|
38 | # ===================== |
---|
39 | # Private macro used in YAT_CHECK_HEADER_BAM |
---|
40 | AC_DEFUN([_YAT_CHECK_HEADER_BAM], |
---|
41 | [ |
---|
42 | AS_IF([test x$yat_bam_header = xno], [dnl |
---|
43 | AC_CHECK_HEADER([$1], [ |
---|
44 | AC_DEFINE_UNQUOTED(AS_TR_CPP([YAT_HAVE_$1]), [1], |
---|
45 | [Define to 1 if you have <$1> header file]) |
---|
46 | yat_bam_header=yes; |
---|
47 | ]) |
---|
48 | ]) |
---|
49 | ]) # _YAT_CHECK_HEADER_BAM |
---|
50 | |
---|
51 | |
---|
52 | # YAT_CHECK_LIBBAM([action-if-found], [action-if-not-found]) |
---|
53 | # ========================================================== |
---|
54 | # |
---|
55 | AC_DEFUN([YAT_CHECK_LIBBAM], |
---|
56 | [ |
---|
57 | BAM_LIBS=no |
---|
58 | AC_MSG_CHECKING([for library containing bam_header_destroy]) |
---|
59 | for libs in "" "-lbam" "-lbam -lpthread"; do |
---|
60 | AS_IF([test x"$BAM_LIBS" = x"no"], [ |
---|
61 | YAT_LINK_BAM_IFELSE([$libs], [BAM_LIBS=$libs]) |
---|
62 | ]) |
---|
63 | done |
---|
64 | AS_IF([test x"$BAM_LIBS" = x""], [ |
---|
65 | AC_MSG_RESULT([none required]) |
---|
66 | ],[ |
---|
67 | AC_MSG_RESULT([$BAM_LIBS]) |
---|
68 | ]) |
---|
69 | AS_IF([test x"$BAM_LIBS" = xno], [$2], [$1]) |
---|
70 | ]) # YAT_CHECK_LIBBAM |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | # YAT_LINK_BAM_IFELSE([lib], [action-if-found], [action-if-not-found]) |
---|
75 | # ==================================================================== |
---|
76 | # Add lib to LIBS and try to link some code using libbam |
---|
77 | AC_DEFUN([YAT_LINK_BAM_IFELSE], |
---|
78 | [ |
---|
79 | save_LIBS=$LIBS |
---|
80 | LIBS="$1 $LIBS" |
---|
81 | AC_LINK_IFELSE([AC_LANG_PROGRAM([_YAT_BAM_INCLUDES], |
---|
82 | [ |
---|
83 | bam_header_t* hdr = bam_header_init(); |
---|
84 | bam_header_destroy(hdr); |
---|
85 | ])], |
---|
86 | [$2], [$3]) |
---|
87 | LIBS=$save_LIBS |
---|
88 | ]) # YAT_LINK_BAM_IFELSE |
---|
89 | |
---|
90 | # YAT_BAM_NT16_REV_TABLE |
---|
91 | # ==================================================================== |
---|
92 | # Check if global variable bam_nt16_rev_table is available in |
---|
93 | # -lbam. If found call AC_DEFINE(HAVE_BAM_NT16_REV_TABLE). |
---|
94 | AC_DEFUN([YAT_BAM_NT16_REV_TABLE], |
---|
95 | [ |
---|
96 | AC_MSG_CHECKING([for bam_nt16_rev_table]) |
---|
97 | AC_LINK_IFELSE([AC_LANG_PROGRAM([_YAT_BAM_INCLUDES], |
---|
98 | [char c = bam_nt16_rev_table@<:@7@:>@;]) |
---|
99 | ],[ |
---|
100 | AC_MSG_RESULT([yes]) |
---|
101 | AC_DEFINE([HAVE_BAM_NT16_REV_TABLE], [1], |
---|
102 | [Define to 1 if libbam contains bam_nt16_rev_table]) |
---|
103 | ],[ |
---|
104 | AC_MSG_RESULT([no]) |
---|
105 | ]) |
---|
106 | ]) # YAT_BAM_NT16_REV_TABLE |
---|
107 | |
---|
108 | |
---|
109 | # YAT_FUNC_BAM_CALEND(SAM_FILE) |
---|
110 | # =================== |
---|
111 | # Check if there is a bam_calend that works |
---|
112 | AC_DEFUN([YAT_FUNC_BAM_CALEND], |
---|
113 | [ |
---|
114 | AC_CACHE_CHECK([for working bam_calend], |
---|
115 | [yat_cv_func_bam_calend], |
---|
116 | [AC_RUN_IFELSE( |
---|
117 | [AC_LANG_PROGRAM( |
---|
118 | [_YAT_BAM_INCLUDES], |
---|
119 | [ |
---|
120 | samfile_t* samfile = samopen("$1", "r", NULL); |
---|
121 | if (!samfile) |
---|
122 | return 1; |
---|
123 | bam1_t* read = bam_init1(); |
---|
124 | while (samread(samfile, read) >= -1) { |
---|
125 | bool have_match = false; |
---|
126 | if (read->core.n_cigar < 1) |
---|
127 | continue; |
---|
128 | uint32_t end = bam_calend(&read->core, bam1_cigar(read)); |
---|
129 | // replace BAM_CMATCH with BAM_CEQUAL in cigar |
---|
130 | for (size_t k=0; k<read->core.n_cigar; ++k) { |
---|
131 | uint32_t& element = bam1_cigar(read)@<:@k@:>@; |
---|
132 | uint32_t op = element & BAM_CIGAR_MASK; |
---|
133 | if (op == BAM_CMATCH) { |
---|
134 | have_match = true; |
---|
135 | uint32_t oplen = element >> BAM_CIGAR_SHIFT; |
---|
136 | element = oplen<<BAM_CIGAR_SHIFT|BAM_CEQUAL; |
---|
137 | } |
---|
138 | } |
---|
139 | // if no match element found, next read please |
---|
140 | if (!have_match) |
---|
141 | continue; |
---|
142 | // bam_calend works on BAM_CEQUAL |
---|
143 | if (bam_calend(&read->core, bam1_cigar(read)) == end) |
---|
144 | return 0; |
---|
145 | // bam_calend is not working as expected |
---|
146 | return 2; |
---|
147 | } |
---|
148 | // end of data, fail |
---|
149 | return 3; |
---|
150 | ]) |
---|
151 | ], |
---|
152 | [yat_cv_func_bam_calend=yes], |
---|
153 | [yat_cv_func_bam_calend=no], |
---|
154 | [yat_cv_func_bam_calend="guessing no"]) |
---|
155 | ]) |
---|
156 | AS_IF([test x"$yat_cv_func_bam_calend" = x"yes"],[ |
---|
157 | AC_DEFINE([HAVE_BAM_CALEND], [1], [Define to 1 if bam_calend is working]) |
---|
158 | ]) |
---|
159 | ]) |
---|
160 | |
---|
161 | |
---|
162 | # _YAT_BAM_INCLUDES |
---|
163 | # ================= |
---|
164 | # Private macro that expand PP magic we see in 'yat/omic/bam_config.h' |
---|
165 | AC_DEFUN([_YAT_BAM_INCLUDES], |
---|
166 | [AC_REQUIRE([YAT_CHECK_HEADER_BAM]) |
---|
167 | @%:@if YAT_HAVE_BAM_H |
---|
168 | @%:@ include <bam.h> |
---|
169 | @%:@ include <sam.h> |
---|
170 | @%:@elif YAT_HAVE_BAM_BAM_H |
---|
171 | @%:@ include <bam/bam.h> |
---|
172 | @%:@ include <bam/sam.h> |
---|
173 | @%:@elif YAT_HAVE_SAMTOOLS_BAM_H |
---|
174 | @%:@ include <samtools/bam.h> |
---|
175 | @%:@ include <samtools/sam.h> |
---|
176 | @%:@endif |
---|
177 | ]) # _YAT_BAM_HEADER |
---|