-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from maxibor/kraken
Adding Kraken2 metagenomics classifier
- Loading branch information
Showing
10 changed files
with
347 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
import argparse | ||
import csv | ||
|
||
|
||
def _get_args(): | ||
'''This function parses and return arguments passed in''' | ||
parser = argparse.ArgumentParser( | ||
prog='kraken_parse', | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
description='Parsing kraken') | ||
parser.add_argument('krakenReport', help="path to kraken report file") | ||
parser.add_argument( | ||
'-c', | ||
dest="count", | ||
default=50, | ||
help="Minimum number of hits on clade to report it. Default = 50") | ||
parser.add_argument( | ||
'-o', | ||
dest="output", | ||
default=None, | ||
help="Output file. Default = <basename>.kraken_parsed.csv") | ||
|
||
args = parser.parse_args() | ||
|
||
infile = args.krakenReport | ||
countlim = int(args.count) | ||
outfile = args.output | ||
|
||
return(infile, countlim, outfile) | ||
|
||
|
||
def _get_basename(file_name): | ||
if ("/") in file_name: | ||
basename = file_name.split("/")[-1].split(".")[0] | ||
else: | ||
basename = file_name.split(".")[0] | ||
return(basename) | ||
|
||
|
||
def parse_kraken(infile, countlim): | ||
''' | ||
INPUT: | ||
infile (str): path to kraken report file | ||
countlim (int): lowest count threshold to report hit | ||
OUTPUT: | ||
resdict (dict): key=taxid, value=readCount | ||
''' | ||
with open(infile, 'r') as f: | ||
resdict = {} | ||
csvreader = csv.reader(f, delimiter='\t') | ||
for line in csvreader: | ||
reads = int(line[1]) | ||
if reads >= countlim: | ||
taxid = line[4] | ||
resdict[taxid] = reads | ||
return(resdict) | ||
|
||
|
||
def write_output(resdict, infile, outfile): | ||
with open(outfile, 'w') as f: | ||
basename = _get_basename(infile) | ||
f.write(f"TAXID,{basename}\n") | ||
for akey in resdict.keys(): | ||
f.write(f"{akey},{resdict[akey]}\n") | ||
|
||
|
||
if __name__ == '__main__': | ||
INFILE, COUNTLIM, outfile = _get_args() | ||
|
||
if not outfile: | ||
outfile = _get_basename(INFILE)+".kraken_parsed.csv" | ||
|
||
tmp_dict = parse_kraken(infile=INFILE, countlim=COUNTLIM) | ||
write_output(resdict=tmp_dict, infile=INFILE, outfile=outfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
|
||
import argparse | ||
import os | ||
import pandas as pd | ||
import numpy as np | ||
|
||
|
||
def _get_args(): | ||
'''This function parses and return arguments passed in''' | ||
parser = argparse.ArgumentParser( | ||
prog='merge_kraken_res', | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
description='Merging csv count files in one table') | ||
parser.add_argument( | ||
'-o', | ||
dest="output", | ||
default="kraken_count_table.csv", | ||
help="Output file. Default = kraken_count_table.csv") | ||
|
||
args = parser.parse_args() | ||
|
||
outfile = args.output | ||
|
||
return(outfile) | ||
|
||
|
||
def get_csv(): | ||
tmp = [i for i in os.listdir() if ".csv" in i] | ||
return(tmp) | ||
|
||
|
||
def _get_basename(file_name): | ||
if ("/") in file_name: | ||
basename = file_name.split("/")[-1].split(".")[0] | ||
else: | ||
basename = file_name.split(".")[0] | ||
return(basename) | ||
|
||
|
||
def merge_csv(all_csv): | ||
df = pd.read_csv(all_csv[0], index_col=0) | ||
for i in range(1, len(all_csv)): | ||
df_tmp = pd.read_csv(all_csv[i], index_col=0) | ||
df = pd.merge(left=df, right=df_tmp, on='TAXID', how='outer') | ||
df.fillna(0, inplace=True) | ||
return(df) | ||
|
||
|
||
def write_csv(pd_dataframe, outfile): | ||
pd_dataframe.to_csv(outfile) | ||
|
||
|
||
if __name__ == "__main__": | ||
OUTFILE = _get_args() | ||
all_csv = get_csv() | ||
resdf = merge_csv(all_csv) | ||
write_csv(resdf, OUTFILE) | ||
print(resdf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* ------------------------------------------------- | ||
* Nextflow config file for running tests | ||
* ------------------------------------------------- | ||
* Defines bundled input files and everything required | ||
* to run a fast and simple test. Use as follows: | ||
* nextflow run nf-core/eager -profile test, docker (or singularity, or conda) | ||
*/ | ||
|
||
params { | ||
config_profile_name = 'Test profile kraken' | ||
config_profile_description = 'Minimal test dataset to check pipeline function with kraken metagenomic profiler' | ||
// Limit resources so that this can run on Travis | ||
max_cpus = 2 | ||
max_memory = 6.GB | ||
max_time = 48.h | ||
genome = false | ||
//Input data | ||
single_end = false | ||
metagenomic_tool = 'kraken' | ||
run_metagenomic_screening = true | ||
readPaths = [['JK2782_TGGCCGATCAACGA_L008', ['https://github.com/nf-core/test-datasets/raw/eager/testdata/Mammoth/fastq/JK2782_TGGCCGATCAACGA_L008_R1_001.fastq.gz.tengrand.fq.gz','https://github.com/nf-core/test-datasets/raw/eager/testdata/Mammoth/fastq/JK2782_TGGCCGATCAACGA_L008_R2_001.fastq.gz.tengrand.fq.gz']], | ||
['JK2802_AGAATAACCTACCA_L008', ['https://github.com/nf-core/test-datasets/raw/eager/testdata/Mammoth/fastq/JK2802_AGAATAACCTACCA_L008_R1_001.fastq.gz.tengrand.fq.gz','https://github.com/nf-core/test-datasets/raw/eager/testdata/Mammoth/fastq/JK2802_AGAATAACCTACCA_L008_R2_001.fastq.gz.tengrand.fq.gz']], | ||
] | ||
// Genome references | ||
fasta = 'https://raw.githubusercontent.com/nf-core/test-datasets/eager/reference/Mammoth/Mammoth_MT_Krause.fasta' | ||
database = 'https://github.com/nf-core/test-datasets/raw/eager/databases/kraken/eager_test.tar.gz' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.