source: trunk/m4/yat_check_libbam.m4 @ 3415

Last change on this file since 3415 was 3415, checked in by Peter, 4 years ago

refs #794

Change logic in configure test looking for libhts/samtools headers and
libraries. Instead of looking whether both headers and libraries are
available, first look for both and <htslib/hts.h> and libhts. If found
use that. If not found, look if both <bam.h> and libbam are
available. If yes, use them. If not abort.

Only test for function bam_calend and global variable
bam_nt16_rev_table when building against libbam as they are irrelevant
in htslib mode.

yat/utility/config_public.h.in: change back to macros
YAT_HAVE_BAM_H
YAT_HAVE_BAM_BAM_H
YAT_HAVE_SAMTOOLS_BAM_H
as they were called in 0.12. Add a new macro YAT_HAVE_HTSLIB_SAM_H,
which is defined when builiding against htslib (just like
YAT_HAVE_HTSLIB).

yat/omic/config_bam.h: use new macro names in
'yat/utility/config_public.h' and avoid having nested if statements
and instead use style:

#if defined YAT_HAVE_HTSLIB_HTS_H
# define YAT_SAM_HEADER "htslib/sam.h"
#elif defined YAT_HAVE_BAM_H
# define YAT_BAM_HEADER "bam.h"
# define YAT_SAM_HEADER "sam.h"
#elif defined YAT_HAVE_BAM_BAM_H
# define YAT_BAM_HEADER "bam/bam.h"
# define YAT_SAM_HEADER "bam/sam.h"
#endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date
File size: 7.2 KB
Line 
1## $Id: yat_check_libbam.m4 3415 2015-04-30 07:36:46Z peter $
2#
3# serial 7 (yat 0.13)
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
24
25# YAT_CHECK_HTS([action-if-found], [action-if-not-found])
26# =====================
27# if header and lib exist execute action-if-found, otherwise execute
28# action-if-not-found
29AC_DEFUN([YAT_CHECK_HTS],
30[
31  YAT_HEADER_HTS([YAT_LIB_HTS([$1], [$2])],
32                 [$2])
33])
34
35
36# YAT_HEADER_HTS([action-if-found], [action-if-not-found])
37# =====================
38# Check if header hts.h is available
39AC_DEFUN([YAT_HEADER_HTS],
40[
41  YAT_CHECK_HEADERS([htslib/hts.h], [$1], [$2])
42]) # YAT_HEADER_HTS
43
44
45# YAT_LIB_HTS([action-if-found], [action-if-not-found])
46# =====================
47# Check if bam_hdr_destroy is available
48AC_DEFUN([YAT_LIB_HTS],
49[
50BAM_LIBS=no
51AC_SEARCH_LIBS([bam_hdr_destroy], [hts],
52               [AS_IF([test "$ac_cv_search_bam_hdr_destroy" != "none required"],
53                      [BAM_LIBS=$ac_cv_search_bam_hdr_destroy])
54                $1],
55               [$2])
56]) # YAT_LIB_HTS
57
58
59# YAT_CHECK_BAM([action-if-found], [action-if-not-found])
60# =======================================================
61# Check if header and lib is available for old samtools API 0.1.x
62AC_DEFUN([YAT_CHECK_BAM],
63[
64  YAT_HEADER_BAM([YAT_LIB_BAM([$1], [$2])],
65                 [$2])
66])
67
68
69# YAT_HEADER_BAM([action-if-found], [action-if-not-found])
70# ==============================================================
71# Check how to include bam.h
72AC_DEFUN([YAT_HEADER_BAM],
73[
74  YAT_CHECK_HEADERS([bam.h bam/bam.h samtools/bam.h], [$1], [$2])
75]) # YAT_HEADER_BAM
76
77
78# YAT_CHECK_HEADERS(headers, [action-if-found], [action-if-not-found])
79# ====================================================================
80# Loop over headers (space-separated), check if header exist. If
81# header exists, AC_DEFINE(YAT_HAVE_header) and execute shell command
82# action-if-found. If none of headers is available, execute
83# action-if-not-found.
84AC_DEFUN([YAT_CHECK_HEADERS],
85[
86yat_check_headers_result=no
87m4_foreach_w([myheader], [$1], [_YAT_CHECK_HEADERS(myheader)])
88AS_IF([test x$yat_check_headers_result = xyes], [$2], [$3])
89]) # YAT_CHECK_HEADERS
90
91
92# _YAT_CHECK_HEADERS
93# =====================
94# Private macro used in YAT_CHECK_HEADERS
95AC_DEFUN([_YAT_CHECK_HEADERS],
96[
97AS_IF([test x$yat_check_headers_result = xno], [dnl
98  AC_CHECK_HEADER([$1], [
99    AC_DEFINE_UNQUOTED(AS_TR_CPP([YAT_HAVE_$1]), [1],
100                       [Define to 1 if you have <$1> header file])
101    yat_check_headers_result=yes;
102  ])
103])
104]) # _YAT_CHECK_HEADER_BAM
105
106
107# YAT_LIB_BAM([action-if-found], [action-if-not-found])
108# ==========================================================
109#
110AC_DEFUN([YAT_LIB_BAM],
111[
112BAM_LIBS=no
113AC_MSG_CHECKING([for library containing bam_header_destroy])
114for libs in "" "-lbam" "-lbam -lpthread"; do
115  AS_IF([test x"$BAM_LIBS" = x"no"], [
116    YAT_LINK_BAM_IFELSE([$libs], [BAM_LIBS=$libs])
117  ])
118done
119AS_IF([test x"$BAM_LIBS" = x""], [
120  AC_MSG_RESULT([none required])
121],[
122  AC_MSG_RESULT([$BAM_LIBS])
123])
124AS_IF([test x"$BAM_LIBS" = xno], [$2], [$1])
125]) # YAT_LIB_BAM
126
127
128
129# YAT_LINK_BAM_IFELSE([lib], [action-if-found], [action-if-not-found])
130# ====================================================================
131# Add lib to LIBS and try to link some code using libbam
132AC_DEFUN([YAT_LINK_BAM_IFELSE],
133[
134save_LIBS=$LIBS
135LIBS="$1 $LIBS"
136AC_LINK_IFELSE([AC_LANG_PROGRAM([_YAT_BAM_INCLUDES],
137                                [
138                                 bam_header_t* hdr = bam_header_init();
139                                 bam_header_destroy(hdr);
140                                ])],
141               [$2], [$3])
142LIBS=$save_LIBS
143]) # YAT_LINK_BAM_IFELSE
144
145
146# YAT_BAM_NT16_REV_TABLE
147# ====================================================================
148# Check if global variable bam_nt16_rev_table is available in
149# -lbam. If found call AC_DEFINE(HAVE_BAM_NT16_REV_TABLE).
150AC_DEFUN([YAT_BAM_NT16_REV_TABLE],
151[
152AC_MSG_CHECKING([for bam_nt16_rev_table])
153AC_LINK_IFELSE([AC_LANG_PROGRAM([_YAT_BAM_INCLUDES],
154                  [char c = bam_nt16_rev_table@<:@7@:>@;])
155               ],[
156                 AC_MSG_RESULT([yes])
157                 AC_DEFINE([HAVE_BAM_NT16_REV_TABLE], [1],
158                           [Define to 1 if libbam contains bam_nt16_rev_table])
159               ],[
160                 AC_MSG_RESULT([no])
161               ])
162]) # YAT_BAM_NT16_REV_TABLE
163
164
165# YAT_FUNC_BAM_CALEND(SAM_FILE)
166# ===================
167# Check if there is a bam_calend that works
168AC_DEFUN([YAT_FUNC_BAM_CALEND],
169[
170  AC_CACHE_CHECK([for working bam_calend],
171    [yat_cv_func_bam_calend],
172    [AC_RUN_IFELSE(
173       [AC_LANG_PROGRAM(
174          [_YAT_BAM_INCLUDES],
175          [
176          samfile_t* samfile = samopen("$1", "r", NULL);
177          if (!samfile)
178            return 1;
179          bam1_t* read = bam_init1();
180          while (samread(samfile, read) >= -1) {
181            bool have_match = false;
182            if (read->core.n_cigar < 1)
183              continue;
184            uint32_t end = bam_calend(&read->core, bam1_cigar(read));
185            // replace BAM_CMATCH with BAM_CEQUAL in cigar
186            for (size_t k=0; k<read->core.n_cigar; ++k) {
187              uint32_t& element = bam1_cigar(read)@<:@k@:>@;
188              uint32_t op = element & BAM_CIGAR_MASK;
189              if (op == BAM_CMATCH) {
190                have_match = true;
191                uint32_t oplen = element >> BAM_CIGAR_SHIFT;
192                element = oplen<<BAM_CIGAR_SHIFT|BAM_CEQUAL;
193              }
194            }
195            // if no match element found, next read please
196            if (!have_match)
197              continue;
198            // bam_calend works on BAM_CEQUAL
199            if (bam_calend(&read->core, bam1_cigar(read)) == end)
200              return 0;
201            // bam_calend is not working as expected
202            return 2;
203          }
204          // end of data, fail
205          return 3;
206          ])
207       ],
208       [yat_cv_func_bam_calend=yes],
209       [yat_cv_func_bam_calend=no],
210       [yat_cv_func_bam_calend="guessing no"])
211    ])
212  AS_IF([test x"$yat_cv_func_bam_calend" = x"yes"],[
213    AC_DEFINE([HAVE_BAM_CALEND], [1], [Define to 1 if bam_calend is working])
214  ])
215])
216
217
218# _YAT_BAM_INCLUDES
219# =================
220# Private macro that expand PP magic we see in 'yat/omic/bam_config.h'
221AC_DEFUN([_YAT_BAM_INCLUDES],
222[
223@%:@if YAT_HAVE_HTS_H
224@%:@ include <sam.h>
225@%:@elif YAT_HAVE_HTSLIB_HTS_H
226@%:@ include <htslib/sam.h>
227@%:@elif YAT_HAVE_BAM_H
228@%:@ include <sam.h>
229@%:@ include <bam.h>
230@%:@elif YAT_HAVE_BAM_BAM_H
231@%:@ include <bam/bam.h>
232@%:@ include <bam/sam.h>
233@%:@elif YAT_HAVE_SAMTOOLS_BAM_H
234@%:@ include <samtools/bam.h>
235@%:@ include <samtools/sam.h>
236@%:@endif
237]) # _YAT_BAM_HEADER
Note: See TracBrowser for help on using the repository browser.