-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnest.java
178 lines (111 loc) · 3.92 KB
/
nest.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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.PrintWriter;
public class nest {
public static void main(String[] args) throws FileNotFoundException{
File RSV1 = new File(args[0]); // read the RSV1 alignment file
Scanner scnr1 = new Scanner(RSV1);
List<String> seq1 = new ArrayList<String>();
List<String> taxa1 = new ArrayList<String>();
while(scnr1.hasNextLine()) {
String line = scnr1.nextLine().trim();
if (line.charAt(0)=='>') {
taxa1.add(line);
}
else {
seq1.add(line);
}
}
int seq_size = seq1.size()/taxa1.size(); // This is alignment file, we should have the same number for single sequence
String[] sequence1 = new String[taxa1.size()];
for(int i=0;i<sequence1.length;i++) {
StringBuilder sb= new StringBuilder();
int n= seq_size*i;
for(int j=0; j<seq_size;j++) {
sb.append(seq1.get(n+j));
}
String sequence= sb.toString().toUpperCase();
sequence1[i]= sequence;
}
// get the RSV1 gene start and end region using references sequences
//the 2nd sequence is the wo-SHGF reference and the 3rd is the WGS reference
int start1=0;
int end1=0;
for (int i=0;i< sequence1[1].length();i++) {
if (sequence1[1].charAt(i) !=sequence1[2].charAt(i) && sequence1[1].charAt(i)=='-') {
start1=i;
break;
}
}
for (int i= sequence1[1].length()-1; i>0; i--) {
if (sequence1[1].charAt(i) !=sequence1[2].charAt(i) && sequence1[1].charAt(i)=='-') {
end1=i;
break;
}
}
//System.out.println(start1 + "\t" + end1);
// cut the first sequence ( assembly consensus) from the alignment file
String RSV1bf= sequence1[0].substring(0,start1);
String RSV1af= sequence1[0].substring(end1+1,sequence1[1].length()-1);
// RSV-SHGF region processed, inter-genetic overlap is going to be removed
File RSV2 = new File(args[1]); // read the RSV1 alignment file
Scanner scnr2 = new Scanner(RSV2);
List<String> seq2 = new ArrayList<String>();
List<String> taxa2 = new ArrayList<String>();
while(scnr2.hasNextLine()) {
String line = scnr2.nextLine().trim();
if (line.charAt(0)=='>') {
taxa2.add(line);
}
else {
seq2.add(line);
}
}
int seq_size2 = seq2.size()/taxa2.size(); // This is alignment file, we should have the same number for single sequence
String[] sequence2 = new String[taxa2.size()];
for(int i=0;i<sequence2.length;i++) {
StringBuilder sb= new StringBuilder();
int n= seq_size2*i;
for(int j=0; j<seq_size2;j++) {
sb.append(seq2.get(n+j));
}
String sequence= sb.toString().toUpperCase();
sequence2[i]= sequence;
}
int start2=0;
int end2=0;
for (int i=0;i< sequence2[1].length();i++) {
if (sequence2[1].charAt(i) !='-') {
start2=i;
break;
}
}
for (int i= sequence2[1].length()-1; i>0; i--) {
if (sequence2[1].charAt(i) != '-') {
end2=i;
break;
}
}
//System.out.println(start2 + "\t" + end2);
String SHGF = sequence2[0].substring(start2,end2+1);
//System.out.println(SHGF);
// combine substring together
String WGS= RSV1bf+ SHGF + RSV1af;
// remove the gaps
String WGSn= WGS.replace("-", "");
String taxa= taxa1.get(0).replace("_RSV1", "");
String id = taxa.replace(">", "");
String gp = taxa2.get(2).replace(">", "");
int len = WGSn.length();
PrintWriter outFile = new PrintWriter(id +".fasta");
outFile.println(taxa);
outFile.println(WGSn);
System.out.println(id + "\t" + gp +"\t" + len );
scnr1.close();
scnr2.close();
outFile.close();
}
}