-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectItem.java
111 lines (88 loc) · 2.72 KB
/
SelectItem.java
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
package fionaApp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class SelectItem {
/**
* 这个方法用作从文件中读取行然后将行中字符串元素放入一个字符串List
* @param filepath输入文件路径
* @return 字符串LIst
* @throws IOException
*/
public ArrayList<String> getStringList(String filepath) throws IOException {
FileReader file = new FileReader(filepath);
BufferedReader filein = new BufferedReader(file);
String line = null;
ArrayList<String> lineList = new ArrayList<String>();
while ((line = filein.readLine()) != null) {
lineList.add(line);
}
filein.close();
return lineList;
}
/**
*
* @param list1
* @param list2
*/
public ArrayList<String> getOverlapList(ArrayList<String> list1, ArrayList<String> list2) {
ArrayList<String> overlapList = new ArrayList<String>();
for (int i = 0; i <list1.size(); i++) {
for (int j = 0; j < list2.size(); j++) {
if (list2.get(j).contains(list1.get(i)) ) {
overlapList.add(list2.get(j));
break;
}
}
}
return overlapList;
}
/**
*
* @param listStrings
* @param filepath
* @return
* @throws IOException
*/
public String getListFile(String[] listStrings, String filepath) throws IOException {
File newfile = new File(filepath);
if (!newfile.exists()) {
newfile.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(newfile));
for (int i = 0; i <listStrings.length; i++) {
bw.write(listStrings[i]);
bw.newLine();
}
bw.flush();
bw.close();
String resultFile =newfile.getAbsolutePath();
return resultFile;
}
/**
* 主方法
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
SelectItem obj = new SelectItem();
ArrayList<String>zp1List = obj.getStringList("D:/test/tdr/tdrCandidateGenefromArray.txt");
ArrayList<String>zp2List = obj.getStringList("D:/test/tdr/tdr123OverlapFile.txt");
//ArrayList<String>zp3List = obj.getStringList("D:/test/tdrZp3geneID.txt");
System.out.println("now it begins to overlap!");
ArrayList<String> overlapList12 = obj.getOverlapList(zp1List, zp2List);
int length = overlapList12.size();
String[] overlapArray12 =new String[length];
if (overlapArray12 !=null) {
for (int i = 0; i < overlapArray12.length; i++) {
overlapArray12[i] = overlapList12.get(i);
}
}
String overlapFile12filepath = obj.getListFile(overlapArray12, "D:/test/tdr/tdr123overlap_microarrayOverlapFile.txt");
System.out.println("you got the file in the path: " + overlapFile12filepath);
}
}