annotate 0.4.0/bin/process_centrifuge_output.py @ 103:96a34c382154

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