Skip to content

Commit

Permalink
implement length() function (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntachukwu authored Oct 21, 2021
1 parent 5af608b commit 3e287fb
Show file tree
Hide file tree
Showing 30 changed files with 1,452 additions and 12 deletions.
42 changes: 39 additions & 3 deletions wdl2cwl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# WDL-CWL Type Mappings
wdl_type = {
"Array[String]": "string[]",
"String": "string",
"File": "File",
"Int": "int",
Expand Down Expand Up @@ -189,6 +190,42 @@ def get_command(
if data_type != "File":
new_command += "$(" + inputs(i) + ")"

elif "length(" in sub_str:

if ("true" and "false") in sub_str:
true_value = sub_str[
sub_str.find("true") + 5 : sub_str.find("false")
].strip()
temp = sub_str.split("false=")
false_value = temp[1].split(temp[1][0])[1]
comparison_expression = temp[1].split(temp[1][0])[2]
comparison_expression = comparison_expression[7:]
operator = ""
for i in comparison_expression:
if i in ">=!<":
operator += i
input_name = comparison_expression.split(operator)[0][:-1]
value_to_compare = comparison_expression.split(operator)[1]

append_str = (
"${if ("
+ inputs(input_name)
+ ".length "
+ operator
+ " "
+ value_to_compare
+ ") {return "
+ true_value
+ ";} else {return '"
+ false_value
+ "';}}"
)
new_command += append_str
else:
raise ValueError(
"Length function without the if...else keywords is currently not supported"
)

elif ("true" and "false") in sub_str:
true_value = sub_str[
sub_str.find("true") + 5 : sub_str.find("false")
Expand Down Expand Up @@ -260,14 +297,13 @@ def get_command(

if input_name in input_names:
append_str_sub = (
f'("{separator}".join({inputs(input_name)}{temp_append_str}))'
f'({inputs(input_name)}{temp_append_str}.join("{separator}"))'
)

if "?" in data_type and input_name in unbound_input_names:
append_str = f'$({inputs(input_name)} === null ? "" : {(append_str_sub)})'
else:
append_str = f'$("{separator}".join({inputs(input_name)}{temp_append_str}))'

append_str = f'$({inputs(input_name)}{temp_append_str}.join("{separator}"))'
new_command += append_str

elif "sub(" in sub_str:
Expand Down
151 changes: 151 additions & 0 deletions wdl2cwl/tests/cwl_files/bcftools_annotate.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
class: CommandLineTool
id: Annotate
inputs:
- id: inputFile
type: File
- id: annsFile
type:
- File
- 'null'
- id: collapse
type:
- string
- 'null'
- id: exclude
type:
- string
- 'null'
- id: headerLines
type:
- File
- 'null'
- id: newId
type:
- string
- 'null'
- id: include
type:
- string
- 'null'
- id: markSites
type:
- string
- 'null'
- id: regions
type:
- string
- 'null'
- id: regionsFile
type:
- File
- 'null'
- id: renameChrs
type:
- File
- 'null'
- id: samplesFile
type:
- File
- 'null'
- id: columns
default: '[]'
type: string[]
- id: force
default: false
type: boolean
- id: keepSites
default: false
type: boolean
- id: noVersion
default: false
type: boolean
- id: samples
default: '[]'
type: string[]
- id: singleOverlaps
default: false
type: boolean
- id: removeAnns
default: '[]'
type: string[]
- id: outputPath
default: output.vcf.gz
type: string
- id: threads
default: 0
type: int
- id: memory
default: 256M
type: string
- id: dockerImage
default: quay.io/biocontainers/bcftools:1.10.2--h4f4756c_2
type: string
outputs:
- id: outputVcf
type: File
outputBinding:
glob: $(inputs.outputPath)
- id: outputVcfIndex
type:
- File
- 'null'
outputBinding:
glob: $(inputs.outputPath).tbi
requirements:
- class: DockerRequirement
dockerPull: quay.io/biocontainers/bcftools:1.10.2--h4f4756c_2
- class: InitialWorkDirRequirement
listing:
- entryname: example.sh
entry: |4

set -e
mkdir -p "\$(dirname $(inputs.outputPath))"
bcftools annotate \
-o $(inputs.outputPath) \
-O \
$(inputs.annsFile === null ? "" : "--annotations " + inputs.annsFile.path ) \
$(inputs.collapse === null ? "" : "--collapse " + inputs.collapse ) \
${if (inputs.columns.length > 0) {return "--columns";} else {return '';}} $(inputs.columns.join(",")) \
$(inputs.exclude === null ? "" : "--exclude " + inputs.exclude ) \
$(inputs.force ? "--force" : "") \
$(inputs.headerLines === null ? "" : "--header-lines " + inputs.headerLines.path ) \
$(inputs.newId === null ? "" : "--set-id " + inputs.newId ) \
$(inputs.include === null ? "" : "--include " + inputs.include ) \
$(inputs.keepSites ? "--keep-sites" : "") \
$(inputs.markSites === null ? "" : "--mark-sites " + inputs.markSites ) \
$(inputs.noVersion ? "--no-version" : "") \
$(inputs.regions === null ? "" : "--regions " + inputs.regions ) \
$(inputs.regionsFile === null ? "" : "--regions-file " + inputs.regionsFile.path ) \
$(inputs.renameChrs === null ? "" : "--rename-chrs " + inputs.renameChrs.path ) \
${if (inputs.samples.length > 0) {return "--samples";} else {return '';}} $(inputs.samples.join(",")) \
$(inputs.samplesFile === null ? "" : "--samples-file " + inputs.samplesFile.path ) \
$(inputs.singleOverlaps ? "--single-overlaps" : "") \
${if (inputs.removeAnns.length > 0) {return "--remove";} else {return '';}} $(inputs.removeAnns.join(",")) \
$(inputs.inputFile.path)

$(inputs.ifcompressedthen'bcftools index --tbi ~{outputPath)'else''}
- class: InlineJavascriptRequirement
- class: NetworkAccess
networkAccess: true
- class: ResourceRequirement
ramMin: |-
${
var unit = inputs.memory.match(/[a-zA-Z]+/g).join("");
var value = parseInt(inputs.memory.match(/[0-9]+/g));
var memory = "";
if(unit==="KiB") memory = value/1024;
else if(unit==="MiB") memory = value;
else if(unit==="GiB") memory = value*1024;
else if(unit==="TiB") memory = value*1024*1024;
else if(unit==="B") memory = value/(1024*1024);
else if(unit==="KB" || unit==="K") memory = (value*1000)/(1024*1024);
else if(unit==="MB" || unit==="M") memory = (value*(1000*1000))/(1024*1024);
else if(unit==="GB" || unit==="G") memory = (value*(1000*1000*1000))/(1024*1024);
else if(unit==="TB" || unit==="T") memory = (value*(1000*1000*1000*1000))/(1024*1024);
return parseInt(memory);
}
cwlVersion: v1.2
baseCommand:
- bash
- example.sh
166 changes: 166 additions & 0 deletions wdl2cwl/tests/cwl_files/bcftools_stats.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
class: CommandLineTool
id: Stats
inputs:
- id: inputVcf
type: File
- id: inputVcfIndex
type: File
- id: compareVcf
type:
- File
- 'null'
- id: compareVcfIndex
type:
- File
- 'null'
- id: afBins
type:
- string
- 'null'
- id: afTag
type:
- string
- 'null'
- id: collapse
type:
- string
- 'null'
- id: depth
type:
- string
- 'null'
- id: exclude
type:
- string
- 'null'
- id: exons
type:
- File
- 'null'
- id: applyFilters
type:
- string
- 'null'
- id: fastaRef
type:
- File
- 'null'
- id: fastaRefIndex
type:
- File
- 'null'
- id: include
type:
- string
- 'null'
- id: regions
type:
- string
- 'null'
- id: regionsFile
type:
- File
- 'null'
- id: samplesFile
type:
- File
- 'null'
- id: targets
type:
- string
- 'null'
- id: targetsFile
type:
- File
- 'null'
- id: userTsTv
type:
- string
- 'null'
- id: outputPath
default: basename(inputVcf)+.stats
type: string
- id: firstAlleleOnly
default: false
type: boolean
- id: splitByID
default: false
type: boolean
- id: samples
default: '[]'
type: string[]
- id: verbose
default: false
type: boolean
- id: threads
default: 0
type: int
- id: memory
default: 256M
type: string
- id: dockerImage
default: quay.io/biocontainers/bcftools:1.10.2--h4f4756c_2
type: string
outputs:
- id: stats
type: File
outputBinding:
glob: $(inputs.outputPath)
requirements:
- class: DockerRequirement
dockerPull: quay.io/biocontainers/bcftools:1.10.2--h4f4756c_2
- class: InitialWorkDirRequirement
listing:
- entryname: example.sh
entry: |4

set -e
mkdir -p \$(dirname $(inputs.outputPath))
bcftools stats \
$(inputs.afBins === null ? "" : "--af-bins " + inputs.afBins ) \
$(inputs.afTag === null ? "" : "--af-tag " + inputs.afTag ) \
$(inputs.firstAlleleOnly ? "--1st-allele-only" : "") \
$(inputs.collapse === null ? "" : "--collapse " + inputs.collapse ) \
$(inputs.depth === null ? "" : "--depth " + inputs.depth ) \
$(inputs.exclude === null ? "" : "--exclude " + inputs.exclude ) \
$(inputs.exons === null ? "" : "--exons " + inputs.exons.path ) \
$(inputs.applyFilters === null ? "" : "--apply-filters " + inputs.applyFilters ) \
$(inputs.fastaRef === null ? "" : "--fasta-ref " + inputs.fastaRef.path ) \
$(inputs.include === null ? "" : "--include " + inputs.include ) \
$(inputs.splitByID ? "--split-by-ID" : "") \
$(inputs.regions === null ? "" : "--regions " + inputs.regions ) \
$(inputs.regionsFile === null ? "" : "--regions-file " + inputs.regionsFile.path ) \
${if (inputs.samples.length > 0) {return "--samples";} else {return '';}} $(inputs.samples.join(",")) \
$(inputs.samplesFile === null ? "" : "--samples-file " + inputs.samplesFile.path ) \
$(inputs.targets === null ? "" : "--targets " + inputs.targets ) \
$(inputs.targetsFile === null ? "" : "--targets-file " + inputs.targetsFile.path ) \
$(inputs.userTsTv === null ? "" : "--user-tstv " + inputs.userTsTv ) \
--threads $(inputs.threads) \
$(inputs.verbose ? "--verbose" : "") \
$(inputs.inputVcf.path) $(inputs.compareVcf === null ? "" : inputs.compareVcf.path) > $(inputs.outputPath)
- class: InlineJavascriptRequirement
- class: NetworkAccess
networkAccess: true
- class: ResourceRequirement
ramMin: |-
${
var unit = inputs.memory.match(/[a-zA-Z]+/g).join("");
var value = parseInt(inputs.memory.match(/[0-9]+/g));
var memory = "";
if(unit==="KiB") memory = value/1024;
else if(unit==="MiB") memory = value;
else if(unit==="GiB") memory = value*1024;
else if(unit==="TiB") memory = value*1024*1024;
else if(unit==="B") memory = value/(1024*1024);
else if(unit==="KB" || unit==="K") memory = (value*1000)/(1024*1024);
else if(unit==="MB" || unit==="M") memory = (value*(1000*1000))/(1024*1024);
else if(unit==="GB" || unit==="G") memory = (value*(1000*1000*1000))/(1024*1024);
else if(unit==="TB" || unit==="T") memory = (value*(1000*1000*1000*1000))/(1024*1024);
return parseInt(memory);
}
- class: ResourceRequirement
coresMin: $(inputs.threads + 1)
cwlVersion: v1.2
baseCommand:
- bash
- example.sh
2 changes: 1 addition & 1 deletion wdl2cwl/tests/cwl_files/bowtie_1.cwl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ requirements:
--threads $(inputs.threads) \
$(inputs.samRG === null ? "" : "--sam-RG '" + inputs.samRG )$(inputs.samRG === null ? "" : "'") \
$(inputs.indexFiles[0].replace("(\.rev)?\.[0-9]\.ebwt$","")) \
$(",".join(inputs.readsUpstream.map(function(el) { return el.path}))) \
$(inputs.readsUpstream.map(function(el) { return el.path}).join(",")) \
| picard -Xmx$(inputs.picardXmx) SortSam \
INPUT=/dev/stdin \
OUTPUT=$(inputs.outputPath) \
Expand Down
Loading

0 comments on commit 3e287fb

Please sign in to comment.