-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_process.py
More file actions
86 lines (72 loc) · 3.37 KB
/
Copy pathcsv_process.py
File metadata and controls
86 lines (72 loc) · 3.37 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
75
76
77
78
79
80
81
82
83
84
import os
import subprocess
import pandas as pd
def count_matching_repeats(df, repeat_type):
"""
Counts the matching sequences for a specified repeat type (CAG or CTG) where
the matching percentage is 100, and also counts the total occurrences of that
repeat type.
"""
if 'Repeat Type' in df.columns and 'Matching Percentage' in df.columns:
matching_count = ((df['Repeat Type'] == repeat_type) & (df['Matching Percentage'] == 100)).sum()
total_count = (df['Repeat Type'] == repeat_type).sum()
return matching_count, total_count
return None, None
def count_fasta_sequences(fasta_path):
"""
Counts the number of sequences in a FASTA file by counting the number of header lines.
"""
try:
result = subprocess.run(['grep', '-c', '^>', fasta_path], capture_output=True, text=True)
return int(result.stdout.strip())
except Exception as e:
print(f"Error processing file {fasta_path}: {e}")
return None
def process_files(directory):
"""
Processes CSV and FASTA files in the directory to summarize matching and total counts for CAG and CTG repeats.
"""
results = []
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if filename.endswith(".csv"):
df = pd.read_csv(file_path)
matching_cag, total_cag = count_matching_repeats(df, 'CAG')
matching_ctg, total_ctg = count_matching_repeats(df, 'CTG')
results.append({
'Filename': filename,
'CAG Matching Count': matching_cag,
'CAG Total Count': total_cag,
'CTG Matching Count': matching_ctg,
'CTG Total Count': total_ctg,
'FASTA Total Count': None # Placeholder for FASTA counts
})
elif filename.endswith(".fasta"):
fasta_count = count_fasta_sequences(file_path)
results.append({
'Filename': filename,
'CAG Matching Count': None,
'CAG Total Count': None,
'CTG Matching Count': None,
'CTG Total Count': None,
'FASTA Total Count': fasta_count
})
results_df = pd.DataFrame(results)
# Separate CSV and FASTA entries
csv_results_df = results_df[results_df['Filename'].str.endswith('.csv')].reset_index(drop=True)
fasta_results_df = results_df[results_df['Filename'].str.endswith('.fasta')].reset_index(drop=True)
# Remove '_comparison_report.csv' and '.fasta' from filenames for alignment
csv_results_df['Filename'] = csv_results_df['Filename'].str.replace('_comparison_report.csv', '', regex=False)
fasta_results_df['Filename'] = fasta_results_df['Filename'].str.replace('.fasta', '', regex=False)
# Merge dataframes on Filename
merged_df = pd.merge(csv_results_df, fasta_results_df, on='Filename', how='outer', suffixes=('_csv', '_fasta'))
# Save the merged results to a new CSV file
output_path = os.path.join(directory, 'results_summary.csv')
merged_df.to_csv(output_path, index=False)
print(f'Results saved to {output_path}')
import os
if __name__ == "__main__":
# Get the current working directory (the folder where the script is being run)
folder_path = os.getcwd()
# Call the function to process files in the current folder
process_files(folder_path)