annotate vsnp_add_zero_coverage.py @ 2:a52b819aa990 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 94e69abb568077267eb8b15ef624624e2899a750"
author iuc
date Wed, 30 Sep 2020 19:07:23 +0000
parents 0bc0009f9ea0
children efb86aade548
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
2
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
3 import argparse
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
4 import multiprocessing
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
5 import os
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
6 import queue
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
7 import re
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
8 import shutil
2
a52b819aa990 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 94e69abb568077267eb8b15ef624624e2899a750"
iuc
parents: 1
diff changeset
9
a52b819aa990 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 94e69abb568077267eb8b15ef624624e2899a750"
iuc
parents: 1
diff changeset
10 import pandas
a52b819aa990 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 94e69abb568077267eb8b15ef624624e2899a750"
iuc
parents: 1
diff changeset
11 import pysam
1
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
12 from Bio import SeqIO
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
13
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
14 INPUT_BAM_DIR = 'input_bam_dir'
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
15 INPUT_VCF_DIR = 'input_vcf_dir'
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
16 OUTPUT_VCF_DIR = 'output_vcf_dir'
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
17 OUTPUT_METRICS_DIR = 'output_metrics_dir'
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
18
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
19
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
20 def get_base_file_name(file_path):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
21 base_file_name = os.path.basename(file_path)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
22 if base_file_name.find(".") > 0:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
23 # Eliminate the extension.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
24 return os.path.splitext(base_file_name)[0]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
25 elif base_file_name.endswith("_vcf"):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
26 # The "." character has likely
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
27 # changed to an "_" character.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
28 return base_file_name.rstrip("_vcf")
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
29 return base_file_name
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
30
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
31
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
32 def get_coverage_and_snp_count(task_queue, reference, output_metrics, output_vcf, timeout):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
33 while True:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
34 try:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
35 tup = task_queue.get(block=True, timeout=timeout)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
36 except queue.Empty:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
37 break
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
38 bam_file, vcf_file = tup
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
39 # Create a coverage dictionary.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
40 coverage_dict = {}
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
41 coverage_list = pysam.depth(bam_file, split_lines=True)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
42 for line in coverage_list:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
43 chrom, position, depth = line.split('\t')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
44 coverage_dict["%s-%s" % (chrom, position)] = depth
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
45 # Convert it to a data frame.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
46 coverage_df = pandas.DataFrame.from_dict(coverage_dict, orient='index', columns=["depth"])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
47 # Create a zero coverage dictionary.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
48 zero_dict = {}
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
49 for record in SeqIO.parse(reference, "fasta"):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
50 chrom = record.id
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
51 total_len = len(record.seq)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
52 for pos in list(range(1, total_len + 1)):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
53 zero_dict["%s-%s" % (str(chrom), str(pos))] = 0
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
54 # Convert it to a data frame with depth_x
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
55 # and depth_y columns - index is NaN.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
56 zero_df = pandas.DataFrame.from_dict(zero_dict, orient='index', columns=["depth"])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
57 coverage_df = zero_df.merge(coverage_df, left_index=True, right_index=True, how='outer')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
58 # depth_x "0" column no longer needed.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
59 coverage_df = coverage_df.drop(columns=['depth_x'])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
60 coverage_df = coverage_df.rename(columns={'depth_y': 'depth'})
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
61 # Covert the NaN to 0 coverage and get some metrics.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
62 coverage_df = coverage_df.fillna(0)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
63 coverage_df['depth'] = coverage_df['depth'].apply(int)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
64 total_length = len(coverage_df)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
65 average_coverage = coverage_df['depth'].mean()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
66 zero_df = coverage_df[coverage_df['depth'] == 0]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
67 total_zero_coverage = len(zero_df)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
68 total_coverage = total_length - total_zero_coverage
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
69 genome_coverage = "{:.2%}".format(total_coverage / total_length)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
70 # Process the associated VCF input.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
71 column_names = ["CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO", "FORMAT", "Sample"]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
72 vcf_df = pandas.read_csv(vcf_file, sep='\t', header=None, names=column_names, comment='#')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
73 good_snp_count = len(vcf_df[(vcf_df['ALT'].str.len() == 1) & (vcf_df['REF'].str.len() == 1) & (vcf_df['QUAL'] > 150)])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
74 base_file_name = get_base_file_name(vcf_file)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
75 if total_zero_coverage > 0:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
76 header_file = "%s_header.csv" % base_file_name
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
77 with open(header_file, 'w') as outfile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
78 with open(vcf_file) as infile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
79 for line in infile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
80 if re.search('^#', line):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
81 outfile.write("%s" % line)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
82 vcf_df_snp = vcf_df[vcf_df['REF'].str.len() == 1]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
83 vcf_df_snp = vcf_df_snp[vcf_df_snp['ALT'].str.len() == 1]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
84 vcf_df_snp['ABS_VALUE'] = vcf_df_snp['CHROM'].map(str) + "-" + vcf_df_snp['POS'].map(str)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
85 vcf_df_snp = vcf_df_snp.set_index('ABS_VALUE')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
86 cat_df = pandas.concat([vcf_df_snp, zero_df], axis=1, sort=False)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
87 cat_df = cat_df.drop(columns=['CHROM', 'POS', 'depth'])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
88 cat_df[['ID', 'ALT', 'QUAL', 'FILTER', 'INFO']] = cat_df[['ID', 'ALT', 'QUAL', 'FILTER', 'INFO']].fillna('.')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
89 cat_df['REF'] = cat_df['REF'].fillna('N')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
90 cat_df['FORMAT'] = cat_df['FORMAT'].fillna('GT')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
91 cat_df['Sample'] = cat_df['Sample'].fillna('./.')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
92 cat_df['temp'] = cat_df.index.str.rsplit('-', n=1)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
93 cat_df[['CHROM', 'POS']] = pandas.DataFrame(cat_df.temp.values.tolist(), index=cat_df.index)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
94 cat_df = cat_df[['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO', 'FORMAT', 'Sample']]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
95 cat_df['POS'] = cat_df['POS'].astype(int)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
96 cat_df = cat_df.sort_values(['CHROM', 'POS'])
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
97 body_file = "%s_body.csv" % base_file_name
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
98 cat_df.to_csv(body_file, sep='\t', header=False, index=False)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
99 if output_vcf is None:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
100 output_vcf_file = os.path.join(OUTPUT_VCF_DIR, "%s.vcf" % base_file_name)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
101 else:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
102 output_vcf_file = output_vcf
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
103 with open(output_vcf_file, "w") as outfile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
104 for cf in [header_file, body_file]:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
105 with open(cf, "r") as infile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
106 for line in infile:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
107 outfile.write("%s" % line)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
108 else:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
109 if output_vcf is None:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
110 output_vcf_file = os.path.join(OUTPUT_VCF_DIR, "%s.vcf" % base_file_name)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
111 else:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
112 output_vcf_file = output_vcf
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
113 shutil.copyfile(vcf_file, output_vcf_file)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
114 bam_metrics = [base_file_name, "", "%4f" % average_coverage, genome_coverage]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
115 vcf_metrics = [base_file_name, str(good_snp_count), "", ""]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
116 if output_metrics is None:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
117 output_metrics_file = os.path.join(OUTPUT_METRICS_DIR, "%s.tabular" % base_file_name)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
118 else:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
119 output_metrics_file = output_metrics
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
120 metrics_columns = ["File", "Number of Good SNPs", "Average Coverage", "Genome Coverage"]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
121 with open(output_metrics_file, "w") as fh:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
122 fh.write("# %s\n" % "\t".join(metrics_columns))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
123 fh.write("%s\n" % "\t".join(bam_metrics))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
124 fh.write("%s\n" % "\t".join(vcf_metrics))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
125 task_queue.task_done()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
126
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
127
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
128 def set_num_cpus(num_files, processes):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
129 num_cpus = int(multiprocessing.cpu_count())
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
130 if num_files < num_cpus and num_files < processes:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
131 return num_files
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
132 if num_cpus < processes:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
133 half_cpus = int(num_cpus / 2)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
134 if num_files < half_cpus:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
135 return num_files
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
136 return half_cpus
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
137 return processes
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
138
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
139
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
140 if __name__ == '__main__':
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
141 parser = argparse.ArgumentParser()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
142
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
143 parser.add_argument('--output_metrics', action='store', dest='output_metrics', required=False, default=None, help='Output metrics text file')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
144 parser.add_argument('--output_vcf', action='store', dest='output_vcf', required=False, default=None, help='Output VCF file')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
145 parser.add_argument('--reference', action='store', dest='reference', help='Reference dataset')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
146 parser.add_argument('--processes', action='store', dest='processes', type=int, help='User-selected number of processes to use for job splitting')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
147
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
148 args = parser.parse_args()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
149
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
150 # The assumption here is that the list of files
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
151 # in both INPUT_BAM_DIR and INPUT_VCF_DIR are
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
152 # equal in number and named such that they are
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
153 # properly matched if the directories contain
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
154 # more than 1 file (i.e., hopefully the bam file
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
155 # names and vcf file names will be something like
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
156 # Mbovis-01D6_* so they can be # sorted and properly
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
157 # associated with each other).
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
158 bam_files = []
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
159 for file_name in sorted(os.listdir(INPUT_BAM_DIR)):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
160 file_path = os.path.abspath(os.path.join(INPUT_BAM_DIR, file_name))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
161 bam_files.append(file_path)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
162 vcf_files = []
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
163 for file_name in sorted(os.listdir(INPUT_VCF_DIR)):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
164 file_path = os.path.abspath(os.path.join(INPUT_VCF_DIR, file_name))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
165 vcf_files.append(file_path)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
166
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
167 multiprocessing.set_start_method('spawn')
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
168 queue1 = multiprocessing.JoinableQueue()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
169 num_files = len(bam_files)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
170 cpus = set_num_cpus(num_files, args.processes)
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
171 # Set a timeout for get()s in the queue.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
172 timeout = 0.05
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
173
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
174 # Add each associated bam and vcf file pair to the queue.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
175 for i, bam_file in enumerate(bam_files):
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
176 vcf_file = vcf_files[i]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
177 queue1.put((bam_file, vcf_file))
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
178
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
179 # Complete the get_coverage_and_snp_count task.
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
180 processes = [multiprocessing.Process(target=get_coverage_and_snp_count, args=(queue1, args.reference, args.output_metrics, args.output_vcf, timeout, )) for _ in range(cpus)]
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
181 for p in processes:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
182 p.start()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
183 for p in processes:
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
184 p.join()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
185 queue1.join()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
186
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
187 if queue1.empty():
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
188 queue1.close()
0bc0009f9ea0 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 6a0c9a857c1f4638ef18e106b1f8c0681303acc5"
iuc
parents:
diff changeset
189 queue1.join_thread()