-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
691f953
commit d74ff8d
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import csv | ||
import sys | ||
from pathlib import Path | ||
|
||
def check_file_exists(file_path): | ||
return Path(file_path).is_file() | ||
|
||
def validate_samplesheet(samplesheet_path): | ||
# Check if the samplesheet path is correct | ||
if not Path(samplesheet_path).is_file(): | ||
print("Error: Invalid path to samplesheet.") | ||
sys.exit(1) | ||
|
||
# Open the samplesheet csv | ||
with open(samplesheet_path, 'r') as file: | ||
reader = csv.reader(file) | ||
headers = next(reader) # Read the header | ||
|
||
# Check if the header matches the expected format | ||
expected_header = ['sample', 'bam', 'bai'] | ||
if headers != expected_header: | ||
print("Error: Invalid header format.") | ||
sys.exit(1) | ||
|
||
# Check columns for all rows are csv | ||
for row in reader: | ||
for column in row: | ||
if '\t' in column: | ||
print("Error: Found a column with a tab delimiter, expected csv.") | ||
sys.exit(1) | ||
|
||
def modify_samplesheet(samplesheet_path): | ||
output_csv = sys.argv[2] | ||
output_header = ['sample', 'bam', 'bai'] | ||
|
||
with open(samplesheet_path, 'r') as file: | ||
reader = csv.reader(file) | ||
headers = next(reader) # Read the header | ||
|
||
rows = [] | ||
for row in reader: | ||
bam = row[1] | ||
bai = row[2] | ||
|
||
with open(output_csv, 'w', newline='') as csvfile: | ||
writer = csv.writer(csvfile) | ||
writer.writerow(output_header) | ||
writer.writerows(rows) | ||
|
||
def main(): | ||
input_csv = sys.argv[1] | ||
|
||
# Validate samplesheet | ||
validate_samplesheet(input_csv) | ||
|
||
# Modify samplesheet | ||
modify_samplesheet(input_csv) | ||
|
||
if __name__ == "__main__": | ||
main() |