Last active
July 27, 2016 13:23
-
-
Save slowkow/2dc57a3fe155b0a22ccb to your computer and use it in GitHub Desktop.
Merge FASTQ files from multiple lanes.
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/env python | |
# encoding: utf8 | |
""" | |
merge_lanes.py | |
Kamil Slowikowski | |
July 17, 2015 | |
Broad Technology labs provides sequencing data in a directory structure like | |
this: | |
C1-123_2012-06-01_2012-06-03 | |
├── 1 | |
│ ├── HCHT1BGXX.1.AGGCAGAA_AGAGGATA.unmapped.1.fastq.gz | |
│ ├── HCHT1BGXX.1.AGGCAGAA_AGAGGATA.unmapped.2.fastq.gz | |
│ ... | |
├── 2 | |
│ ├── HCHT1BGXX.2.AGGCAGAA_AGAGGATA.unmapped.1.fastq.gz | |
│ ├── HCHT1BGXX.2.AGGCAGAA_AGAGGATA.unmapped.2.fastq.gz | |
│ ... | |
... | |
│ | |
└── info | |
├── logs | |
│ ├── generateGetManifest.HCHT1BGXX.0.0.log | |
│ ├── generateGetSite.HCHT1BGXX.0.0.log | |
│ ├── ID_riker-12345-1234567890123.json | |
│ ├── logs.tar.gz | |
│ ├── sendFileEmail.sendGetSiteEmail.HCHT1BGXX.0.0.log | |
│ └── workflow.log | |
└── summary | |
This script reads the JSON file: | |
info/logs/ID_riker-12345-1234567890123.json | |
and prints the Unix commands to merge FASTQ files from multiple lanes: | |
gzip -cd 1/HCHT1BGXX.1.AGGCAGAA_AGAGGATA.unmapped.1.fastq.gz \ | |
2/HCHT1BGXX.2.AGGCAGAA_AGAGGATA.unmapped.1.fastq.gz \ | |
| pigz > sampleAlias.R1.fastq.gz | |
If we execute the commands, we will create new files like this: | |
C1-166_2015-07-02_2015-07-06 | |
├── sampleAlias.R1.fastq.gz | |
└── sampleAlias.R2.fastq.gz | |
LICENSE | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
""" | |
import json | |
import os | |
import argparse | |
# We want to ignore this error: | |
# IOError: [Errno 32] Broken pipe | |
from signal import signal, SIGPIPE, SIG_DFL | |
signal(SIGPIPE,SIG_DFL) | |
def main(): | |
args = get_args() | |
x = json.loads(args.json.read()) | |
sampleAliases = get_sampleAliases(x) | |
for sampleAlias in sampleAliases: | |
for end in (1, 2): | |
fastqs = filenames(x, sampleAlias, end) | |
outform = '{sampleAlias}.R{end}.fastq.gz' | |
d = { | |
'sampleAlias': sampleAlias, | |
'end': end | |
} | |
out = outform.format(**d) | |
cmdform = 'gzip -cd {} | pigz > {}' | |
print cmdform.format(' '.join(fastqs), out) | |
def get_args(): | |
parser = argparse.ArgumentParser( | |
description="""Print commands needed to merge FASTQ files from | |
multiple lanes.""" | |
) | |
parser.add_argument( | |
'json', metavar='FILE', type=argparse.FileType('r'), | |
help='JSON file found in info/logs/*.json' | |
) | |
return parser.parse_args() | |
def filenames(js, sampleAlias, end = 1): | |
"""Returns a list of filenames corresponding to a sampleAlias. | |
""" | |
fname = '{lane}/{flowcellBarcode}.{lane}.{barcode1}_{barcode2}.unmapped.{end}.fastq.gz' | |
flowcellBarcode = js['workflow']['flowcellBarcode'] | |
lanes = js['workflow']['lanes'] | |
retval = [] | |
for lane_i, lane in enumerate(lanes, 1): | |
libraries = lane['libraries'] | |
library_i = 0 | |
for i, x in enumerate(libraries): | |
if x['sampleAlias'] == sampleAlias: | |
library_i = i | |
library = libraries[library_i] | |
barcode1, barcode2 = library['molecularBarcodeSequences'] | |
d = { | |
'flowcellBarcode': flowcellBarcode, | |
'lane': lane_i, | |
'barcode1': barcode1, | |
'barcode2': barcode2, | |
'end': end | |
} | |
filename = fname.format(**d) | |
assert os.path.isfile(filename) | |
retval.append(fname.format(**d)) | |
return retval | |
def get_sampleAliases(js): | |
"""Returns a list of sampleAliases. | |
""" | |
retval = [] | |
for lane in js['workflow']['lanes']: | |
for library in lane['libraries']: | |
sampleAlias = library['sampleAlias'] | |
if not sampleAlias in retval: | |
retval.append(sampleAlias) | |
return retval | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment