-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastaCounter.java
More file actions
130 lines (115 loc) · 4.68 KB
/
FastaCounter.java
File metadata and controls
130 lines (115 loc) · 4.68 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
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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
public class FastaCounter {
public static void main(String[] args) {
List<String> files = new ArrayList<>();
int desiredCores = -1;
// Parse command-line arguments
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-i") || args[i].equals("--input")) {
// Extract input files
for (int j = i + 1; j < args.length; j++) {
if (args[j].startsWith("-")) {
break;
}
files.add(args[j]);
}
} else if (args[i].equals("-n") || args[i].equals("--cores")) {
// Extract number of cores
if (i + 1 < args.length) {
try {
desiredCores = Integer.parseInt(args[i + 1]);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number of cores.");
return;
}
}
}
}
// Check if required arguments are provided
if (files.isEmpty() || desiredCores == -1) {
System.out.println("Usage: java LineCounter -i <file1> <file2> ... -n <cores>");
return;
}
// Get the number of available cores
int availableCores = Runtime.getRuntime().availableProcessors();
// Validate the desired number of cores
if (desiredCores <= 0 || desiredCores > availableCores) {
System.out.println("Error: Invalid number of cores. Available cores: " + availableCores);
return;
}
// Create a thread pool with the desired number of cores
ExecutorService executor = Executors.newFixedThreadPool(desiredCores);
// Create a list to hold the Future objects
List<Future<StringBuilder>> futures = new ArrayList<>();
// Submit tasks to the thread pool
for (String file : files) {
Future<StringBuilder> future = executor.submit(() -> countLines(file));
futures.add(future);
}
// Print the line count for each file
for (int i = 0; i < files.size(); i++) {
String file = files.get(i);
StringBuilder result = null;
try {
result = futures.get(i).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
System.out.println("Output for " + file + ":\n" + result);
}
// Shutdown the thread pool
executor.shutdown();
}
private static StringBuilder countLines(String file) {
int lineCount = 0;
StringBuilder output = new StringBuilder();
try {
BufferedReader reader;
if (file.endsWith(".gz")) {
// Handle gzipped file
InputStream fileStream = new FileInputStream(file);
InputStream gzipStream = new GZIPInputStream(fileStream);
reader = new BufferedReader(new InputStreamReader(gzipStream, StandardCharsets.UTF_8));
} else {
// Handle regular text file
reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
}
String line;
StringBuilder sequence = new StringBuilder();
String sequenceId = "";
while ((line = reader.readLine()) != null) {
if (line.startsWith(">")) {
if (sequence.length() > 0 ) {
output.append(file).append(" ").append(sequenceId).append(" ").append(lineCount).append("\n");
sequence.setLength(0);
lineCount = 0;
}
sequenceId = line.substring(1);
} else {
sequence.append(line);
lineCount += line.length();
}
}
// Add the length of the last sequence if present
if (sequence.length() > 0) {
output.append(file).append(" ").append(sequenceId).append(" ").append(lineCount).append("\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
}