-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename-consensus.sh
More file actions
71 lines (61 loc) · 2 KB
/
Copy pathrename-consensus.sh
File metadata and controls
71 lines (61 loc) · 2 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
#!/bin/bash
# Help function
help() {
echo "Usage: $0 <fasta_file> <sample_sheet>"
echo
echo "This script replaces the headers in a FASTA file based on a sample sheet."
echo
echo "FASTA file format:"
echo " The FASTA file should contain sequences with headers starting with '>' followed by the barcode ID."
echo " Example FASTA format:"
echo " >barcode01"
echo " ATCGGCTAGCTAGCTAGC"
echo " >barcode02"
echo " GCTAGCTAGCTAGCTAGC"
echo
echo "Sample sheet format:"
echo " The sample sheet is a CSV file containing two columns: the barcode and the new header."
echo " Example CSV format:"
echo " barcode01,M24-0025_24PCR-PA-15_01"
echo " barcode02,M24-0026_24PCR-PA-15_02"
echo " barcode03,M24-0027_24PCR-PA-15_03"
echo
echo "Options:"
echo " -h, --help Display this help message"
echo
exit 0
}
# Check if help is requested
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
help
fi
# Check if correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Error: Incorrect number of arguments."
echo "Usage: $0 <fasta_file> <sample_sheet>"
exit 1
fi
# Input FASTA file and CSV sample sheet
FASTA_FILE=$1
SAMPLE_SHEET=$2
# Check if the FASTA file and sample sheet exist
if [ ! -f "$FASTA_FILE" ]; then
echo "Error: FASTA file $FASTA_FILE does not exist."
exit 1
fi
if [ ! -f "$SAMPLE_SHEET" ]; then
echo "Error: Sample sheet $SAMPLE_SHEET does not exist."
exit 1
fi
# Create a temporary mapping file
awk -F, '{print ">" $1 "\t" $2}' "$SAMPLE_SHEET" > barcode_mapping.txt
# Replace the headers in the FASTA file based on the mapping
awk '
NR==FNR {a[$1]=$2; next}
/^>/ {print ">" a[$1]; next}
{print}
' barcode_mapping.txt "$FASTA_FILE" > new_fasta_file.fasta
# Cleanup temporary mapping file
rm barcode_mapping.txt
# Notify user of success
echo "Header replacement complete. Output saved to new_fasta_file.fasta"