annotate 0.2.1/bin/process_centrifuge_output.py @ 0:77494b0fa3c7

"planemo upload"
author kkonganti
date Mon, 27 Jun 2022 15:55:37 -0400
parents
children
rev   line source
kkonganti@0 1 #!/usr/bin/env python
kkonganti@0 2
kkonganti@0 3 import os
kkonganti@0 4 import argparse
kkonganti@0 5 import logging as log
kkonganti@0 6 import pandas as pd
kkonganti@0 7 import numpy as np
kkonganti@0 8 from Bio import SeqIO
kkonganti@0 9
kkonganti@0 10
kkonganti@0 11 def main():
kkonganti@0 12 # READ IN ARGUMENTS
kkonganti@0 13 desc = """
kkonganti@0 14 This script is part of the centriflaken pipeline: It processes centrifuge
kkonganti@0 15 output and produces either a filtered FASTQ or a text file of FASTQ IDs based
kkonganti@0 16 on the supplied taxa/bug
kkonganti@0 17 """
kkonganti@0 18 parser = argparse.ArgumentParser(prog='process_centrifuge_output.py', description=desc)
kkonganti@0 19 parser.add_argument("-v", dest='verbose', action="store_true", help="For more verbose output")
kkonganti@0 20 parser.add_argument("-i", dest='input_fastq', required=False,
kkonganti@0 21 help="Path to input FASTQ file (same as input to centrifuge). If not mentioned, \
kkonganti@0 22 a text file of sequence IDs are produced instead of a FASTQ file")
kkonganti@0 23 parser.add_argument("-t", dest='taxa_filtered_fastq_file', required=True,
kkonganti@0 24 help="Path to output FASTQ or output text file filtered by the taxa specified")
kkonganti@0 25 parser.add_argument("-r", dest='cent_report', required=True, help="Path to centrifuge report")
kkonganti@0 26 parser.add_argument("-o", dest='cent_output', required=True, help="Path to centrifuge output")
kkonganti@0 27 parser.add_argument("-b", dest='bug', required=True,
kkonganti@0 28 help="Name or fragment of name of the bug by which reads are extracted")
kkonganti@0 29 args = parser.parse_args()
kkonganti@0 30
kkonganti@0 31 # MORE INFO IF VERBOSE
kkonganti@0 32 if args.verbose:
kkonganti@0 33 log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
kkonganti@0 34 else:
kkonganti@0 35 log.basicConfig(format="%(levelname)s: %(message)s")
kkonganti@0 36
kkonganti@0 37 # ASSIGN VARIABLES
kkonganti@0 38 input_fastq = args.input_fastq
kkonganti@0 39 taxa_filtered_fastq_file = args.taxa_filtered_fastq_file
kkonganti@0 40 cent_report = args.cent_report
kkonganti@0 41 cent_output = args.cent_output
kkonganti@0 42 bug = args.bug
kkonganti@0 43 report_col_list = ["name", "taxID"]
kkonganti@0 44 output_col_list = ["taxID", "readID"]
kkonganti@0 45
kkonganti@0 46 # Match and filter taxa names and ids from centrifuge report file
kkonganti@0 47 report_df = pd.read_csv(cent_report, delimiter="\t", usecols=report_col_list)
kkonganti@0 48 report_df['name'] = report_df['name'].str.lower()
kkonganti@0 49 filt_report_df = report_df[report_df['name'].str.contains(bug.lower())]
kkonganti@0 50 #print("\nMatching taxa names and ids:\n",filt_report_df)
kkonganti@0 51 taxID_list = filt_report_df['taxID']
kkonganti@0 52
kkonganti@0 53 # Match the above tax ids to read ids from centrifuge output file and deduplicate
kkonganti@0 54 output_df = pd.read_csv(cent_output, delimiter="\t", usecols=output_col_list)
kkonganti@0 55 filt_output_df = output_df.loc[output_df['taxID'].isin(taxID_list)]
kkonganti@0 56 readID_list = filt_output_df['readID']
kkonganti@0 57 readID_dedup_list = np.unique(readID_list)
kkonganti@0 58 TF=open(taxa_filtered_fastq_file, "w")
kkonganti@0 59
kkonganti@0 60 if (not input_fastq):
kkonganti@0 61 # print("\nFILTERED READ ID LIST:\n", readID_dedup_list)
kkonganti@0 62 for ID in readID_dedup_list:
kkonganti@0 63 TF.write(f"{ID}\n")
kkonganti@0 64 else:
kkonganti@0 65 # Extract filtered reads from input fastq and write to output fastq
kkonganti@0 66 print ("Indexing reads..")
kkonganti@0 67 rec = SeqIO.index(input_fastq,"fastq")
kkonganti@0 68 for i in readID_dedup_list:
kkonganti@0 69 if i in rec:
kkonganti@0 70 SeqIO.write(rec[i], TF, "fastq")
kkonganti@0 71
kkonganti@0 72 TF.close()
kkonganti@0 73
kkonganti@0 74 if __name__ == "__main__":
kkonganti@0 75 main()