Example #1
0
                problems.append(msg)
        names.append(name)
        ns.append(n)
        motifs.append(motif)
        
    if valid:
        pnames = []
        pseqs = []
        for name,n,motif in zip(names,ns,motifs):
            try:
                sites = str2sites(motif)
                seq = sites2seq(sites, region=form.cleaned_data['output'], add16k=form.cleaned_data['add16k'])
                for i in range(n):
                    pnames.append(name)
                    pseqs.append(seq)
            except Exception, e:
                valid = False
                problems.append(e)

    if valid:
        as_fasta = ''.join(list(entry2str({'name':name,'sequence':seq}, WRAP) 
                                for name,seq in zip(pnames, pseqs)))
        c = Context({'results': as_fasta})

    else: # not valid
        c = Context({'problems':problems})

    t = loader.get_template('mttransform/mttransform_seqs.html')
    return HttpResponse(t.render(c))

def run_command():
    """Transform human mtDNA sequence to variable sites."""

    # Set up the options parser
    usage = "usage: %prog [options] filename"
    parser = OptionParser(usage=usage)
    parser.add_option('-r',
                      '--region',
                      dest='region',
                      default='hvr1',
                      help='which predefined sequence region to generate (default hvr1)'
                           '(one of hvr1, hvr2, hvr1to2, coding, or all)')
    parser.add_option('-b',
                      '--begin',
                      dest='begin',
                      type='int',
                      default=None,
                      help='define a region to generate (use with --end)')
    parser.add_option('-e',
                      '--end',
                      dest='end',
                      type='int',
                      default=None,
                      help='define a region to generate (use with --begin)')

    # Parse the options
    (options, args) = parser.parse_args()

    # The filename is always required
    if len(args) != 1:
        print 'You must provide a filename!'
        print "Type 'sites2seq -h' for help."
        sys.exit(1)

    # make sure the sites file exists
    if not os.path.exists(args[0]):
        print 'ERROR: Could not find file: %s' % args[0]
        sys.exit(1)

    count = 0
    for entry in csv.reader(open(args[0], 'rU')):
        count += 1
        if len(entry) != 3:
            print 'ERROR: Problem on row %d of the input file' % count
            sys.exit(1)
        name = entry[0]
        n = int(entry[1])
        sites = entry[2]

        region = options.region
        if options.begin is not None and options.end is not None:
            if options.end < options.begin:
                # wrap through the origin
                region = range(options.begin, 16570)+range(1, options.end+1)
            else:
                region = range(options.begin, options.end+1)
        sequence = sites2seq(sites, region=region)

        entry = {'name':name, 'sequence':sequence}
        for i in range(n):
            print entry2str(entry)