#!/usr/bin/python
#
# Copyright (c) 2012 Mikkel Schubert <MSchubert@snm.ku.dk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is 
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# 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 OR COPYRIGHT HOLDERS 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.
#
import os
import sys

from pypeline.common.text import padded_table
                     
def read_alignment_records(root):
    with open(os.path.join(root, "SampleSheet.csv")) as records:
        header = records.readline().strip().split(",")
        for line in records:
            yield dict(zip(header, line.strip().split(",")))


def main(argv):
    lines = []
    for root in argv:
        for record in read_alignment_records(root):
            record["Lane"] = int(record["Lane"])
            line = [record["SampleID"],
                    record["SampleID"],
                    record["Index"],
                    record["FCID"],
                    "Illumina",
                    os.path.join(root, "%(SampleID)s_%(Index)s_L%(Lane)03i_R{Pair}_*.fastq.gz" % record)]
            lines.append(line)
    lines.sort(key = lambda v: map(str.lower, v))
    lines.insert(0, ["Name", "Sample", "Library", "Barcode", "Platform", "Path"])

    for line in padded_table(lines):
        print line


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

@@ -1156,16 +1156,57 @@
 ort sys%0A
+import datetime%0A%0Aimport pypeline.ui as ui
 %0Afrom py
@@ -1248,114 +1248,1502 @@
 ble%0A
-                     %0Adef read_alignment_records(root):%0A    with open(os.path.join(root, %22SampleSheet.csv%22
+%0A%0A_HEADER = %5C%0A%22%22%22# Timestamp: %25s%0A# %0A# Columns:%0A#     Name:     Name of the target. Final BAM filenames will consist of this%0A#               name and the name of the BWA prefix joined by a dot. For%0A#               example, given the name 'foobar' and the BWA prefix %0A#               'path/to/indices/hg19', the final BAM files will be named%0A#               'foobar.hg19.bam' and 'foobar.hg19.unaligned.bam'.%0A#     Sample:%0A#     Library:  %0A#     Barcode:  The Sample, Library, and Barcode (or other unique ID) of the%0A#               lane. This is saved in the 'SM', 'LB' and 'PU'. %0A#     Platform: The sequencing platform used to generate this data. %0A#     Path:     Path to FASTQ files. This uses the Python 'glob' module to find%0A#               files, and may therefore contain wildcards (*, %5B...%5D). For PE%0A#               reads, replace the mate number with %22%7BPair%7D%22. %0A#               For example%0A#                  /path/to/files/reads_R%7BPair%7D_*.fastq.gz%0A#%0A# Data is organized by Name/Sample/Library/Barcode. Therefore, this combination%0A# of columns MUST be unique. To prevent duplicate removal being done on multiple %0A# different samples, it is enforced that all libraries are assosiated with ONE %0A# sample only for any one target. Finally, any input file may only be specified%0A# ONCE. The pipeline will not run if any of these assumptions are violated.%0A#%0A#%22%22%22%0A%0A%0A_FILENAME = %22SampleSheet.csv%22%0A                     %0Adef read_alignment_records(root):%0A    with open(os.path.join(root, _FILENAME
 )) a
@@ -3517,59 +3517,556 @@
     
-for line in padded_table(lines):%0A        print line
+print _HEADER %25 datetime.datetime.now().isoformat()%0A    for line in padded_table(lines):%0A        print line%0A%0A    if not argv:%0A        ui.print_info(%22No directories specified, empty table printed:%22, file = sys.stderr)%0A        ui.print_info(%22%5CtUsage: %25s %5Bdirectory ...%5D%22 %25 sys.argv%5B0%5D, file = sys.stderr)%0A        ui.print_info(%22Each directory must contain a '%25s' file.%22 %25 _FILENAME, file = sys.stderr)%0A    else:%0A        ui.print_info(%22Makefile printed. Please check for correctness and update Path column before running pipeline.%22, file = sys.stderr)
 %0A%0A%0Ai

Add documentation to generated mkfiles.