forked from AsadRizvi97/snp-identifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (53 loc) · 2.59 KB
/
Main.java
File metadata and controls
58 lines (53 loc) · 2.59 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
import java.util.Arrays;
import java.util.LinkedList;
import edu.princeton.cs.algs4.In;
public class Main {
private LinkedList<Chromosome> chromosomes; //Store chromosomes as linked list
private int numberOfSamples; //Store number of samples for print()
public Main(String[] dataFiles) {
LinkedList<String> identifiers = new LinkedList<>(); //Create linked list of identifiers to see if chromosome exists already
chromosomes = new LinkedList<>();
numberOfSamples = dataFiles.length; //Set number of samples to number of data files inputted
for (int i = 0; i < dataFiles.length; i++) { //For each data file, convert each line to Chromosome | Position | Reference | SNP
In dataInput = new In(dataFiles[i]);
while (!dataInput.isEmpty()) {
String[] split = dataInput.readLine().split("\\s+");
if (split[0].startsWith("#") || split[3].length() != 1 || split[3].equals(".") || split[4].length() != 1 || split[4].equals(".")) {
continue;
}
String[] line = {split[0], split[1], split[3], split[4]};
if (identifiers.contains(split[0])) { //If chromosome already exists, just update the old one already there
Chromosome chromosome = chromosomes.get(identifiers.indexOf(split[0]));
chromosome.update(line, i + 1);
} else { //If no chromosome exists, create it
Chromosome chromosome = new Chromosome(line, dataFiles.length, i + 1);
chromosomes.add(chromosome);
identifiers.add(split[0]);
}
}
}
}
public void print(int label) {
for (Chromosome chromosome : chromosomes) { //For each chromosome, print Position | Reference | Sample 1 | Sample 2 etc.
System.out.printf("%-11s%s\n", "Chromosome", chromosome.identifier());
System.out.printf("%-10s%-11s", "Position", "Reference");
for (int i = 0; i < numberOfSamples; i++) {
System.out.printf("%-7s%-3d", "Sample", i + 1);
}
System.out.println();
chromosome.print(label);
System.out.println();
}
}
public static void main(String[] args) {
int label = 0;
Main aligner;
if (args[0].equals("-label") || args[0].equals("-l")) {
label = 1;
aligner = new Main(Arrays.copyOfRange(args, 1, args.length));
} else {
aligner = new Main(args);
}
aligner.print(label);
}
}