#!/usr/bin/env python3 """ optparse is deprecated in favor of argparse as of Python 2.7. However, since 2.7 is not always present on many systems, at this writing, it is safer to stick with optparse for now. It should be easy to change later, since the syntax is very similar between argparse and optparse. from optparse import OptionParser """ from optparse import OptionParser import datetime import getpass import os import re import shutil import stat import subprocess import sys import time ''' bl_fastq_pair.py - Set environment variables for BioLegato Helper Applications Synopsis: bl_fastq_pair.py --tsv tsvfile [options] @modified: April 5, 2019 @author: Brian Fristensky @contact: Brian.Fristensky@umanitoba.ca ''' #blib = os.environ.get("BIRCHPYLIB") #sys.path.append(blib) #from birchlib import Birchmod PROGRAM = "bl_fastq_pair.py : " USAGE = "\n\tUSAGE: bl_fastq_pair.py --tsv tsvfile [options]" DEBUG = True if DEBUG : print('bl_fastq_pair: Debugging mode on') #BM = Birchmod(PROGRAM, USAGE) # - - - - - - - - - - - - - Utility classes - - - - - - - - - - - - - - - - - def chmod_ar(filename): """ Make a file world-readable. """ if os.path.exists(filename): st = os.stat(filename) os.chmod(filename, st.st_mode | stat.S_IREAD \ | stat.S_IRGRP | stat.S_IROTH) def chmod_arx(filename): """ Make a file or directory world-readable and world-executable/searchable. """ if os.path.exists(filename): st = os.stat(filename) os.chmod(filename, st.st_mode | stat.S_IEXEC | stat.S_IREAD \ | stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH \ | stat.S_IROTH) def LocalHostname(): """ Return the name of the local machine. Tries a number of methods to get a name other than 'localhost' or a null result. """ import socket import platform def CheckName(name) : if name == None or name.startswith("localhost") or name == "" : OKAY = False else : OKAY = True return OKAY name = os.getenv('HOSTNAME') if not CheckName(name) : name = platform.uname()[1] if not CheckName(name) : if socket.gethostname().find('.')>=0: name=socket.gethostname() else: name=socket.gethostbyaddr(socket.gethostname())[0] return name def SendEmail(From,To,Subject,Text) : """ Very simple email method adapted from: http://stackoverflow.com/questions/882712/sending-html-email-using-python There are more elaborate examples on this site for sending HTML messages and attachments. """ import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText Host = 'localhost' msg = MIMEMultipart('alternative') msg['Subject'] = Subject Html = """\
%s
""" %(Text) part1 = MIMEText(Text, 'plain') part2 = MIMEText(Html, 'html') msg.attach(part1) msg.attach(part2) try: server = smtplib.SMTP(Host) server.sendmail(From, To, msg.as_string()) server.quit() print("Successfully sent email") except : print("Error: unable to send email") # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - class Parameters: """ Wrapper class for command line parameters """ def __init__(self): """ Initializes arguments: TSVFILE = "" HASHSIZE = 100000 BUCKETS = False OUTDIR = "" EMAIL = "" Then calls read_args() to fill in their values from command line """ self.TSVFILE = "." self.HASHSIZE = 100000 self.BUCKETS = False self.OUTDIR = "." self.EMAIL = "" self.read_args() if DEBUG : print('------------ Parameters from command line ------') print(' TSVFILE: ' + self.TSVFILE) print(' HASHSIZE: ' + str(self.HASHSIZE)) print(' BUCKETS: ' + str(self.BUCKETS)) print(' OUTDIR: ' + self.OUTDIR) print(' EMAIL: ' + self.EMAIL) print() def read_args(self): """ Read command line arguments into a Parameter object """ parser = OptionParser() parser.add_option("--tsvfile", dest="tsvfile", action="store", default="", help="list of paired/unpaired read files") parser.add_option("--hashsize", dest="hashsize", action="store", default=100000, help="trim N bases in addition to adapter removal") parser.add_option("--buckets", dest="buckets", action="store_true", default=False, help="Write number of sequences per bucket to output") parser.add_option("--outdir", dest="outdir", action="store", default=".", help="output directory") parser.add_option("--email", dest="email", action="store", default="", help="send email upon completion") (options, args) = parser.parse_args() self.TSVFILE = options.tsvfile self.HASHSIZE = options.hashsize self.BUCKETS = options.buckets self.OUTDIR = options.outdir self.EMAIL = options.email # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - class TSVFiles : """ Methods for reading lists of paired read TSV files, and for writing lists to output. """ def __init__(self): """ Initializes arguments: READPAIRS = [] """ self.READPAIRS = [] def ReadTSVfile(self,FN) : """ TSV file containing names of paired-end and/or single end read files. Paired-end files are on lines such as leftreadfile.fq.gz