File: Bref3.java

package info (click to toggle)
beagle 220722-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,644 kB
  • sloc: java: 17,045; sh: 55; makefile: 11
file content (168 lines) | stat: -rw-r--r-- 5,478 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2014-2021 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle 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.
 *
 * Beagle 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package bref;

import blbutil.Const;
import blbutil.FileIt;
import blbutil.InputIt;
import blbutil.SampleFileIt;
import blbutil.Utilities;
import java.io.File;
import vcf.RefGTRec;
import vcf.RefIt;
import vcf.Samples;

/**
 * <p>Class {@code Bref3} converts files in VCF format into 
 * bref version 3 format.
 * </p>
 * <p>Instances of class {@code Bref3} are not thread-safe.</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public class Bref3 {

    private static final String program = "bref3.22Jul22.46e.jar";

    /**
     * The {@code main()} method is the entry point to the bref program.
     * See the usage() method for usage instructions.
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length>2) {
            System.out.println(usage());
            System.exit(0);
        }
        if (args.length==1 && args[0].equalsIgnoreCase("help")) {
            System.out.println(usage());
            System.exit(0);
        }
        boolean useStdIn = useStdIn(args);
        int maxNSeq = -1;
        if (args.length==2 || (useStdIn && args.length==1)) {
            maxNSeq = maxNSeq(args[args.length-1]);
        }
        String inputFile = useStdIn ? null : args[0];
        writeBref(inputFile, maxNSeq);
    }

    private static boolean useStdIn(String[] sa) {
        if (sa.length==0) {
            return true;
        }
        else if (sa.length==1) {
            return (sa[0].endsWith(".vcf") || sa[0].endsWith(".vcf.gz"))==false;
        }
        else {
            return false;
        }
    }

    private static int maxNSeq(String arg) {
        int maxNSeq = -1;
        try {
            maxNSeq = Integer.parseInt(arg);
            if (maxNSeq < 1 || maxNSeq > Character.MAX_VALUE) {
                exit("Error: invalid <nSeq> " + arg);
            }
        }
        catch (NumberFormatException e) {
            exit("Error: <nSeq> is not a parsable integer: " + arg);
        }
        return maxNSeq;
    }

    private static void exit(String msg) {
        System.out.println(usage());
        Utilities.exit(msg);
    }

    private static void writeBref(String fileName, int maxNSeq) {
        try (SampleFileIt<RefGTRec> it = refIt(fileName);
                BrefWriter brefOut = brefOut(it.samples(), maxNSeq)) {
            while (it.hasNext()) {
                brefOut.write(it.next());
            }
        }
    }

    private static SampleFileIt<RefGTRec> refIt(String fileName) {
        FileIt<String> it = null;
        if (fileName==null) {
            it = InputIt.fromStdIn();
        }
        else if (fileName.endsWith(".gz")) {
            it = InputIt.fromGzipFile(new File(fileName));
        }
        else {
            it = InputIt.fromTextFile(new File(fileName));
        }
        return RefIt.create(it);
    }

    private static BrefWriter brefOut(Samples samples, int maxNSeq) {
        File outFile = null;    // write to standard output
        if (maxNSeq<0) {
            return new AsIsBref3Writer(program, samples, outFile);
        }
        else {
            return new CompressBref3Writer(program, samples, maxNSeq, outFile);
        }
    }

    private static String usage() {
        StringBuilder sb = new StringBuilder(500);
        sb.append("usage:");
        sb.append(Const.nl);
        sb.append("  java -jar ");
        sb.append(program);
        sb.append(" help");
        sb.append(Const.nl);
        sb.append(Const.nl);
        sb.append("  java -jar ");
        sb.append(program);
        sb.append(" [vcf] <nseq>  > [bref3]");
        sb.append(Const.nl);
        sb.append(Const.nl);
        sb.append("  cat   [vcf]   | java -jar ");
        sb.append(program);
        sb.append(" <nseq>  > [bref3]");
        sb.append(Const.nl);
        sb.append(Const.nl);
        sb.append("where");
        sb.append(Const.nl);
        sb.append("  [bref3]  = the output bref3 file");
        sb.append(Const.nl);        
        sb.append("  [vcf]    = A VCF file with phased, non-missing genotype data.  If the");
        sb.append(Const.nl);
        sb.append("             file is gzip-compressed, its filename must end in \".gz\"");
        sb.append(Const.nl);
        sb.append("             and \"cat\" must be replaced with \"zcat\"");
        sb.append(Const.nl);
        sb.append("  <nseq>   = optional argument for maximum number of unique sequences");
        sb.append(Const.nl);
        sb.append("             in a bref3 block. If there are N reference samples,");
        sb.append(Const.nl);
        sb.append("             the default value is: <max-seq>=2^(2*log10(N) + 1)");
        sb.append(Const.nl);
        return sb.toString();
    }
}