-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrap.splitfasta.awk
More file actions
executable file
·75 lines (65 loc) · 1.69 KB
/
scrap.splitfasta.awk
File metadata and controls
executable file
·75 lines (65 loc) · 1.69 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
#!/usr/bin/awk -f
/^>/ {
# Extract the sequence name
seqname = substr($0, 2, 10)
# Replace any characters that might be problematic in filenames
gsub(/[^a-zA-Z0-9]/, "_", seqname)
# Close the previous file if it exists
if (outfile) {
close(outfile)
}
# Open a new file for the current sequence
outfile = seqname ".fasta"
}
{
# Write the current line to the appropriate file
print $0 > outfile
}
#!/usr/bin/awk -f
/^>/ {
# Extract the sequence name
seqname = substr($0, 2)
# Replace any characters that might be problematic in filenames
gsub(/[^a-zA-Z0-9]/, "_", seqname)
# Close the previous file if it's a different sequence name
if (seqname != prev_seqname && outfile) {
close(outfile)
}
# Open a new file for the current sequence if it's a new sequence name
outfile = seqname ".fasta"
prev_seqname = seqname
}
{
# Write the current line to the appropriate file
print $0 >> outfile
}
#!/bin/bash
# Check if the input file is provided
if [ -z "$1" ]; then
echo "Usage: $0 input.fasta"
exit 1
fi
input_file="$1"
awk '
BEGIN {
# Initialize variables
prev_seqname = ""
}
/^>/ {
# Extract the sequence name
seqname = substr($0, 2)
# Sanitize the sequence name for the file
seqname = gensub(/[^a-zA-Z0-9]/, "_", "g", seqname)
# Close the previous file if the sequence name changes
if (seqname != prev_seqname && outfile) {
close(outfile)
}
# Open (or append to) a file named after the current sequence
outfile = seqname ".fasta"
prev_seqname = seqname
}
{
# Write the line to the appropriate file
print $0 >> outfile
}
' "$input_file"