From 6cd03d1c377a0e9ed5d80c6fd27e9618e8b40ddf Mon Sep 17 00:00:00 2001 From: borrageiros Date: Wed, 3 Jul 2024 23:38:52 +0200 Subject: [PATCH] Subir archivos a "/" --- change_quality.py | 163 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 change_quality.py diff --git a/change_quality.py b/change_quality.py new file mode 100644 index 0000000..84537ee --- /dev/null +++ b/change_quality.py @@ -0,0 +1,163 @@ +import argparse +import random +import os + + + +### FORWARD +def generate_quality_first_bases_fw(): + return ''.join(random.choice('?@ABCD') for _ in range(2)) + +def generate_quality_next_bases_fw(): + return ''.join(random.choice('ABCDEG') for _ in range(3)) + +def generate_quality_middle_bases_fw(): + return ''.join(random.choice('CDEFGH') for _ in range(5)) + +def generate_quality_last_bases_fw(): + return ''.join(random.choice('HI') for _ in range(200)) + +def generate_quality_decreasing_200_220_fw(): + return ''.join(random.choice('CDEFGH') for _ in range(30)) + +def generate_quality_decreasing_220_260_fw(): + return ''.join(random.choice('ABCDEG') for _ in range(25)) + +def generate_quality_decreasing_260_290_fw(): + return ''.join(random.choice('?@ABCD') for _ in range(25)) + +def generate_quality_decreasing_290_300_fw(): + return ''.join(random.choice('?@') for _ in range(10)) + +def change_quality_fw(input_file, output_file): + with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out: + for line_num, line in enumerate(f_in): + if line_num % 4 == 0: + f_out.write(line) + elif line_num % 4 == 1: + f_out.write(line) + elif line_num % 4 == 2: + f_out.write(line) + else: + new_quality = ( + generate_quality_first_bases_fw() + + generate_quality_next_bases_fw() + + generate_quality_middle_bases_fw() + + generate_quality_last_bases_fw() + + generate_quality_decreasing_200_220_fw() + + generate_quality_decreasing_220_260_fw() + + generate_quality_decreasing_260_290_fw() + + generate_quality_decreasing_290_300_fw() + ) + f_out.write(new_quality + '\n') + + +### REVERSE +def generate_quality_first_bases_rv(read_length): + return ''.join(random.choice('?@ABCD') for _ in range(read_length * 0.01)) + +def generate_quality_next_bases_rv(read_length): + return ''.join(random.choice('ABCDEG') for _ in range(read_length * 0.02)) + +def generate_quality_middle_bases_rv(read_length): + return ''.join(random.choice('CDEFGH') for _ in range(read_length * 0.02)) + +def generate_quality_main_bases_rv(read_length): + return ''.join(random.choice('HI') for _ in range((read_length * 0.10 * 6) + (read_length * 0.05))) + +def generate_quality_decreasing_175_200_rv(read_length): + return ''.join(random.choice('CDEFGH') for _ in range(read_length * 0.10)) + +def generate_quality_decreasing_200_220_rv(read_length): + return ''.join(random.choice('ABCDEG') for _ in range(read_length * 0.10)) + +def generate_quality_decreasing_220_260_rv(read_length): + return ''.join(random.choice('?@ABCD') for _ in range(read_length * 0.05)) + +def generate_quality_decreasing_260_290_rv(read_length): + return ''.join(random.choice('?@ABC') for _ in range(read_length * 0.04)) + +def generate_quality_decreasing_290_300_rv(read_length): + return ''.join(random.choice('?@AB') for _ in range(read_length * 0.01)) + +def change_quality_rv(input_file, output_file): + with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out: + for line_num, line in enumerate(f_in): + if line_num % 4 == 0: + f_out.write(line) + elif line_num % 4 == 1: + f_out.write(line) + elif line_num % 4 == 2: + f_out.write(line) + else: + new_quality = ( + generate_quality_first_bases_rv() + + generate_quality_next_bases_rv() + + generate_quality_middle_bases_rv() + + generate_quality_main_bases_rv() + + generate_quality_decreasing_175_200_rv() + + generate_quality_decreasing_200_220_rv() + + generate_quality_decreasing_220_260_rv() + + generate_quality_decreasing_260_290_rv() + + generate_quality_decreasing_290_300_rv() + ) + f_out.write(new_quality + '\n') + + + +### COMMANDS +# Create the parser +parser = argparse.ArgumentParser(description='Script to change the quality of a FASTQ file.') + +# Mandatory commands +parser.add_argument('-i', '--input', type=str, required=True, help='Input ".fastq" file') +parser.add_argument('-o', '--output', type=str, required=True, help='Output file') +parser.add_argument('-ow', '--overwrite', action='store_true', help='Force overwrite output') + +# Mutually exclusive group of arguments +group = parser.add_mutually_exclusive_group(required=True) +group.add_argument('-fw', '--forward', action='store_true', help='Forward mode (default)') +group.add_argument('-rv', '--reverse', action='store_true', help='Reverse mode') + +# Parse the arguments +args = parser.parse_args() + + +def main(): + # Check if input file is 'fastq' + if not args.input.endswith('.fastq'): + print("The input needs to be '.fastq'") + return + + # Check if the input file not exists + if not os.path.exists(args.input): + print("The input file does not exists") + return + + # Check if output file is 'fastq' + if not args.output.endswith('.fastq'): + print("The output needs to be '.fastq'") + return + + # Check if the input and the output are the same + if os.path.abspath(args.input) == os.path.abspath(args.output): + base, ext = os.path.splitext(args.output) + args.output = base + "_changed" + ext + print("The input file are the same as output file.") + print("Saving the output as: ", args.output) + + # Check if the output file exists + if not args.overwrite and os.path.exists(args.output): + print(args.output, "file already exists, use -ow to force the overwrite") + return + + # Execute change quality + if args.forward: + change_quality_fw(args.input, args.output) + print("Qualitys changed successfully (forward) and saved at:", args.output) + elif args.reverse: + change_quality_rv(args.input, args.output) + print("Qualitys changed successfully (reverse) and saved at:", args.output) + +if __name__ == "__main__": + main()