{
  "class": "Workflow",
  "steps": [
    {
      "id": "#Transcribe",
      "run": {
        "hints": [
          {
            "class": "sbg:CPURequirement",
            "value": 1
          },
          {
            "class": "sbg:MemRequirement",
            "value": 1000
          },
          {
            "class": "DockerRequirement",
            "dockerImageId": "",
            "dockerPull": "rfranklin/pythondev"
          }
        ],
        "description": "This project was created to demonstrate the use of argparse to create CLI tools in Python, wrap them using CWL/SDK.v2, and running them.\n\nThis tool takes a TXT file with a DNA sequence and converts to an RNA sequence.",
        "outputs": [
          {
            "outputBinding": {
              "glob": "*.txt"
            },
            "id": "#output_file_glob",
            "type": [
              "File"
            ]
          }
        ],
        "temporaryFailCodes": [],
        "baseCommand": [
          "python",
          "transcribe_argparse.py"
        ],
        "requirements": [
          {
            "class": "CreateFileRequirement",
            "fileDef": [
              {
                "filename": "transcribe_argparse.py",
                "fileContent": "#!/usr/bin/env/python\nimport argparse\nimport re\nimport sys\n\ndef transcribe(args):\n\t# create a transcription map and use regex to translate\n\tmap = {\"A\":\"U\", \"T\":\"A\", \"C\":\"G\", \"G\":\"C\"}\n\tmap = dict((re.escape(k), v) for k, v in map.iteritems())\n\tpattern = re.compile(\"|\".join(map.keys()))\n\tDNA = args['dna'].read().strip()\n\tmRNA = pattern.sub(lambda m: map[re.escape(m.group(0))], DNA)\n\n\t# write a verbose output to stderr and just mRNA to sdtout \n\tif args['verbose']:\n\t\tsys.stderr.write(\"Your original DNA sequence: \" + DNA + \"\\n\")\n\t\tsys.stderr.write(\"Your translated mRNA sequence: \" + mRNA + \"\\n\")\n\tsys.stdout.write(mRNA + '\\n')\n\tsys.exit(0)\n\treturn mRNA\n\nif __name__ == \"__main__\":\n\t\"\"\" Parse the command line arguments \"\"\"\n\tparser = argparse.ArgumentParser()\n\tparser.add_argument(\"-d\", \"--dna\", type=argparse.FileType(\"r\"), default=sys.stdin)\n\tparser.add_argument(\"-v\", \"--verbose\", action=\"store_true\", default=False)\n\t# By setting args as var(...), it becomes a dict, so 'dna' is a key\n\t# Alternative use: args = parser.parse_args(), and 'dna' is an attr of args!\n\t# You must change how you call the args you parse based on this usage! \n\targs = vars(parser.parse_args())\n\n\t\"\"\" Run the desired methods \"\"\"\n\ttranscribe(args)"
              }
            ]
          },
          {
            "class": "ExpressionEngineRequirement",
            "id": "#cwl-js-engine",
            "requirements": [
              {
                "class": "DockerRequirement",
                "dockerPull": "rabix/js-engine"
              }
            ]
          }
        ],
        "stdin": "",
        "arguments": [],
        "inputs": [
          {
            "description": "Input file",
            "inputBinding": {
              "position": 3,
              "prefix": "-d",
              "separate": true
            },
            "id": "#input_file",
            "type": [
              "File"
            ]
          },
          {
            "inputBinding": {
              "position": 4,
              "prefix": "--verbose",
              "separate": true
            },
            "id": "#verbose",
            "type": [
              "null",
              "boolean"
            ]
          },
          {
            "description": "Specify output filename",
            "id": "#output_filename",
            "type": [
              "null",
              "string"
            ]
          }
        ],
     
        "stdout": {
          "class": "Expression",
          "script": "($job.inputs.output_filename || 'rna') + '.txt'",
          "engine": "#cwl-js-engine"
        },
        "successCodes": [],
        "class": "CommandLineTool",
        "label": "Transcribe",
        "id": "https://cgc-api.sbgenomics.com/gauravCGC/fog/transcribe-argparse/16/raw/"
      },
      "inputs": [
        {
          "id": "#Transcribe.input_file",
          "source": [
            "#input_file"
          ]
        },
        {
          "id": "#Transcribe.verbose",
          "default": true
        },
        {
          "id": "#Transcribe.output_filename"
        }
      ],
      "outputs": [
        {
          "id": "#Transcribe.output_file_glob"
        }
      ]
    },
    {
      "id": "#Translate",
      "run": {
        "stdout": {
          "class": "Expression",
          "script": "($job.inputs.output_filename || 'protein') + '.txt'",
          "engine": "#cwl-js-engine"
        },
        "description": "This tool allows you to take an RNA sequence and translate to a protein product based on the first detected ORF.",
        "outputs": [
          {
            "description": "Output protein sequence",
            "outputBinding": {
              "glob": "*.txt"
            },
            "id": "#output_protein",
            "type": [
              "File"
            ]
          }
        ],
        "baseCommand": [
          "python",
          "translate.py"
        ],
        "requirements": [
          {
            "class": "CreateFileRequirement",
            "fileDef": [
              {
                "filename": "translate.py",
                "fileContent": "#!/usr/bin/env/python\nimport argparse\nimport sys\n\ndef translate(args):\n\tmRNA = args['mRNA'].read().strip()\n\tcodon_map = {\"UUU\":\"F\", \"UUC\":\"F\", \"UUA\":\"L\", \"UUG\":\"L\",\n    \"UCU\":\"S\", \"UCC\":\"S\", \"UCA\":\"S\", \"UCG\":\"S\",\n    \"UAU\":\"Y\", \"UAC\":\"Y\", \"UAA\":\"STOP\", \"UAG\":\"STOP\",\n    \"UGU\":\"C\", \"UGC\":\"C\", \"UGA\":\"STOP\", \"UGG\":\"W\",\n    \"CUU\":\"L\", \"CUC\":\"L\", \"CUA\":\"L\", \"CUG\":\"L\",\n    \"CCU\":\"P\", \"CCC\":\"P\", \"CCA\":\"P\", \"CCG\":\"P\",\n    \"CAU\":\"H\", \"CAC\":\"H\", \"CAA\":\"Q\", \"CAG\":\"Q\",\n    \"CGU\":\"R\", \"CGC\":\"R\", \"CGA\":\"R\", \"CGG\":\"R\",\n    \"AUU\":\"I\", \"AUC\":\"I\", \"AUA\":\"I\", \"AUG\":\"M\",\n    \"ACU\":\"T\", \"ACC\":\"T\", \"ACA\":\"T\", \"ACG\":\"T\",\n    \"AAU\":\"N\", \"AAC\":\"N\", \"AAA\":\"K\", \"AAG\":\"K\",\n    \"AGU\":\"S\", \"AGC\":\"S\", \"AGA\":\"R\", \"AGG\":\"R\",\n    \"GUU\":\"V\", \"GUC\":\"V\", \"GUA\":\"V\", \"GUG\":\"V\",\n    \"GCU\":\"A\", \"GCC\":\"A\", \"GCA\":\"A\", \"GCG\":\"A\",\n    \"GAU\":\"D\", \"GAC\":\"D\", \"GAA\":\"E\", \"GAG\":\"E\",\n    \"GGU\":\"G\", \"GGC\":\"G\", \"GGA\":\"G\", \"GGG\":\"G\",}\n\n\tprotein = ''\n\t# find the start codon and proceed until a 'STOP'\n\tstart = mRNA.find('AUG')\n\tif start != -1:\n\t\twhile start+2 < len(mRNA):\n\t\t\tprotein += codon_map[mRNA[start:start+3]]\n\t\t\tstart += 3\n\t\tprotein = protein[:protein.find('STOP')]\n\tprint protein\n\nif __name__ == \"__main__\":\n\t\"\"\" Parse the command line arguments \"\"\"\n\tparser = argparse.ArgumentParser()\n\tparser.add_argument(\"-r\", \"--mRNA\", type=argparse.FileType('r'), default=sys.stdin)\n\targs = vars(parser.parse_args())\n\n\t\"\"\" Run the main method \"\"\"\n\ttranslate(args)"
              }
            ]
          },
          {
            "class": "ExpressionEngineRequirement",
            "id": "#cwl-js-engine",
            "requirements": [
              {
                "class": "DockerRequirement",
                "dockerPull": "rabix/js-engine"
              }
            ]
          }
        ],
        "stdin": "",
        "arguments": [],
        "inputs": [
          {
            "description": "Input file with mRNA sequence",
            "inputBinding": {
              "position": 3,
              "prefix": "-r",
              "separate": true
            },
            "id": "#input_file",
            "type": [
              "File"
            ]
          },
          {
            "description": "Optional output_filename string",
            "id": "#output_filename",
            "type": [
              "null",
              "string"
            ]
          }
        ],
 
        "hints": [
          {
            "class": "sbg:CPURequirement",
            "value": 1
          },
          {
            "class": "sbg:MemRequirement",
            "value": 1000
          },
          {
            "class": "DockerRequirement",
            "dockerImageId": "",
            "dockerPull": "rfranklin/pythondev"
          }
        ],
        "successCodes": [],
        "class": "CommandLineTool",
        "label": "Translate",
        "id": "https://cgc-api.sbgenomics.com/gauravCGC/fog/translate-argparse/12/raw/"
      },
      "inputs": [
        {
          "id": "#Translate.input_file",
          "source": [
            "#Transcribe.output_file_glob"
          ]
        },
        {
          "id": "#Translate.output_filename",
          "source": [
            "#output_filename"
          ]
        }
      ],
      "outputs": [
        {
          "id": "#Translate.output_protein"
        }
      ]
    }
  ],
  "requirements": [],
  "inputs": [
    {
      "description": "Optional output_filename string",
      "id": "#output_filename",
      "type": [
        "null",
        "string"
      ]
    },
    {
      "id": "#input_file",
      "type": [
        "File"
      ],
      "label": "input_file"
    }
  ],
  "outputs": [
    {
      "id": "#output_protein",
      "type": [
        "File"
      ],
      "label": "output_protein",
      "source": [
        "#Translate.output_protein"
      ]
    }
  ],
  "id": "gauravCGC/tutorials/dna2protein/0",
  "label": "dna2protein",
  "description": "A workflow that converts DNA sequences into peptides.",
  "hints": []
}
