Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add aggregate_spike_count to reports.py #955

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import glob
import os
import time
from collections import OrderedDict
from collections import OrderedDict, defaultdict
import csv
import math
import shutil
Expand Down Expand Up @@ -453,6 +453,50 @@ def parser_consolidate_spike_count(parser=argparse.ArgumentParser()):
__commands__.append(('consolidate_spike_count', parser_consolidate_spike_count))


def aggregate_spike_count(inDir, outFile):
'''aggregate multiple spike count reports into one.'''
spike_in_sample_counts = defaultdict(dict) # For a given spikein ID, map to sample name and corresponding count
samples_seen = []
with open(outFile, 'wt') as outf:
for fn in glob.glob(inDir+"*.spike_count.txt"):# os.listdir():
#fn = os.path.join(inDir, fn)
s = os.path.basename(fn)
if not s.endswith('.spike_count.txt'):
raise Exception()
if s.find("ERCC"):
s=s[:s.find("ERCC")-1]
else:
s = s[:-len('.spike_count.txt')]
if s not in samples_seen:
samples_seen.append(s)
with open(fn, 'rt') as inf:
for line in inf:
if not line.startswith('Input bam') and not line.startswith('*'):
spike, count = [line.strip().split('\t')[i] for i in [0,2]]
spike_in_sample_counts[spike][s] = count
#outf.write('\t'.join([s, spike, count]) + '\n')
outf.write("\t".join(["spike-in"]+samples_seen)+"\n")
for spike in spike_in_sample_counts.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted IDs or OrderedDict?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

row = []
row.append(spike)
for s in samples_seen:
if s in spike_in_sample_counts[spike]:
row.append(spike_in_sample_counts[spike][s])
else:
row.append("0")
outf.write("\t".join(row)+"\n")


def parser_aggregate_spike_count(parser=argparse.ArgumentParser()):
parser.add_argument('inDir', help='Input spike count directory.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metavar these so variables can avoid camelcasing?

parser.add_argument('outFile', help='Output report file.')
util.cmd.attach_main(parser, aggregate_spike_count, split_args=True)
return parser


__commands__.append(('aggregate_spike_count', parser_aggregate_spike_count))


# =========================


Expand Down