-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVCFsubsetter.java
More file actions
79 lines (71 loc) · 2.92 KB
/
Copy pathVCFsubsetter.java
File metadata and controls
79 lines (71 loc) · 2.92 KB
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
/**
* Using this code you would be able to subset the huge VCF file into the certain subsets...Hope it helps...
* @author Milad
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class VCFsubsetter {
public static void main(String[] args) {
String inputFilePath = "G:\\Eidi\\ExAC.r1.sites.vep.vcf"; // Path to your input VCF file
int variantsPerSubset = 700000; // Number of variants per subset VCF file
try {
subsetVCF(inputFilePath, variantsPerSubset);
System.out.println("VCF file subsetted successfully.");
} catch (IOException e) {
System.err.println("Error processing VCF file: " + e.getMessage());
}
}
public static void subsetVCF(String inputFilePath, int variantsPerSubset) throws IOException {
List<String> headerLines = new ArrayList<>();
// Read the header lines
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) { // Header line
headerLines.add(line);
} else {
break; // Stop reading after reaching the end of header
}
}
}
// Create subset VCF files
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
String outputFilePath = null;
BufferedWriter writer = null;
int variantsWritten = 0;
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
// Skip header lines as they are already saved
continue;
}
if (variantsWritten % variantsPerSubset == 0) {
// Close previous writer and open new file for next subset
if (writer != null) {
writer.close();
}
outputFilePath = "G:\\Eidi\\subset_" + ((variantsWritten / variantsPerSubset) + 1) + ".vcf";
writer = new BufferedWriter(new FileWriter(outputFilePath));
// Write header lines
for (String headerLine : headerLines) {
writer.write(headerLine);
writer.newLine();
}
}
// Write variant line
writer.write(line);
writer.newLine();
variantsWritten++;
}
// Close the last writer
if (writer != null) {
writer.close();
}
}
}
}