# HG changeset patch # User rplanel # Date 1565277510 14400 # Node ID 3e33310a7082c4f54212560379375cc27630b071 planemo upload for repository https://github.com/rplanel/galaxy-tools/tree/master/tools/sequence-splitter commit cba12d215a89e9685c7d1f55d067770d7ec0dea2 diff -r 000000000000 -r 3e33310a7082 sequence-splitter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sequence-splitter.py Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +# coding: utf-8 + +import argparse +import logging +import os +from itertools import chain, islice, tee + +# BioPython +from Bio import SeqIO + +logging.basicConfig( + level=logging.INFO, format="%(asctime)s : %(levelname)s : %(message)s" +) +logger = logging.getLogger() + + +def main(): + args = parse_arguments() + # extract the basename and the file extension + basename, file_extension = os.path.splitext(args.sequences) + # extract the filename (with no extension) + _, filename = os.path.split(basename) + chunk_size = args.chunk_size + # split the sequences in chunks + if chunk_size: + logger.info("%s = %s", "chunk size parameter", chunk_size) + sequences_record = gen_sequence_record(args.sequences, args.format) + chunks = gen_get_chunks_by_size(sequences_record, chunk_size) + else: + logger.info("%s = %s", "number of chunks parameter", args.nb_chunk) + chunks = gen_get_chunks(args.sequences, args.format, args.nb_chunk) + + # Write the chunks in numbered files. + write_chunks(chunks, args.output, filename, file_extension, args.format) + + +def gen_get_chunks(sequences_path, sequences_format, nb_chunk): + """[summary] + + Arguments: + sequences_path {[type]} -- [description] + sequences_format {[type]} -- [description] + nb_chunk {[type]} -- [description] + + Returns: + [type] -- [description] + """ + # First record to count the sequences + sequences_record_to_count = gen_sequence_record( + sequences_path, sequences_format) + # Get the number of sequences + nb_sequences = get_nb_sequences(sequences_record_to_count) + logger.info("%s = %i", "Number of sequences per chunk", nb_sequences) + # Second record to that will be splitted + sequences_to_split = gen_sequence_record(sequences_path, sequences_format) + + # Get the size of the chunks + chunk_size = int(nb_sequences / nb_chunk) if nb_sequences > nb_chunk else 1 + return gen_get_chunks_by_size(sequences_to_split, chunk_size) + + +def gen_get_chunks_by_size(iterable, size=10): + logger.info( + "%s = %i", + "chunk size got (could be different from parameter if more chunk asked \ + than sequences in multifasta)", + size + ) + iterator = iter(iterable) + for first in iterator: + yield chain([first], islice(iterator, size - 1)) + + +def gen_sequence_record(sequences_path, sequence_format): + return SeqIO.parse(sequences_path, sequence_format) + + +def get_nb_sequences(sequences): + return sum(1 for _ in sequences) + + +def write_chunks(iterable, dirname, filename, file_extension, sequence_format): + for idx, chunk in enumerate(iterable): + if not os.path.exists(dirname): + os.mkdir(dirname) + output_file = os.path.join( + dirname, filename + "-chunk-" + str(idx + 1) + file_extension + ) + with open(output_file, mode="w") as output_handle: + count_seq, seq_to_write = tee(chunk, 2) + logger.info( + "%s : number of seuquences = %i", output_file, len( + list(count_seq)) + ) + SeqIO.write(seq_to_write, output_handle, sequence_format) + + +def parse_arguments(): + parser = argparse.ArgumentParser(description="Split fasta/fastq files") + parser.add_argument( + "-s", "--sequences", type=str, help="File that contains the sequences" + ) + + parser.add_argument("-f", "--format", type=str, + help="File format (fastq, fasta)") + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + "-c", "--chunk-size", + type=int, + help="The number of sequences by chunks." + ) + group.add_argument("-n", "--nb-chunk", help="Number of chunks", type=int) + + parser.add_argument( + "-o", + "--output", + type=str, + default="./", + help="The output directory where the chunks will be saved", + ) + + return parser.parse_args() + + +if __name__ == "__main__": + logger.info("START") + main() + logger.info("FINISHED") diff -r 000000000000 -r 3e33310a7082 sequence-splitter.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sequence-splitter.xml Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,132 @@ + + + python + biopython + + + operation_2409 + operation_3359 + + + topic_3307 + topic_0080 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 000000000000 -r 3e33310a7082 test-data/sample-2-chunk-1.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-2-chunk-1.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,2 @@ +>derice +ACTGACTAGCTAGCTAACTG diff -r 000000000000 -r 3e33310a7082 test-data/sample-2-chunk-2.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-2-chunk-2.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,2 @@ +>sanka +GCATCGTAGCTAGCTACGAT diff -r 000000000000 -r 3e33310a7082 test-data/sample-2-chunk-3.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-2-chunk-3.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,2 @@ +>junior +CATCGATCGTACGTACGTAG diff -r 000000000000 -r 3e33310a7082 test-data/sample-2-chunk-4.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-2-chunk-4.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,2 @@ +>yul +ATCGATCGATCGTACGATCG diff -r 000000000000 -r 3e33310a7082 test-data/sample-3-chunk-1.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-3-chunk-1.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,20 @@ +@cluster_2:UMI_ATTCCG +TTTCCGGGGCACATAATCTTCAGCCGGGCGC ++ +9C;=;=<9@4868>9:67AA<9>65<=>591 +@cluster_8:UMI_CTTTGA +TATCCTTGCAATACTCTCCGAACGGGAGAGC ++ +1/04.72,(003,-2-22+00-12./.-.4- +@cluster_12:UMI_GGTCAA +GCAGTTTAAGATCATTTTATTGAAGAGCAAG ++ +?7?AEEC@>=1?A?EEEB9ECB?==:B.A?A +@cluster_21:UMI_AGAACA +GGCATTGCAAAATTTATTACACCCCCAGATC ++ +>=2.660/?:36AD;0<14703640334-// +@cluster_29:UMI_GCAGGA +CCCCCTTAAATAGCTGTTTATTTGGCCCCAG ++ +8;;;>DC@DAC=B?C@9?B?CDCB@><90;?150 +@cluster_36:UMI_AACAGA +TCCCCCCCCCAAATCGGAAAAACACACCCCC ++ +5?:5;<02:@977=:<0=9>@5>7>;>*3,- +@cluster_37:UMI_GAGGAG +GTCTTTGTACAAAATTTTATTAAAGGTCTTT ++ +?B?DEC@A=?ADDAEEEC?EC@D6A@@>DE4 +@cluster_39:UMI_GAACCG +CCTTCCATCACCAGATCGGAAAAACACACGC ++ +00>7;8@5<192?/8;0;;>=3=/3239713 +@cluster_43:UMI_GGATTG +GAGTTATAATCCAATCTTTATTTAAAAATCT ++ +>=AEC?C@;??0A>?0DEB9EEB@DDC1?=6 diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-1.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-1.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,4 @@ +>derice +ACTGACTAGCTAGCTAACTG +>sanka +GCATCGTAGCTAGCTACGAT diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-1.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-1.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +@cluster_2:UMI_ATTCCG +TTTCCGGGGCACATAATCTTCAGCCGGGCGC ++ +9C;=;=<9@4868>9:67AA<9>65<=>591 +@cluster_8:UMI_CTTTGA +TATCCTTGCAATACTCTCCGAACGGGAGAGC ++ +1/04.72,(003,-2-22+00-12./.-.4- diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-2.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-2.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,4 @@ +>junior +CATCGATCGTACGTACGTAG +>yul +ATCGATCGATCGTACGATCG diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-2.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-2.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +@cluster_12:UMI_GGTCAA +GCAGTTTAAGATCATTTTATTGAAGAGCAAG ++ +?7?AEEC@>=1?A?EEEB9ECB?==:B.A?A +@cluster_21:UMI_AGAACA +GGCATTGCAAAATTTATTACACCCCCAGATC ++ +>=2.660/?:36AD;0<14703640334-// diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-3.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-3.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +@cluster_29:UMI_GCAGGA +CCCCCTTAAATAGCTGTTTATTTGGCCCCAG ++ +8;;;>DC@DAC=B?C@9?B?CDCB@><90;?150 diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-4.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-4.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +@cluster_36:UMI_AACAGA +TCCCCCCCCCAAATCGGAAAAACACACCCCC ++ +5?:5;<02:@977=:<0=9>@5>7>;>*3,- +@cluster_37:UMI_GAGGAG +GTCTTTGTACAAAATTTTATTAAAGGTCTTT ++ +?B?DEC@A=?ADDAEEEC?EC@D6A@@>DE4 diff -r 000000000000 -r 3e33310a7082 test-data/sample-chunk-5.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample-chunk-5.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +@cluster_39:UMI_GAACCG +CCTTCCATCACCAGATCGGAAAAACACACGC ++ +00>7;8@5<192?/8;0;;>=3=/3239713 +@cluster_43:UMI_GGATTG +GAGTTATAATCCAATCTTTATTTAAAAATCT ++ +>=AEC?C@;??0A>?0DEB9EEB@DDC1?=6 diff -r 000000000000 -r 3e33310a7082 test-data/sample.fasta --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample.fasta Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,8 @@ +>derice +ACTGACTAGCTAGCTAACTG +>sanka +GCATCGTAGCTAGCTACGAT +>junior +CATCGATCGTACGTACGTAG +>yul +ATCGATCGATCGTACGATCG \ No newline at end of file diff -r 000000000000 -r 3e33310a7082 test-data/sample.fastq --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sample.fastq Thu Aug 08 11:18:30 2019 -0400 @@ -0,0 +1,40 @@ +@cluster_2:UMI_ATTCCG +TTTCCGGGGCACATAATCTTCAGCCGGGCGC ++ +9C;=;=<9@4868>9:67AA<9>65<=>591 +@cluster_8:UMI_CTTTGA +TATCCTTGCAATACTCTCCGAACGGGAGAGC ++ +1/04.72,(003,-2-22+00-12./.-.4- +@cluster_12:UMI_GGTCAA +GCAGTTTAAGATCATTTTATTGAAGAGCAAG ++ +?7?AEEC@>=1?A?EEEB9ECB?==:B.A?A +@cluster_21:UMI_AGAACA +GGCATTGCAAAATTTATTACACCCCCAGATC ++ +>=2.660/?:36AD;0<14703640334-// +@cluster_29:UMI_GCAGGA +CCCCCTTAAATAGCTGTTTATTTGGCCCCAG ++ +8;;;>DC@DAC=B?C@9?B?CDCB@><90;?150 +@cluster_36:UMI_AACAGA +TCCCCCCCCCAAATCGGAAAAACACACCCCC ++ +5?:5;<02:@977=:<0=9>@5>7>;>*3,- +@cluster_37:UMI_GAGGAG +GTCTTTGTACAAAATTTTATTAAAGGTCTTT ++ +?B?DEC@A=?ADDAEEEC?EC@D6A@@>DE4 +@cluster_39:UMI_GAACCG +CCTTCCATCACCAGATCGGAAAAACACACGC ++ +00>7;8@5<192?/8;0;;>=3=/3239713 +@cluster_43:UMI_GGATTG +GAGTTATAATCCAATCTTTATTTAAAAATCT ++ +>=AEC?C@;??0A>?0DEB9EEB@DDC1?=6 \ No newline at end of file