QPromaQ commited on
Commit
309abfe
·
verified ·
1 Parent(s): 90b6fa1

Upload 2 files

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. ftp_urls.txt +3 -0
  3. full.sh +254 -0
.gitattributes CHANGED
@@ -62,3 +62,4 @@ predictions_truth/all_models_predictions_long.csv filter=lfs diff=lfs merge=lfs
62
  Supp1.csv filter=lfs diff=lfs merge=lfs -text
63
  Supp2.xlsx filter=lfs diff=lfs merge=lfs -text
64
  tRNA_intership.html filter=lfs diff=lfs merge=lfs -text
 
 
62
  Supp1.csv filter=lfs diff=lfs merge=lfs -text
63
  Supp2.xlsx filter=lfs diff=lfs merge=lfs -text
64
  tRNA_intership.html filter=lfs diff=lfs merge=lfs -text
65
+ ftp_urls.txt filter=lfs diff=lfs merge=lfs -text
ftp_urls.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e77a3ce4a9b53f07f62088c0a0e1afdcacda0f66f4085184f2bb33cae5478c36
3
+ size 32160334
full.sh ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ # ==========================================================
6
+ # ARAGORN pipeline for genomes listed in ftp_urls.txt
7
+ # Adds: GC content + normalized 4-mers + TypePerAcc to NDJSON
8
+ # Keeps original file operations and structure
9
+ # ==========================================================
10
+
11
+ # --- Configuration ---
12
+ THREADS=16
13
+ FTP_LIST="ftp_urls.txt"
14
+ GENOMES_DIR="genomes"
15
+ OUTDIR="aragorn_out"
16
+ LOGDIR="logs"
17
+ TMPDIR_BASE="tmp_aragorn"
18
+ LINES_DIR="json_lines" # per-genome NDJSON lines (new)
19
+ AGG_JSON="FEATURES_ALL.ndjson" # merged NDJSON (new)
20
+
21
+ # --- Setup ---
22
+ mkdir -p "$GENOMES_DIR" "$OUTDIR" "$LOGDIR" "$TMPDIR_BASE" "$LINES_DIR"
23
+ : > "$AGG_JSON"
24
+
25
+ # --- Helpers: feature calculators ---
26
+
27
+ # 1) GC content
28
+ gc_content() {
29
+ local fasta="$1"
30
+ awk '
31
+ BEGIN{A=0;C=0;G=0;T=0}
32
+ /^>/ {next}
33
+ {
34
+ for(i=1;i<=length($0);i++){
35
+ b=toupper(substr($0,i,1))
36
+ if(b=="U") b="T"
37
+ if(b=="A") A++
38
+ else if(b=="C") C++
39
+ else if(b=="G") G++
40
+ else if(b=="T") T++
41
+ }
42
+ }
43
+ END{
44
+ total=A+C+G+T
45
+ gc=(total>0)?(G+C)/total:0
46
+ printf("gc_fraction\t%.6f\n", gc)
47
+ printf("gc_percent\t%.3f\n", gc*100)
48
+ printf("length_acgt\t%d\n", total)
49
+ printf("count_A\t%d\ncount_C\t%d\ncount_G\t%d\ncount_T\t%d\n", A,C,G,T)
50
+ }' "$fasta"
51
+ }
52
+
53
+ # 2) Normalized 4-mers (256 combos), freq = count / sum(L_i-3)
54
+ tetra_norm() {
55
+ local fasta="$1"
56
+ awk '
57
+ function count_seq(seq){
58
+ n=length(seq)
59
+ if(n<4) return
60
+ for(i=1;i<=n-3;i++){
61
+ k=substr(seq,i,4)
62
+ if(k ~ /^[ACGT]{4}$/) cnt[k]++
63
+ }
64
+ win_total += (n-3)
65
+ }
66
+ BEGIN{
67
+ split("A C G T", B, " ")
68
+ for(i1=1;i1<=4;i1++)
69
+ for(i2=1;i2<=4;i2++)
70
+ for(i3=1;i3<=4;i3++)
71
+ for(i4=1;i4<=4;i4++){
72
+ k=B[i1] B[i2] B[i3] B[i4]
73
+ cnt[k]=0
74
+ }
75
+ win_total=0
76
+ }
77
+ /^>/{
78
+ if(seq!=""){ count_seq(seq) }
79
+ seq=""
80
+ next
81
+ }
82
+ {
83
+ s=toupper($0)
84
+ gsub(/U/,"T",s)
85
+ gsub(/[^ACGT]/,"N",s)
86
+ seq=seq s
87
+ }
88
+ END{
89
+ if(seq!=""){ count_seq(seq) }
90
+ denom = (win_total>0)?win_total:1
91
+ split("A C G T", B, " ")
92
+ for(i1=1;i1<=4;i1++)
93
+ for(i2=1;i2<=4;i2++)
94
+ for(i3=1;i3<=4;i3++)
95
+ for(i4=1;i4<=4;i4++){
96
+ k=B[i1] B[i2] B[i3] B[i4]
97
+ freq = cnt[k]/denom
98
+ printf("%s\t%.8f\n", k, freq)
99
+ }
100
+ printf("windows_total\t%d\n", win_total)
101
+ }' "$fasta"
102
+ }
103
+
104
+ # 3) Type Per Acc from ARAGORN output (like your example)
105
+ trna_type_per_acc() {
106
+ local aragorn_txt="$1" acc="$2"
107
+ awk -v ACC="$acc" '
108
+ BEGIN{ IGNORECASE=1 }
109
+ {
110
+ line=$0
111
+ if(line ~ /tRNA/){
112
+ aa=""
113
+ if(match(line, /tRNA-([A-Za-z]+)/, m)){ aa=m[1] }
114
+ else if(match(line, /^[[:space:]]*([A-Za-z]{3})[[:space:]]*\(/, m)){ aa=m[1] }
115
+ ac=""
116
+ if(match(line, /\(([A-Za-z]{3})\)/, n)){ ac=n[1] }
117
+ if(ac!=""){
118
+ gsub(/u/,"T",ac); gsub(/U/,"T",ac); ac=toupper(ac); gsub(/[^ACGT]/,"N",ac)
119
+ }
120
+ if(aa!=""){ aa=tolower(aa); aa=toupper(substr(aa,1,1)) substr(aa,2,2) }
121
+ if(aa!="" && ac!=""){
122
+ key = ACC "_genome_" aa "_" ac
123
+ counts[key]++
124
+ }
125
+ }
126
+ }
127
+ END{ for(k in counts) printf("%d %s\n", counts[k], k) }
128
+ ' "$aragorn_txt" | sort -k1,1nr -k2,2
129
+ }
130
+
131
+ # 4) Build one NDJSON line into json_lines/<ACC>.ndjson
132
+ emit_ndjson_line() {
133
+ local acc="$1" gc_fp="$2" tetra_fp="$3" tpa_fp="$4" out_line_fp="$5"
134
+ {
135
+ printf '{'
136
+ printf '"acc":"%s",' "$acc"
137
+
138
+ printf '"gc":{'
139
+ awk '
140
+ $1=="gc_fraction"{printf("\"fraction\":%s", $2); next}
141
+ $1=="gc_percent"{printf(",\"percent\":%s", $2); next}
142
+ $1=="length_acgt"{printf(",\"length\":%s", $2); next}
143
+ $1=="count_A"{printf(",\"A\":%s", $2); next}
144
+ $1=="count_C"{printf(",\"C\":%s", $2); next}
145
+ $1=="count_G"{printf(",\"G\":%s", $2); next}
146
+ $1=="count_T"{printf(",\"T\":%s", $2); next}
147
+ ' "$gc_fp"
148
+ printf '},'
149
+
150
+ printf '"tetra_norm":{'
151
+ awk '
152
+ BEGIN{first=1}
153
+ {
154
+ if($1=="windows_total"){ wt=$2; next }
155
+ if(!first) printf(",")
156
+ printf("\"%s\":%s", $1, $2)
157
+ first=0
158
+ }
159
+ END{
160
+ if(!first) printf(",")
161
+ printf("\"windows_total\":%s", (wt ? wt : 0))
162
+ }
163
+ ' "$tetra_fp"
164
+ printf '},'
165
+
166
+ printf '"trna_type_per_acc":['
167
+ if [ -s "$tpa_fp" ]; then
168
+ awk '
169
+ BEGIN{first=1}
170
+ {
171
+ if(!first) printf(",")
172
+ printf("{\"count\":%d,\"label\":\"%s\"}", $1, $2)
173
+ first=0
174
+ }' "$tpa_fp"
175
+ fi
176
+ printf ']'
177
+
178
+ printf '}\n'
179
+ } > "$out_line_fp"
180
+ }
181
+
182
+ # --- Function for processing one genome (kept as in your script, extended) ---
183
+ process_one() {
184
+ url="$1"
185
+ acc=$(basename "$url" | sed 's/_genomic.*//')
186
+ tmpdir="$TMPDIR_BASE/${acc}_tmp"
187
+ mkdir -p "$tmpdir"
188
+ cd "$tmpdir" || exit 1
189
+
190
+ echo "[INFO] Processing $acc" >&2
191
+
192
+ # Download genome
193
+ wget -q -O "${acc}.fna.gz" "$url"
194
+ if [ ! -s "${acc}.fna.gz" ]; then
195
+ echo "[ERROR] Failed to download $acc" >&2
196
+ rm -rf "$tmpdir"
197
+ return
198
+ fi
199
+
200
+ # Unpack
201
+ gunzip -f "${acc}.fna.gz"
202
+ fasta=$(ls *.fna *.fa *.fasta 2>/dev/null | head -n 1)
203
+ if [ ! -f "$fasta" ]; then
204
+ echo "[ERROR] No FASTA found for $acc after extraction" >&2
205
+ rm -rf "$tmpdir"
206
+ return
207
+ fi
208
+
209
+ # Run ARAGORN (unchanged)
210
+ out_file="../../$OUTDIR/${acc}.txt"
211
+ log_file="../../$LOGDIR/${acc}.log"
212
+
213
+ echo "[RUN] ARAGORN on $acc" >&2
214
+ aragorn -t -l -gc1 -w -o "$out_file" "$fasta" >"$log_file" 2>&1
215
+
216
+ if [ -s "$out_file" ]; then
217
+ echo "[OK] $acc done" >&2
218
+ else
219
+ echo "[FAIL] ARAGORN empty output for $acc" >&2
220
+ echo "$acc" >> "../../$LOGDIR/failed_genomes.txt"
221
+ : > "$out_file" # keep an empty file to be parsable
222
+ fi
223
+
224
+ # --- NEW: compute features and emit NDJSON line (all stays in tmpdir / json_lines) ---
225
+ gc_fp="${acc}.gc.tsv"
226
+ tetra_fp="${acc}.tetra.tsv"
227
+ tpa_fp="${acc}.tpa.txt"
228
+
229
+ gc_content "$fasta" > "$gc_fp"
230
+ tetra_norm "$fasta" > "$tetra_fp"
231
+ trna_type_per_acc "$out_file" "$acc" > "$tpa_fp" || true
232
+
233
+ # write single-line NDJSON per genome
234
+ json_line="../../$LINES_DIR/${acc}.ndjson"
235
+ emit_ndjson_line "$acc" "$gc_fp" "$tetra_fp" "$tpa_fp" "$json_line"
236
+
237
+ # Cleanup tmp (unchanged)
238
+ cd - >/dev/null
239
+ rm -rf "$tmpdir"
240
+ }
241
+
242
+ export -f process_one gc_content tetra_norm trna_type_per_acc emit_ndjson_line
243
+ export OUTDIR LOGDIR TMPDIR_BASE LINES_DIR
244
+
245
+ # --- Parallel execution (unchanged) ---
246
+ echo "[PIPELINE] Starting with $(wc -l < "$FTP_LIST") genomes using $THREADS threads..."
247
+ cat "$FTP_LIST" | parallel -j "$THREADS" process_one {}
248
+
249
+ # --- Merge NDJSON lines to a single file (new, simple, sequential) ---
250
+ # deterministic order by filename
251
+ ls "$LINES_DIR"/*.ndjson 2>/dev/null | sort | xargs cat -- > "$AGG_JSON" || true
252
+
253
+ echo "[PIPELINE] All done. NDJSON -> $AGG_JSON"
254
+