|
| 1 | +// elPrep: a high-performance tool for preparing SAM/BAM files. |
| 2 | +// Copyright (c) 2017-2019 imec vzw. |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version, and Additional Terms |
| 8 | +// (see below). |
| 9 | + |
| 10 | +// This program is distributed in the hope that it will be useful, but |
| 11 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// Affero General Public License for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU Affero General Public |
| 16 | +// License and Additional Terms along with this program. If not, see |
| 17 | +// <https://github.com/ExaScience/elprep/blob/master/LICENSE.txt>. |
| 18 | + |
| 19 | +package cmd |
| 20 | + |
| 21 | +import ( |
| 22 | + "bytes" |
| 23 | + "flag" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "runtime" |
| 29 | + |
| 30 | + "github.com/exascience/elprep/v4/filters" |
| 31 | +) |
| 32 | + |
| 33 | +// MergeOpticalDuplicatesMetricsHelp is the help string for this command. |
| 34 | +const MergeOpticalDuplicatesMetricsHelp = "\nmerge-optical-duplicates-metrics parameters:\n" + |
| 35 | + "elprep merge-optical-duplicates-metrics sam-input-file sam-output-file metrics-file /path/to/intermediate/metrics\n" + |
| 36 | + "[--remove-duplicates]\n" + |
| 37 | + "[--nr-of-threads nr]\n" + |
| 38 | + "[--timed]\n" + |
| 39 | + "[--log-path path]\n" |
| 40 | + |
| 41 | +// Merge implements the elprep merge command. |
| 42 | +func MergeOpticalDuplicatesMetrics() error { |
| 43 | + var ( |
| 44 | + profile, logPath string |
| 45 | + nrOfThreads int |
| 46 | + timed, removeDuplicates bool |
| 47 | + ) |
| 48 | + |
| 49 | + var flags flag.FlagSet |
| 50 | + |
| 51 | + flags.IntVar(&nrOfThreads, "nr-of-threads", 0, "number of worker threads") |
| 52 | + flags.BoolVar(&timed, "timed", false, "measure the runtime") |
| 53 | + flags.BoolVar(&removeDuplicates, "remove-duplicates", false, "use when duplicates were removed during duplicate marking") |
| 54 | + flags.StringVar(&profile, "profile", "", "write a runtime profile to the specified file(s)") |
| 55 | + flags.StringVar(&logPath, "log-path", "", "write log files to the specified directory") |
| 56 | + |
| 57 | + parseFlags(flags, 6, MergeOpticalDuplicatesMetricsHelp) |
| 58 | + |
| 59 | + input := getFilename(os.Args[2], MergeOpticalDuplicatesMetricsHelp) |
| 60 | + output := getFilename(os.Args[3], MergeOpticalDuplicatesMetricsHelp) |
| 61 | + metrics := getFilename(os.Args[4], MergeOpticalDuplicatesMetricsHelp) |
| 62 | + intermediateMetrics := getFilename(os.Args[5], MergeOpticalDuplicatesMetricsHelp) |
| 63 | + |
| 64 | + setLogOutput(logPath) |
| 65 | + |
| 66 | + // sanity checks |
| 67 | + |
| 68 | + var sanityChecksFailed bool |
| 69 | + |
| 70 | + if !checkExist("", input) { |
| 71 | + log.Println("Warning: Input file does not exist: ", input) |
| 72 | + } |
| 73 | + |
| 74 | + if !checkExist("", intermediateMetrics) { |
| 75 | + sanityChecksFailed = true |
| 76 | + } |
| 77 | + |
| 78 | + if profile != "" && !checkCreate("--profile", profile) { |
| 79 | + sanityChecksFailed = true |
| 80 | + } |
| 81 | + |
| 82 | + metricsDir, err := filepath.Abs(intermediateMetrics) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if nrOfThreads < 0 { |
| 88 | + sanityChecksFailed = true |
| 89 | + log.Println("Error: Invalid nr-of-threads: ", nrOfThreads) |
| 90 | + } |
| 91 | + |
| 92 | + if sanityChecksFailed { |
| 93 | + fmt.Fprint(os.Stderr, MergeOpticalDuplicatesMetricsHelp) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + // building output command line |
| 98 | + |
| 99 | + var command bytes.Buffer |
| 100 | + fmt.Fprint(&command, os.Args[0], " merge-optical-duplicates-metrics ", input, " ", output, " ", metrics, " ", intermediateMetrics) |
| 101 | + if nrOfThreads > 0 { |
| 102 | + runtime.GOMAXPROCS(nrOfThreads) |
| 103 | + fmt.Fprint(&command, " --nr-of-threads ", nrOfThreads) |
| 104 | + } |
| 105 | + if timed { |
| 106 | + fmt.Fprint(&command, " --timed ") |
| 107 | + } |
| 108 | + if logPath != "" { |
| 109 | + fmt.Fprint(&command, " --log-path ", logPath) |
| 110 | + } |
| 111 | + if removeDuplicates { |
| 112 | + fmt.Fprint(&command, " --remove-duplicates") |
| 113 | + } |
| 114 | + |
| 115 | + // executing command |
| 116 | + |
| 117 | + log.Println("Executing command:\n", command.String()) |
| 118 | + |
| 119 | + var ctr filters.DuplicatesCtrMap |
| 120 | + |
| 121 | + // merge intermediate metrics files |
| 122 | + err = timedRun(timed, profile, "Loading and combining duplicate metrics.", 1, func() error { |
| 123 | + ctr = filters.LoadAndCombineDuplicateMetrics(metricsDir) |
| 124 | + return ctr.Err() |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + return timedRun(timed, profile, "Printing comdined duplicate metrics.", 2, func() error { |
| 130 | + return filters.PrintDuplicatesMetrics(input, output, metrics, removeDuplicates, ctr) |
| 131 | + }) |
| 132 | +} |
0 commit comments