-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseSTR.java
More file actions
243 lines (197 loc) · 9.52 KB
/
ParseSTR.java
File metadata and controls
243 lines (197 loc) · 9.52 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import java.util.*;
import java.io.*;
import java.io.Console;
/**
* Converts a .csv file of haplotypes downloaded
* from Family Tree DNA project pages to a format
* that can be used for data analysis by doing the
* following:
* - asks the user which .csv in the current
* directory that they'd like to convert
* - processes the file to split separate
* multi-site DYS markers into discrete values
* - saves the group assignment for each record in a
* new column called "assignment" and then deletes
* the group header rows
* - outputs the the converted data to file named by
* appending "_converted" to the original file name
* like so:
* z251_raw_data.csv --> z251_raw_data_converted.csv
*
*
* @author Cai Stuart-Maver
* @version 2015-12-09
**/
public class ParseSNP
{
/**
* Reads from a user-provided .csv file, converts the data
* to a format suitale for data analysis, and writes the new
* data to a new .csv file
*
* @param args not used
*/
public static void main(String[] args)
{
System.out.println(" _____ _____ _ _ _____ \n" +
" | __ \\ / ____| \\ | | __ \\ \n" +
" | |__) |_ _ _ __ ___ ___| (___ | \\| | |__) |\n" +
" | ___/ _` | '__/ __|/ _ \\\\___ \\| . ` | ___/ \n" +
" | | | (_| | | \\__ \\ __/____) | |\\ | | \n" +
" |_| \\__,_|_| |___/\\___|_____/|_| \\_|_| \n");
System.out.println("0=============================================0\n" +
"| Welcome to the PareseSNP csv parsing tool |\n" +
"| |\n" +
"| Ver: 0.7 Author: Cai Stuart-Maver |\n" +
"| [email protected] |\n" +
"0=============================================0\n");
System.out.println("\n\n" +
"This application converts a .csv containing STR data\n" +
"and SNP group assignments downloaded from Family Tree\n" +
"DNA to a format suitable for data analysis.\n\n");
File csvToParse = chooseCSV();
try
{
Scanner inFile = new Scanner(csvToParse);
String inFileName = csvToParse.getName();
String outFileName = inFileName.substring(0, inFileName.indexOf('.'));
outFileName = outFileName + "_converted.csv";
PrintWriter outFile = new PrintWriter(outFileName);
int lineNum = 0;
String outputStr;
String groupHeader = "";
String[] splitStr;
while(inFile.hasNext())
{
// Treat the first line differently, it's the header row
if (lineNum == 0)
{
outputStr = inFile.nextLine();
//add the new assignment column
outputStr = "Assignment, " + outputStr;
//split multi-site marker headers into unique columns
outputStr = outputStr.replaceFirst("DYS385", "DYS385a, DYS385b");
outputStr = outputStr.replace("DYS459", "DYS459a, DYS459b");
outputStr = outputStr.replace("DYS464", "DYS464a, DYS464b, DYS464c, DYS464d");
outputStr = outputStr.replace("YCAII", "YCAIIa, YCAIIb");
outputStr = outputStr.replace("CDY", "CDYa, CDYb");
outputStr = outputStr.replace("DYF395S1", "DYF395S1a, DYF395S1b");
outputStr = outputStr.replace("DYS413", "DYS413a, DYS413b");
//write header row to the file
outFile.println(outputStr);
}
// the rest of the lines are group headers and records
else
{
outputStr = inFile.nextLine();
/*
* if this line contains quotes, it may have commas
* embedded in the quotes which will throw off the
* split(",") method below, so replace them with ";"
*/
if(outputStr.contains("\""))
{
String tempStr = outputStr.substring(outputStr.indexOf('"'),
outputStr.lastIndexOf('"'));
String replacementStr = tempStr.replace(",", ";");
outputStr = outputStr.replace(tempStr, replacementStr);
// uncomment the lines below for debugging
/*
System.out.println(tempStr);
System.out.println(replacementStr);
System.out.println(outputStr);
*/
}
splitStr = outputStr.split(",");
//split the multiple DYS values into single data elements
outputStr = "";
if(splitStr.length > 1) //group header rows have a length of one, skip those
{
outputStr = groupHeader + ",";
for(int i = 0; i < splitStr.length; i++)
{
if(splitStr[i].indexOf("-") != -1 && i > 4) //only look for '-' in DYS cells
{
/* for DYS464, most kits have 4 2-digit values
* searated by "-", so 11 chars. A very few kits
* have 6 values, and in those cases, we discard
* the 5th and 6th values
*/
if(splitStr[i].length() > 11)
{
splitStr[i] = splitStr[i].substring(0,12);
}
splitStr[i] = splitStr[i].replaceAll("-", ",");
}
outputStr = outputStr + splitStr[i] + ",";
}
outFile.println(outputStr);
}
else
{
/* when a group header is found, the next lines will
* have the group header saved to their 'assignment'
* column. Group headers are not otherwise written to
* the file
*/
groupHeader = splitStr[0];
}
}
lineNum++;
}
inFile.close();
outFile.close();
System.out.println("\n\n" + inFileName + " has been parsed;\n" +
"the converted file name is " + outFileName +
"\nGoodbye!");
}
catch (Exception e)
{
}
} // end main method
/**
* searches the current working directory for .csv files,
* saves them to an array list, then asks the user to select
* on of the files to convert
*
* @returns File the .csv in the current directory chosen by the user
*/
private static File chooseCSV()
{
String path = System.getProperty("user.dir");
ArrayList<File> csvFileList = new ArrayList<File>();
int csvListIncrementor = 0;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
System.out.println("Here are the CSV files in the current directory: ");
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
String fileName = listOfFiles[i].getName();
if (fileName.endsWith(".csv"))
{
csvFileList.add(new File(fileName));
System.out.println("File [" + csvListIncrementor + "]: " + listOfFiles[i].getName());
csvListIncrementor++;
}
}
}
System.out.print("Please enter the number of the file you'd like to parse (or -1 to quit): ");
Scanner scanIn = new Scanner(System.in);
int choice = Integer.parseInt(scanIn.nextLine());
if(choice == -1)
{
System.out.println("Quit command received; exiting program.");
System.exit(0);
}
else if(choice < 0 || choice > (csvFileList.size()-1))
{
System.out.println("Invalid option. Pregram will terminate.");
System.exit(1);
}
// comment out line below for debugging
// System.out.println(choice);
return csvFileList.get(choice);
}
} // end class ParseSNP