|
| 1 | +package com.antigenomics.vdjtools.annotate |
| 2 | + |
| 3 | +import com.antigenomics.vdjtools.io.SampleWriter |
| 4 | +import com.antigenomics.vdjtools.misc.CommonUtil |
| 5 | +import com.antigenomics.vdjtools.sample.Sample |
| 6 | +import com.antigenomics.vdjtools.sample.SampleCollection |
| 7 | +import com.antigenomics.vdjtools.sample.SegmentConverter |
| 8 | + |
| 9 | +def cli = new CliBuilder(usage: "SegmentsToFamilies [options] " + |
| 10 | + "[sample1 sample2 ... if not -m] output_prefix") |
| 11 | +cli.h("display help message") |
| 12 | +cli.m(longOpt: "metadata", argName: "filename", args: 1, |
| 13 | + "Metadata file. First and second columns should contain file name and sample id. " + |
| 14 | + "Header is mandatory and will be used to assign column names for metadata.") |
| 15 | +cli.s(longOpt: "species", argName: "name", args: 1, |
| 16 | + "Dataset species, 'human' or 'mouse'.", required: true) |
| 17 | +cli.c(longOpt: "compress", "Compress output sample files.") |
| 18 | + |
| 19 | + |
| 20 | +def opt = cli.parse(args) |
| 21 | + |
| 22 | +if (opt == null) { |
| 23 | + System.exit(2) |
| 24 | +} |
| 25 | + |
| 26 | +if (opt.h) { |
| 27 | + cli.usage() |
| 28 | + System.exit(2) |
| 29 | +} |
| 30 | + |
| 31 | +// Check if metadata is provided |
| 32 | + |
| 33 | +def metadataFileName = opt.m |
| 34 | + |
| 35 | +if (metadataFileName ? opt.arguments().size() != 1 : opt.arguments().size() < 2) { |
| 36 | + if (metadataFileName) |
| 37 | + println "Only output prefix should be provided in case of -m" |
| 38 | + else |
| 39 | + println "At least 1 sample files should be provided if not using -m" |
| 40 | + cli.usage() |
| 41 | + System.exit(2) |
| 42 | +} |
| 43 | + |
| 44 | +def outputFilePrefix = opt.arguments()[-1], |
| 45 | + species = (String) opt.s, |
| 46 | + compress = (boolean) opt.c |
| 47 | + |
| 48 | +if (!["human", "mouse"].any { it.equalsIgnoreCase(species) }) { |
| 49 | + println "Should specify either human or mouse as species." |
| 50 | + System.exit(2) |
| 51 | +} |
| 52 | + |
| 53 | +def scriptName = getClass().canonicalName.split("\\.")[-1] |
| 54 | + |
| 55 | +// |
| 56 | +// Batch load samples |
| 57 | +// |
| 58 | + |
| 59 | +println "[${new Date()} $scriptName] Reading samples" |
| 60 | + |
| 61 | +def sampleCollection = metadataFileName ? |
| 62 | + new SampleCollection((String) metadataFileName) : |
| 63 | + new SampleCollection(opt.arguments()[0..-2]) |
| 64 | + |
| 65 | +println "[${new Date()} $scriptName] ${sampleCollection.size()} samples prepared" |
| 66 | + |
| 67 | +// Load segment conversions |
| 68 | +def vSegmentMap = new HashMap<String, String>(), |
| 69 | + jSegmentMap = new HashMap<String, String>() |
| 70 | +CommonUtil.resourceStreamReader("vj_families.txt").splitEachLine("\t") { |
| 71 | + if (it[0].equalsIgnoreCase(species)) { |
| 72 | + if (it[2].equalsIgnoreCase("v")) { |
| 73 | + vSegmentMap.put(it[3], it[4]) |
| 74 | + } else { |
| 75 | + jSegmentMap.put(it[3], it[4]) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +def converter = new SegmentConverter(vSegmentMap, jSegmentMap) |
| 81 | + |
| 82 | +// |
| 83 | +// Iterate over samples and change V segments |
| 84 | +// |
| 85 | +def sw = new SampleWriter(compress) |
| 86 | + |
| 87 | +sampleCollection.eachWithIndex { sample, ind -> |
| 88 | + def sampleId = sample.sampleMetadata.sampleId |
| 89 | + println "[${new Date()} $scriptName] Changing segments for $sampleId.." |
| 90 | + |
| 91 | + // print output |
| 92 | + sw.writeConventional(new Sample(sample, converter), outputFilePrefix) |
| 93 | +} |
| 94 | + |
| 95 | +sampleCollection.metadataTable.storeWithOutput(outputFilePrefix, compress, |
| 96 | + "segm2fam") |
| 97 | + |
| 98 | +println "[${new Date()} $scriptName] Finished" |
0 commit comments