Created
December 19, 2019 17:15
-
-
Save GabrielSGoncalves/c55cb623cabb1aafab10c6c277c1e1b5 to your computer and use it in GitHub Desktop.
Amazon Lambda function to run MAFFT aligner
This file contains hidden or 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
import json | |
import os | |
import boto3 | |
def load_file_from_S3(key, bucket): | |
"""Download file from S3 to /tmp/ folder""" | |
local_path = key.split('/')[-1] | |
print(key) | |
print(local_path) | |
filename = f'/tmp/{local_path}' | |
s3 = boto3.client('s3') | |
s3.download_file(bucket, key, f'/tmp/{local_path}') | |
def upload_file_to_s3(key, bucket): | |
"""Upload local file in /tmp/ to S3 bucket""" | |
local_path = key.split('/')[-1] | |
s3 = boto3.client('s3') | |
with open(f'/tmp/{local_path}', "rb") as f: | |
s3.upload_fileobj(f, bucket, key) | |
def lambda_handler(event, context): | |
"""Main function run when Lambda is invoked""" | |
# Get variables from payload | |
key_in = event.get('file_key_input') | |
bucket_in = event.get('bucket_input') | |
key_out = event.get('file_key_output') | |
bucket_out = event.get('bucket_output') | |
# Get the filename | |
file_name = key_in.split('/')[-1] | |
# Load files on local temp folder | |
load_file_from_S3(key_in, bucket_in) | |
# List files on /tmp/ | |
os.system('ls -la /tmp/') | |
# Change permission for executing Mafft | |
os.system('cp -r ./mafftdir /tmp/') | |
os.system('cp ./mafft.bat /tmp/mafft.bat') | |
os.system('ls -la /tmp/') | |
os.system('chmod -R 755 /tmp/mafftdir') | |
os.system('chmod 755 /tmp/mafft.bat') | |
# Run Mafft aligner | |
os.system(f"/tmp/mafft.bat --auto /tmp/{file_name}> /tmp/{file_name.replace('.fasta', '_aligned.fasta')}") | |
# Upload output file to S3 | |
upload_file_to_s3(key_out, bucket_out) | |
return {'statusCode':200, | |
'body':'MAFFT Lambda success!'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment