Created
April 6, 2016 13:02
-
-
Save nuada/ffda1ddb201b542a65a863f2e5bdfce4 to your computer and use it in GitHub Desktop.
D vs Python
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
import std.stdio; | |
import std.file; | |
import std.path; | |
import std.process; | |
import std.algorithm; | |
import std.parallelism; | |
void main(string[] args) { | |
int[string] no_of_reads; | |
foreach (i, arg; taskPool.parallel(args[1..args.length])) { | |
if (!arg.isFile()) continue; | |
File file; | |
if (arg[arg.length - 2..arg.length] == "gz") { | |
auto pipe = pipeShell("gunzip -c " ~ arg); | |
file = pipe.stdout; | |
} else { | |
file = File(arg); | |
} | |
auto basename = baseName(arg); | |
no_of_reads[basename] = file.byLine().map!(a => 1).sum() / 4; | |
} | |
foreach (string key; sort(no_of_reads.keys)) { | |
writefln("%s\t%d", key, no_of_reads[key]); | |
} | |
} |
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
#!/usr/bin/python | |
import gzip | |
import mmap | |
import sys | |
import os.path | |
def count_fastq_records(handle): | |
return int(handle.read(-1).count('\n')/4) | |
def sample_name(filename): | |
return '.'.join(os.path.basename(filename).split('.')[0:2]) | |
samples = dict() | |
for filename in sys.argv[1:]: | |
with open(filename, 'r') as f: | |
handle = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) | |
if filename.endswith('.gz'): | |
handle = gzip.GzipFile(mode='r', fileobj=handle) | |
samples[sample_name(filename)] = count_fastq_records(handle) | |
for sample in sorted(samples.keys()): | |
print '%s\t%d' % (sample, samples[sample]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment