;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015-2024 Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de>
;;; Copyright © 2017 CM Massimo <carlomaria.massimo@mdc-berlin.de>
;;; Copyright © 2018, 2019, 2021 Marcel Schilling <marcel.schilling@uni-luebeck.de>
;;; Copyright © 2023 Mădălin Ionel Patrașcu <madalinionel.patrascu@mdc-berlin.de>
;;;
;;; This file is NOT part of GNU Guix, but is supposed to be used with GNU
;;; Guix and thus has the same license.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (bimsb packages bioinformatics-nonfree)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module ((guix licenses-nonfree) #:prefix nonfree:)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix hg-download)
  #:use-module (guix git-download)
  #:use-module (guix gexp)
  #:use-module (guix utils)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system copy)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system python)
  #:use-module (guix build-system perl)
  #:use-module (guix build-system r)
  #:use-module (bimsb packages staging)
  #:use-module (bimsb packages tainted)
  #:use-module (bimsb packages variants)
  #:use-module (gnu packages)
  #:use-module (gnu packages admin)
  #:use-module (gnu packages algebra)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages base)
  #:use-module (gnu packages bioconductor)
  #:use-module (gnu packages bioinformatics)
  #:use-module (gnu packages boost)
  #:use-module (gnu packages check)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages cran)
  #:use-module (gnu packages documentation)
  #:use-module (gnu packages file)
  #:use-module (gnu packages gcc)
  #:use-module (gnu packages ghostscript)
  #:use-module (gnu packages graph)
  #:use-module (gnu packages image)
  #:use-module (gnu packages machine-learning)
  #:use-module (gnu packages maths)
  #:use-module (gnu packages mpi)
  #:use-module (gnu packages ncurses)
  #:use-module (gnu packages perl)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages python-science)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages ruby)
  #:use-module (gnu packages statistics)
  #:use-module (gnu packages swig)
  #:use-module (gnu packages tbb)
  #:use-module (gnu packages web)
  #:use-module (gnu packages xml)
  #:use-module (guix-science-nonfree packages bioinformatics)
  #:use-module (past packages boost)
  #:use-module (past packages maths)
  #:use-module (past packages perl)
  #:use-module (past packages python27))

(define-public dinup
  (package
    (name "dinup")
    (version "1.3")
    (source (origin
              (method url-fetch)
              (uri (string-append
                    "http://zhanglab.tongji.edu.cn/softwares/DiNuP/dinup_"
                    version ".tar.gz"))
              (sha256
               (base32
                "14s15h8kk9vqqwy0hykdzffz6zlkbqpvg5wnnfiwd2x7cwxizikm"))))
    (build-system python-build-system)
    (arguments
     `(#:tests? #f ; no "test" target
       #:python ,python-2
       #:phases
       (modify-phases %standard-phases
         (replace 'unpack
           ;; The release tarball contains loose files.
           (lambda* (#:key source #:allow-other-keys)
             (mkdir "dinup")
             (invoke "tar" "-C" "dinup" "-xvf" source)
             (chdir "dinup"))))))
    (native-inputs
     (list python2-setuptools))
    (home-page "http://zhanglab.tongji.edu.cn/softwares/DiNuP/")
    (synopsis "Identify regions of differential nucleosome positioning")
    (description
     "DiNuP compares the nucleosome profiles generated by high-throughput
sequencing between different conditions.  DiNuP provides a statistical p-value
for each identified region of differential nucleosome positioning (RDNP) based
on the difference of read distributions.  It also empirically estimates the
false discovery rate as a cutoff when two samples have different sequencing
depths and differentiate reliable RDNPs from the background noise.")
    (license nonfree:artistic1.0)))

(define-public fstitch
  (let ((commit "7c65fd973f1d04d83cd48dd5561c4e40c14dd8c6")
        (revision "1"))
    (package
      (name "fstitch")
      (version (string-append "0-" revision "." (string-take commit 9)))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/azofeifa/FStitch.git")
                      (commit commit)))
                (file-name (string-append name "-" version))
                (sha256
                 (base32
                  "0g00hdc73w68big3prym0llx0nl7w7xhbfp6g405yfn7dghc8v8c"))
                (modules '((guix build utils)))
                (snippet
                 '(begin
                    ;; Delete pre-built binaries
                    (delete-file "FastReadStitcher/src/FStitch")
                    (for-each delete-file
                              (find-files "FastReadStitcher/src/" "\\.o$"))))))
      (build-system gnu-build-system)
      (arguments
       (list
        #:tests? #f                     ; no tests included
        #:phases
        #~(modify-phases %standard-phases
            (add-after 'unpack 'enter-dir
              (lambda _
                (chdir "FastReadStitcher/src/")
                (substitute* "Makefile"
                  (("\\$\\{PWD\\}/") ""))))
            (delete 'configure)
            (replace 'install
              (lambda _
                (let ((bin (string-append #$output "/bin")))
                  (mkdir-p bin)
                  (install-file "FStitch" bin)))))))
      (home-page "https://github.com/azofeifa/FStitch/")
      (synopsis "Detect nascent RNA transcription in GRO-seq and ChIP-seq")
      (description
       "FStitch was written primarily for scientists looking to
identify putative nascent transcripts de novo in Global Run-On
sequencing data.  However, users may also find this package useful as
a ChIP-seq peak caller.")
      (license nonfree:undeclared))))

(define-public structure
  (package
    (name "structure")
    (version "2.3.4")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "http://pritchardlab.stanford.edu/"
                           "structure_software/release_versions/v" version
                           "/structure_kernel_source.tar.gz"))
       (sha256
        (base32
         "0dxvq34lyzicjwgsyrw49b1pmjms7nmc3g8vj8zga555i68jpdzj"))))
    (build-system gnu-build-system)
    (arguments
     (list
      #:tests? #f                       ; There are no tests.
      #:make-flags '(list "CFLAGS=-fcommon")
      #:phases
      #~(modify-phases %standard-phases
          (delete 'configure)           ; There is no configure phase.
          (replace 'install
            (lambda _
              (let ((bin (string-append #$output "/bin")))
                (mkdir-p bin)
                (install-file "structure" bin)))))))
    (home-page "http://pritchardlab.stanford.edu/structure.html")
    (synopsis "Tool for investigating population structure")
    (description "Structure is a package for using multi-locus genotype data
to investigate population structure.  Its uses include inferring the presence
of distinct populations, assigning individuals to populations, studying hybrid
zones, identifying migrants and admixed individuals, and estimating population
allele frequencies in situations where many individuals are migrants or
admixed.  It can be applied to most of the commonly-used genetic markers,
including SNPS, microsatellites, RFLPs and AFLPs.")
    ;; I have asked upstream for information about the license:
    ;; https://groups.google.com/forum/#!topic/structure-software/1g7bDoN9140
    (license nonfree:undeclared)))

(define-public nofold
  (let ((revision "1")
        (commit "a3da753118db8310d453669aa01d34a270532a4b"))
    (package
      (name "nofold")
      (version (string-append "0.0.0-"
                              revision "." (string-take commit 9)))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/sarahmid/nofold.git")
                      (commit commit)))
                (file-name (string-append name "-" version))
                (sha256
                 (base32
                  "0fq33ra4nrnyjvwd4vc9r2mxrdihkb5imwms7b2kl6dr76vfmy1z"))))
      (build-system gnu-build-system)
      (arguments
       (list
        #:tests? #f
        #:phases
        #~(modify-phases %standard-phases
            (delete 'configure)
            (delete 'build)
            (replace 'install
              (lambda _
                (let ((target (string-append #$output "/share/nofold")))
                  (copy-recursively "." target)))))))
      (inputs
       (list python-2
             locarna
             infernal-1.0
             r
             r-fastcluster))
      (synopsis "Motif finder for RNA secondary structures")
      (description
       "NoFold is an approach for characterizing and clustering RNA
secondary structures without computational folding or alignment.  It
works by mapping each RNA sequence of interest to a structural feature
space, where each coordinate within the space corresponds to the
probabilistic similarity of the sequence to an empirically defined
structure model (e.g. Rfam family covariance models).  NoFold provides
scripts for mapping sequences to this structure space, extracting any
robust clusters that are formed, and annotating those clusters with
structural and functional information.")
      (home-page "https://github.com/sarahmid/nofold")
      (license (nonfree:non-free "https://raw.githubusercontent.com/sarahmid/nofold/master/LICENSE"
                                 "license forbids commercial usage")))))

(define-public defuse-tools
  (package
    (name "defuse-tools")
    (version "0.8.0")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "https://bitbucket.org/dranew/defuse/"
                           "get/v" version ".tar.bz2"))
       (sha256
        (base32
         "0my24iw5mqdrq1k08casdv5p4wvk01inikhvi8mb74r7z93kcpf5"))
       (modules '((guix build utils)))
       ;; Delete bundled stuff
       (snippet '(delete-file-recursively "external"))))
    (build-system gnu-build-system)
    (arguments
     (list
      #:tests? #f                       ; no check target
      #:make-flags
      #~(list "ZLIB=-lz"
              (string-append "BAMTOOLSSRC="
                             #$(this-package-input "bamtools")
                             "/lib/libbamtools.so")
              (string-append "BAMTOOLSDIR="
                             #$(this-package-input "bamtools")
                             "/include/bamtools"))
      #:phases
      #~(modify-phases %standard-phases
          (delete 'configure)
          (add-after 'unpack 'chdir
            (lambda _ (chdir "tools")))
          (add-after 'chdir 'do-not-use-bundled-libraries
            (lambda* (#:key inputs #:allow-other-keys)
              (substitute* "makefile"
                (("\\$\\(BAMDIR\\)/\\$\\(BAMLIB\\)")
                 (string-append (assoc-ref inputs "samtools")
                                "/lib/libbam.a"))
                (("\\$\\(ZDIR\\)/\\$\\(ZLIB\\)") "$(ZLIB)")
                ;; Don't try to build the bundled libs
                (("\\$\\(MAKE\\).*") ""))
              (substitute* '("FastaIndex.cpp"
                             "FastaIndex.h")
                (("#include \"faidx.h\"")
                 "#include <samtools/faidx.h>"))
              (substitute* "bamfastq.cpp"
                (("#include \"BamReader.h\"")
                 "#include <bamtools/api/BamReader.h>"))))
          (replace 'install
            (lambda _
              (let ((bin (string-append #$output "/bin")))
                (for-each
                 (lambda (file) (install-file file bin))
                 '("clustermatepairs"
                   "setcover"
                   "calccov"
                   "estislands"
                   "dosplitalign"
                   "evalsplitalign"
                   "localalign"
                   "splitseq"
                   "matealign"
                   "bamfastq"))))))))
    (inputs
     (list bamtools
           boost-1.68
           samtools-0.1
           zlib))
    (native-inputs (list gcc-8))
    (home-page "https://bitbucket.org/dranew/defuse")
    (synopsis "Gene fusion discovery using RNA-Seq data")
    (description "deFuse is a software package for gene fusion
discovery using RNA-Seq data.  The software uses clusters of
discordant paired end alignments to inform a split read alignment
analysis for finding fusion boundaries.  The software also employs a
number of heuristic filters in an attempt to reduce the number of
false positives and produces a fully annotated output for each
predicted fusion.

This package provides only the binaries from the \"tools\" directory,
not the pipeline scripts.")
    (license (nonfree:non-free "file://LICENSE.md"))))

(define-public gmap-gsnap
  (package
    (name "gmap-gsnap")
    (version "2017-08-15")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://research-pub.gene.com/gmap/src/gmap-gsnap-"
                                  version ".tar.gz"))
              (sha256
               (base32
                "0bqvv26g4ic3nmrcdnnh7kqly86ly9gym75nw1wyzav4ad5h8rqv"))))
    (build-system gnu-build-system)
    ;; FIXME: there are test failures, but I'm not sure why.
    (arguments
     '(#:tests? #f
       #:phases
       (modify-phases %standard-phases
         ;; Because of multiple definitions.
         (add-after 'unpack 'set-fcommon
           (lambda _ (setenv "CFLAGS" "-fcommon"))))))
    (inputs (list perl))
    (home-page "http://research-pub.gene.com/gmap/")
    (synopsis "Genomic mapper and aligner")
    (description "This package provides GMAP and GSNAP.  The former is
a genomic mapping and alignment program for mRNA and EST sequences;
the latter is an aligner for genomic short-read nucleotide sequences.")
    (license (nonfree:non-free "Distribution of modified versions
requires the author's consent."))))

(define-public perl-math-cdf
  (package
    (name "perl-math-cdf")
    (version "0.1")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "mirror://cpan/authors/id/C/CA/CALLAHAN/Math-CDF-"
                           version ".tar.gz"))
       (sha256
        (base32
         "0ram2brgxlyqszf25s22vram8v2pvkwqrjqkr3f4gkim10jvz5kq"))))
    (build-system perl-build-system)
    (home-page "http://search.cpan.org/dist/Math-CDF/")
    (synopsis "Generate probabilities and quantiles from several statistical probability functions")
    (description "This module provides a perl interface to the
DCDFLIB.  Functions are available for 7 continuous
distributions (Beta, Chi-square, F, Gamma, Normal, Poisson and
T-distribution) and for two discrete distributions (Binomial and
Negative Binomial).  Optional non-centrality parameters are available
for the Chi-square, F and T-distributions.  Cumulative probabilities
are available for all 9 distributions and quantile functions are
available for the 7 continuous distributions.")
    ;; Parts of the original library are public domain.  Others are
    ;; non-commercial only.  It is unclear which is which.
    (license nonfree:undeclared)))

(define-public cermit
  (package
    (name "cermit")
    (version "1.11.1")
    (source (origin
              (method url-fetch)
              (uri (string-append "file:///gnu/remote/cermit-" version ".tar.gz"))
              (sha256
               (base32
                "18p06n4ziykiznbk4b1fzjyg2cm43nqr82g6q6p14jym6lywgi5d"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f
       #:make-flags '("cERMIT")
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'chdir
           (lambda _ (chdir "source/cERMIT") #t))
         (add-after 'chdir 'patch-references
           (lambda* (#:key outputs #:allow-other-keys)
             (substitute* "motif_finder.c"
               (("command, \"mv")
                (string-append "command, \"" (which "mv"))))
             (substitute* "PSSM.c"
               (("command, \"R")
                (string-append "command, \"" (which "R")))
               (("./generate_logo.R")
                (string-append (assoc-ref outputs "out")
                               "/share/cermit/generate_logo.R")))
             #t))
         (add-after 'chdir 'fix-makefile
           (lambda _
             (substitute* "makefile"
               (("-m32") "")
               (("../libs/libgsl_1.14.a ../libs/libgslcblas_1.14.a")
                "-lgsl -lgslcblas"))
             (mkdir "bin")
             #t))
         (delete 'configure)
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out   (assoc-ref outputs "out"))
                    (bin   (string-append out "/bin"))
                    (share (string-append out "/share/cermit")))
               (mkdir-p bin)
               (install-file "bin/cERMIT" bin)

               (mkdir-p share)
               (install-file "../../generate_logo.R" share)
               #t))))))
    (inputs
     `(("gsl" ,gsl-1.16)
       ("r" ,r-minimal)))
    (supported-systems '("i686-linux"))
    (home-page "TODO")
    (synopsis "TODO")
    (description "TODO")
    ;; It should be GPLv3+ because it uses GSL and includes other code
    ;; under the GPLv3+.
    (license nonfree:undeclared)))

;; This perl script has no license declared but contains an 'All Rights
;; Reserved' Copyright statement.
(define-public targetscan
  (package
    (name "targetscan")
    (version "7.0")
    (source (origin
              (method url-fetch/zipbomb)
              (uri (string-append
                     "http://www.targetscan.org/vert_72"
                     "/vert_72_data_download/targetscan_70.zip"))
              (sha256
               (base32
                "0jfpkc0rbz600l2h56rmnnz6phcvjak37zl3yzn5hahgj8mcf8vf"))))
    (build-system copy-build-system)
    (arguments
     '(#:install-plan
       '(("targetscan_70.pl" "bin/")
         ("README_70.txt" "doc/targetscan_70/")
         ("targetscan_70_output.txt" "share/targetscan_70/")
         ("UTR_Sequences_sample.txt" "share/targetscan_70/")
         ("miR_Family_info_sample.txt" "share/targetscan_70/"))))
    (home-page "http://www.targetscan.org")
    (native-inputs (list unzip))
    (inputs (list perl))
    (synopsis "This program predicts miRNA targets using the TargetScanS
algorithm.")
    (description "It produces output as displayed in TargetScan.")
    (license (nonfree:non-free "All Rights Reserved"))))
