biofile-kit 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: get_seq_len.py
4
+ Description: Get each sequence length of FASTA file.
5
+ CreateDate: 2022/3/25
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from io import TextIOWrapper
10
+ from typing import Tuple, Union
11
+ import click
12
+ from pybioinformatic import Fasta, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(fasta_files: Tuple[Union[str, TextIOWrapper]],
17
+ parse_seqids: bool = True,
18
+ out_file: TextIOWrapper = None):
19
+ for fasta_file in fasta_files:
20
+ with Fasta(fasta_file) as fa:
21
+ for nucl_obj in fa.parse(parse_seqids):
22
+ click.echo(f'{nucl_obj.get_seq_len_info()}\t{fa.name}', out_file)
23
+
24
+
25
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
26
+ @click.argument('fasta_files', nargs=-1, metavar='<fasta files|stdin>', type=click.File('r'), required=True)
27
+ @click.option('-p', '--parse_seqids', 'parse_seqids',
28
+ is_flag=True, flag_value=True,
29
+ help='Parse sequence id.')
30
+ @click.option('-o', '--output_file', 'outfile',
31
+ metavar='<file|stdout>', type=click.File('w'),
32
+ help=r'Output file (Seq_id\tSeq_len\tFile_path), stdout by default.')
33
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
34
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
35
+ def run(fasta_files, parse_seqids, outfile):
36
+ """Get each sequence length of FASTA file."""
37
+ main(fasta_files, parse_seqids, outfile)
38
+
39
+
40
+ if __name__ == '__main__':
41
+ run()
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: id_index_seq.py
4
+ Description: Extract sequences from FASTA file based on the id provided.
5
+ CreateDate: 2022/3/25
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Tuple, Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Fasta, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.1')
14
+
15
+
16
+ def main(fasta_files: Tuple[Union[str, TextIOWrapper]],
17
+ parse_seqids: bool,
18
+ id_file: TextIOWrapper,
19
+ match: bool = True,
20
+ record_source: bool = False,
21
+ log_file: TextIOWrapper = None,
22
+ output_file: TextIOWrapper = None):
23
+ raw_id_set = set(line.strip() for line in id_file)
24
+ have_found_id_set = set()
25
+ for fasta_file in fasta_files:
26
+ with Fasta(fasta_file) as fa:
27
+ for seq_obj in fa.parse(parse_seqids):
28
+ if match and seq_obj.id in raw_id_set:
29
+ have_found_id_set.add(seq_obj.id)
30
+ if record_source:
31
+ seq_obj.id = f'{seq_obj.id} from={fa.name}'
32
+ click.echo(seq_obj, output_file)
33
+ elif not match:
34
+ for _id in raw_id_set:
35
+ if (seq_obj.id in _id) or (_id in seq_obj.id):
36
+ have_found_id_set.add(_id)
37
+ if record_source:
38
+ seq_obj.id = f'{seq_obj.id} from={fa.name}'
39
+ click.echo(seq_obj, output_file)
40
+ # report sequence that not found.
41
+ not_found_id = list(raw_id_set - have_found_id_set)
42
+ if not_found_id:
43
+ not_found_id.sort()
44
+ msg = ' not found\n'.join(not_found_id) + ' not found\n'
45
+ click.echo(f'\033[33m{msg}\033[0m', err=True, file=log_file)
46
+
47
+
48
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
49
+ @click.argument('fasta_files', nargs=-1, metavar='<fasta files|stdin>', required=True, type=click.File('r'))
50
+ @click.option('-p', '--parse_seqids', 'parse_seqids',
51
+ is_flag=True, flag_value=True,
52
+ help='Parse sequence id in FASTA file.')
53
+ @click.option('-id', '--id_file', 'id_file',
54
+ metavar='<file|stdin>', type=click.File('r'), required=True,
55
+ help='Input id file (one id per line).')
56
+ @click.option('--match/--contain',
57
+ default=True, show_default=True,
58
+ help='Whether the id supplied should exactly match the ID of the sequence.')
59
+ @click.option('-s', '--record_source', 'record_source',
60
+ is_flag=True, flag_value=True,
61
+ help='Record the source of sequence to sequence ID.')
62
+ @click.option('-log', '--log_file', 'log_file',
63
+ metavar='<file|stderr>', type=click.File('w'),
64
+ help='Output log file, stderr by default.')
65
+ @click.option('-o', '--output_file', 'outfile',
66
+ metavar='<file|stdout>', type=click.File('w'),
67
+ help='Output file, stdout by default.')
68
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
69
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
70
+ def run(fasta_files, parse_seqids, id_file, match, record_source, log_file, outfile):
71
+ """Extract sequences from FASTA file based on the id provided."""
72
+ main(fasta_files, parse_seqids, id_file, match, record_source, log_file, outfile)
73
+
74
+
75
+ if __name__ == '__main__':
76
+ run()
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: motif_finder.py
4
+ Description: Find the motif in the sequence.
5
+ CreateDate: 2022/3/29
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from io import TextIOWrapper
10
+ from typing import Tuple, Union
11
+ import click
12
+ from pybioinformatic import Fasta, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.3.0')
14
+
15
+
16
+ def main(fasta_files: Tuple[Union[str, TextIOWrapper]],
17
+ motif: str,
18
+ only_forward: bool = False,
19
+ quiet: bool = False,
20
+ log_file: TextIOWrapper = None,
21
+ output_file: TextIOWrapper = None):
22
+ click.echo('Seq_id\tStart\tEnd\tStrand\tMotif\tSequence', output_file)
23
+ for fasta_file in fasta_files:
24
+ with Fasta(fasta_file) as fa:
25
+ for seq_obj in fa.parse():
26
+ ret = seq_obj.find_motif(motif=motif, only_forward=only_forward)
27
+ if 'not found' not in ret:
28
+ click.echo(ret, output_file)
29
+ else:
30
+ if not quiet:
31
+ click.echo(f"\033[33m{ret}\033[0m", err=True, file=log_file)
32
+
33
+
34
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
35
+ @click.argument('fasta_files', nargs=-1, metavar='<fasta files|stdin>', type=click.File('r'), required=True)
36
+ @click.option('-m', '--motif', 'motif',
37
+ metavar='<str>', required=True,
38
+ help='Specify motif sequence, support for regular expressions.')
39
+ @click.option('-F', '--only-forward', 'only_forward', is_flag=True, flag_value=True,
40
+ help='Only search forward chain.')
41
+ @click.option('-q', '--quiet', 'quiet',
42
+ is_flag=True, flag_value=True,
43
+ help='Do not report sequence that not found motif. This conflicts with the "-l --log_file" option and '
44
+ 'takes precedence over the "-l --log_file" option.')
45
+ @click.option('-log', '--log_file', 'log_file',
46
+ metavar='<file|stderr>', type=click.File('w'),
47
+ help='Write the sequence that not found motif to logfile, stderr by default. '
48
+ 'This conflicts with the "-q --quiet" option and has a lower priority than the "-q --quiet" option.')
49
+ @click.option('-o', '--output_file', 'outfile',
50
+ metavar='<file|stdout>', type=click.File('w'),
51
+ help=r'Output file (Seq_id\tStart\tEnd\tStrand\tMotif\tSequence), stdout by default.')
52
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
53
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
54
+ def run(fasta_files, motif, only_forward, quiet, log_file, outfile):
55
+ """Find the motif in the sequence."""
56
+ main(
57
+ fasta_files=fasta_files,
58
+ motif=motif,
59
+ only_forward=only_forward,
60
+ quiet=quiet,
61
+ log_file=log_file,
62
+ output_file=outfile
63
+ )
64
+
65
+
66
+ if __name__ == '__main__':
67
+ run()
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: seq_rename.py
4
+ Description: Rename sequence id.
5
+ CreateDate: 2024/12/2
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from io import TextIOWrapper
10
+ import click
11
+ from pybioinformatic import Fasta, Displayer
12
+
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(fasta_file: TextIOWrapper,
17
+ map_file: TextIOWrapper,
18
+ parse_seqids: bool,
19
+ remain: bool,
20
+ output_file: TextIOWrapper = None):
21
+ map_dict = {line.strip().split('\t')[0]: line.strip().split('\t')[1] for line in map_file}
22
+ with Fasta(fasta_file) as fa:
23
+ for ret in fa.rename(map_dict=map_dict, parse_seq_id=parse_seqids, remain=remain):
24
+ click.echo(ret, output_file)
25
+
26
+
27
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
28
+ @click.option('-i', '--fasta', 'fasta_file',
29
+ metavar='<file|stdin>', type=click.File('r'), required=True,
30
+ help='Input fasta file.')
31
+ @click.option('-m', '--map', 'map_file',
32
+ metavar='<file|stdin>', required=True, type=click.File('r'),
33
+ help=r'Sequence id map file. (Old_id\tNew_id\tEtc)')
34
+ @click.option('-p', '--parse-seqids', 'parse_seqids', is_flag=True, flag_value=True,
35
+ help='Parse sequence id in fasta file.')
36
+ @click.option('-r', '--remain', 'remain',
37
+ is_flag=True, flag_value=True,
38
+ help='Remain the sequence which is not included in map file.')
39
+ @click.option('-o', '--output', 'output_file',
40
+ metavar='<file|stdout>', type=click.File('w'),
41
+ help='Output file, stdout by default.')
42
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
43
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
44
+ def run(fasta_file, map_file, parse_seqids, remain, output_file):
45
+ """Rename sequence id."""
46
+ main(fasta_file, map_file, parse_seqids, remain, output_file)
47
+
48
+
49
+ if __name__ == '__main__':
50
+ run()
@@ -0,0 +1,18 @@
1
+ from biofile_kit.gff_utils.get_feature_density import run as get_feature_density
2
+ from biofile_kit.gff_utils.gff2bed import run as gff2bed
3
+ from biofile_kit.gff_utils.gff2gtf import run as gff2gtf
4
+ from biofile_kit.gff_utils.gff_extract_seq import run as gff_extract_seq
5
+ from biofile_kit.gff_utils.gff_sort import run as gff_sort
6
+ from biofile_kit.gff_utils.rename import run as rename
7
+
8
+ __version__ = '0.1.0'
9
+
10
+ __all__ = [
11
+ 'get_feature_density',
12
+ 'gff2bed',
13
+ 'gff2gtf',
14
+ 'gff_extract_seq',
15
+ 'gff_sort',
16
+ 'rename',
17
+ '__version__'
18
+ ]
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: get_feature_density.py
4
+ Description: Get feature density from GFF file.
5
+ CreateDate: 2022/10/21
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Gff, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(chr_len_file: TextIOWrapper,
17
+ gff_file: Union[str, TextIOWrapper],
18
+ feature: str,
19
+ span: int,
20
+ output_file: TextIOWrapper = None):
21
+ chr_len_dict = {line.split('\t')[0]: int(line.strip().split('\t')[1])
22
+ for line in chr_len_file if line.strip()}
23
+ with Gff(gff_file) as gff:
24
+ for line in gff.get_feature_density(chr_len_dict, feature, span):
25
+ click.echo(line, output_file)
26
+
27
+
28
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
29
+ @click.option('-l', '--chr_len_file', 'chr_len_file',
30
+ metavar='<file|stdin>', type=click.File('r'), required=True,
31
+ help=r'Input chromosome length file. (Chr_num\tLength)')
32
+ @click.option('-g', '--gff_file', 'gff_file',
33
+ metavar='<gff file|stdin>', type=click.File('r'), required=True,
34
+ help='Input genome annotation GFF file.')
35
+ @click.option('-f', '--feature_type', 'feature_type',
36
+ metavar='<str>', required=True,
37
+ help='Specify feature type. (eg. exon)')
38
+ @click.option('-s', '--span', 'span',
39
+ metavar='<int>', type=int, default=100000, show_default=True,
40
+ help='Density statistical span.')
41
+ @click.option('-o', '--output_file', 'out',
42
+ metavar='<file|stdout>', type=click.File('w'),
43
+ help='Output file, if not specified, stdout by default.')
44
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
45
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
46
+ def run(chr_len_file, gff_file, feature_type, span, out):
47
+ """Get feature density from GFF file."""
48
+ main(chr_len_file, gff_file, feature_type, span, out)
49
+
50
+
51
+ if __name__ == '__main__':
52
+ run()
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: gff2bed.py
4
+ Description: Convert the file format from GFF to BED.
5
+ CreateDate: 2022/3/23
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Gff, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(gff_file: Union[str, TextIOWrapper],
17
+ feature_type: str,
18
+ bed_file: TextIOWrapper = None):
19
+ with Gff(gff_file) as gff:
20
+ if feature_type:
21
+ feature_type = feature_type.split(',')
22
+ for line in gff.to_bed(feature_type):
23
+ click.echo(line, bed_file)
24
+
25
+
26
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
27
+ @click.option('-g', '--gff_file', 'gff_file',
28
+ metavar='<gff file|stdin>', type=click.File('r'), required=True,
29
+ help='Input GFF file.')
30
+ @click.option('-t', '--feature_type', 'feature_type', metavar='<str>',
31
+ help='Specify the type of feature to convert to BED format, multiple types are separated by commas. [default: all]')
32
+ @click.option('-o', '--output_bed_file', 'output_bed_file',
33
+ metavar='<bed file|stdout>', type=click.File('w'),
34
+ help='Output BED file, stdout by default.')
35
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
36
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
37
+ def run(gff_file, feature_type, output_bed_file):
38
+ """Convert the file format from GFF to BED."""
39
+ main(gff_file, feature_type, output_bed_file)
40
+
41
+
42
+ if __name__ == '__main__':
43
+ run()
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: gff2gtf.py
4
+ Description: Convert the file format from GFF to GTF
5
+ CreateDate: 2022/3/23
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Gff, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(gff_file: Union[str, TextIOWrapper],
17
+ gtf_file: TextIOWrapper = None):
18
+ with Gff(gff_file) as gff:
19
+ for line in gff.to_gtf():
20
+ click.echo(line, gtf_file)
21
+
22
+
23
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
24
+ @click.option('-i', '--gff_file', 'gff_file',
25
+ metavar='<gff file|stdin>', type=click.File('r'), required=True,
26
+ help='Input GFF file.')
27
+ @click.option('-o', '--gtf_file', 'gtf_file',
28
+ metavar='<gtf file|stdout>', type=click.File('w'),
29
+ help='Output GTF file, stdout by default.')
30
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
31
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
32
+ def run(gff_file, gtf_file):
33
+ """Convert the file format from GFF to GTF."""
34
+ main(gff_file, gtf_file)
35
+
36
+
37
+ if __name__ == '__main__':
38
+ run()
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: gff_extract_seq.py
4
+ Description: Extract sequences from GFF file.
5
+ CreateDate: 2022/3/24
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Gff, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.2.0')
14
+
15
+
16
+ def main(gff_file: Union[str, TextIOWrapper],
17
+ fa_file: Union[str, TextIOWrapper],
18
+ feature_type: str,
19
+ id_field: str,
20
+ id_file: TextIOWrapper = None,
21
+ remain_attr: list = None,
22
+ num_processing: int = 4,
23
+ out_file: TextIOWrapper = None):
24
+ id_set = set(i.strip() for i in id_file if i.strip()) if id_file else None
25
+ with Gff(gff_file) as gff:
26
+ generator = gff.extract_seq(
27
+ fasta_file=fa_file,
28
+ feature_type=feature_type,
29
+ id_field=id_field,
30
+ feature_id_set=id_set,
31
+ remain_attr=remain_attr,
32
+ num_processing=num_processing
33
+ )
34
+ for nucl_obj in generator:
35
+ click.echo(nucl_obj, out_file)
36
+
37
+
38
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
39
+ @click.option('-g', '--gff-file', 'gff_file',
40
+ metavar='<gff file|stdin>', type=click.File('r'), required=True,
41
+ help='Input GFF file.')
42
+ @click.option('-r', '--ref-fasta', 'ref_fasta_file',
43
+ metavar='<fasta file|stdin>', type=click.File('r'), required=True,
44
+ help='Input reference sequence FASTA file.')
45
+ @click.option('-t', '--feature-type', 'feature_type',
46
+ metavar='<str>', required=True,
47
+ help='Specify feature type of sequence. (Column 3 of the gff file, eg: gene, mRNA, etc)')
48
+ @click.option('-I', '--id-field', 'id_field',
49
+ metavar='<str>', default='ID', show_default=True,
50
+ help='Specify the gff file column 9 field as the sequence id.')
51
+ @click.option('-i', '--id-file', 'id_file',
52
+ metavar='<id file|stdin>', type=click.File('r'),
53
+ help='Provides an ID file (one id per line) that extracts sequences from the GFF file that '
54
+ '\033[1mmatch\033[0m the IDs in the ID file.')
55
+ @click.option('-a', '--other-infor', 'other_infor', metavar='<str>',
56
+ help='Information to retain in column 9 of the gff file, multiple messages are separated by commas. (eg. Parent,pacid)')
57
+ @click.option('-p', '--num-processing', 'num_processing',
58
+ metavar='<int>', type=int, default=4, show_default=True,
59
+ help='Number of processing.')
60
+ @click.option('-o', '--output-file', 'output_file',
61
+ metavar='<fasta file|stdout>', type=click.File('w'),
62
+ help='Output file, stdout by default.')
63
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
64
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
65
+ def run(gff_file, ref_fasta_file, feature_type, id_file, id_field, other_infor, num_processing, output_file):
66
+ """Extract sequences from GFF file."""
67
+ other_infor = other_infor.split(',') if other_infor else None
68
+ main(
69
+ gff_file=gff_file,
70
+ fa_file=ref_fasta_file,
71
+ feature_type=feature_type,
72
+ id_field=id_field,
73
+ id_file=id_file,
74
+ remain_attr=other_infor,
75
+ num_processing=num_processing,
76
+ out_file=output_file
77
+ )
78
+
79
+
80
+ if __name__ == '__main__':
81
+ run()
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: gff_sort.py
4
+ Description: Sort naturally in sequence according to chromosome, gene id, transcript id, start, and end.
5
+ CreateDate: 2022/3/31
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from io import TextIOWrapper
10
+ import click
11
+ from pybioinformatic import Gff, Displayer
12
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
13
+
14
+
15
+ def main(gff_file: TextIOWrapper,
16
+ out_file: TextIOWrapper = None):
17
+ with Gff(gff_file) as gff:
18
+ click.echo(gff.sort(), out_file)
19
+
20
+
21
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
22
+ @click.argument('gff_file', nargs=1, metavar='<gff file|stdin>', type=click.File('r'), required=True)
23
+ @click.option('-o', '--output_file', 'output_file',
24
+ metavar='<gff file|stdout>', type=click.File('w'),
25
+ help='Output sorted GFF file, stdout by default.')
26
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
27
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
28
+ def run(gff_file, output_file):
29
+ """Sort naturally in sequence according to chromosome, gene id, transcript id, start, and end."""
30
+ main(gff_file, output_file)
31
+
32
+
33
+ if __name__ == '__main__':
34
+ run()
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: rename.py
4
+ Description: Rename feature ID.
5
+ CreateDate: 2025/7/3
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from io import TextIOWrapper
10
+ import click
11
+ from pybioinformatic import Gff, dataframe_to_str, Displayer
12
+
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(
17
+ gff_file: TextIOWrapper,
18
+ species_name: str,
19
+ version: str,
20
+ npartitions: int,
21
+ output_file: TextIOWrapper = None
22
+ ):
23
+ with Gff(gff_file) as gff:
24
+ df = gff.rename(species_name=species_name, version=version, npartitions=npartitions)
25
+ s = dataframe_to_str(df, index=False, header=False)
26
+ for line in s.split('\n'):
27
+ split = line.split('\t')
28
+ if split[-1] != 'NaN':
29
+ attr = f'ID={split[-3]};Name={split[-2]};Parent={split[-1]}'
30
+ else:
31
+ attr = f'ID={split[-3]};Name={split[-2]}'
32
+ split = split[:-3]
33
+ split.append(attr)
34
+ click.echo('\t'.join(split), output_file)
35
+
36
+
37
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
38
+ @click.argument('gff_file', nargs=1, metavar='<gff file|stdin>', type=click.File('r'), required=True)
39
+ @click.option('-s', '--species-name', 'species_name',
40
+ metavar='<str>', required=True,
41
+ help='Species name.')
42
+ @click.option('-v', '--anno-version', 'anno_version',
43
+ metavar='<str>', default='v1.0', show_default=True,
44
+ help='Annotation version.')
45
+ @click.option('-n', '--npartitions', 'npartitions',
46
+ metavar='<int>', type=int, default=4, show_default=True,
47
+ help='The number of partitions to distribute the data into for dask processing.')
48
+ @click.option('-o', '--output-file', 'output_file',
49
+ metavar='<file|stdout>', type=click.File('w'),
50
+ help='Output file, stdout by default.')
51
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
52
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
53
+ def run(gff_file, species_name, anno_version, npartitions, output_file):
54
+ """
55
+ This script provides a standardized feature ID renaming scheme for genome annotation files in GFF format.
56
+ It creates species-specific, chromosome-locating, and structured unique identifiers for different types of
57
+ genomic features in accordance with best practices in bioinformatics, ensuring consistency and traceability of
58
+ IDs throughout the annotation file.\n
59
+ 1.genes:<species_name>.<chr_name>Gxxxxxx.<annotation_version>\n
60
+ 2.mRNAs: <species_name>.<chr_name>Gxxxxxx.x.<annotation_version>\n
61
+ 3.other features: <species_name>.<chr_name>Gxxxxxx.x.<feature_type>.x.<annotation_version>
62
+ """
63
+ main(
64
+ gff_file=gff_file,
65
+ species_name=species_name,
66
+ version=anno_version,
67
+ npartitions=npartitions,
68
+ output_file=output_file
69
+ )
70
+
71
+
72
+ if __name__ == '__main__':
73
+ run()
@@ -0,0 +1,12 @@
1
+ from biofile_kit.gtf_utils.gtf2bed import run as gtf2bed
2
+ from biofile_kit.gtf_utils.gtf_extract_seq import run as gtf_extract_seq
3
+ from biofile_kit.gtf_utils.gtf_sort import run as gtf_sort
4
+
5
+ __version__ = '0.1.0'
6
+
7
+ __all__ = [
8
+ 'gtf2bed',
9
+ 'gtf_extract_seq',
10
+ 'gtf_sort',
11
+ '__version__'
12
+ ]
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ File: gtf2bed.py
4
+ Description: Convert the file format from GTF to BED.
5
+ CreateDate: 2022/4/2
6
+ Author: xuwenlin
7
+ E-mail: wenlinxu.njfu@outlook.com
8
+ """
9
+ from typing import Union
10
+ from io import TextIOWrapper
11
+ import click
12
+ from pybioinformatic import Gtf, Displayer
13
+ displayer = Displayer(__file__.split('/')[-1], version='0.1.0')
14
+
15
+
16
+ def main(gtf_file: Union[str, TextIOWrapper],
17
+ out_file: TextIOWrapper = None):
18
+ with Gtf(gtf_file) as gtf:
19
+ for line in gtf.to_bed():
20
+ click.echo(line, out_file)
21
+
22
+
23
+ @click.command(context_settings=dict(help_option_names=['-h', '--help']))
24
+ @click.option('-i', '--gtf_file', 'gtf_file',
25
+ metavar='<gtf file|stdin>', type=click.File('r'), required=True,
26
+ help='Input GTF file.')
27
+ @click.option('-o', '--output_bed_file', 'output_bed_file',
28
+ metavar='<bed file|stdout>', type=click.File('w'),
29
+ help='Output BED file, stdout by default.')
30
+ @click.option('-V', '--version', 'version', help='Show author and version information.',
31
+ is_flag=True, is_eager=True, expose_value=False, callback=displayer.version_info)
32
+ def run(gtf_file, output_bed_file):
33
+ """Convert the file format from GTF to BED."""
34
+ main(gtf_file, output_bed_file)
35
+
36
+
37
+ if __name__ == '__main__':
38
+ run()