File: arden-create

package info (click to toggle)
arden 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,352 kB
  • sloc: python: 1,547; makefile: 3
file content (256 lines) | stat: -rwxr-xr-x 10,287 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
"""
Created 2012
Main script for generating an artificial reference genome. It  calls various modules from core.

@author: Sven Giese
"""





"""
Copyright (c) 2012, Sven H. Giese, gieseS@rki.de, 
Robert Koch-Institut, Germany,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MARTIN S. LINDNER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""





import time 
import os
import sys

import HTSeq
from optparse import OptionParser


# import ARDEN 
from core import  FindOrfs as ORFS
from core import ReadAndWrite as RAW
from core import InsertMutations as IM
from core import Prep as INI

#global variable for DNA basecount
bases = 0

def printExample():
    """
    Function that prints some usage examples.
    
    """
    print("\r\nEXAMPLES:\r\n")
    print ("Here are 3 simple examples with all kinds of different settings. The general input scheme is:")
    print ("usage: python createAR.py [options] [OUTPUTFOLDER] [INPUT FASTA])\r\n")
    print("Case 1:")
    print("\t -f random.fasta (inputfile)")
    print("\t -p /home/ART (outputpath)")
    print("\t -d 12 (minimum distance >= 9)")
    print("\t -o 1 (protect start / stop codons)")
    print("\t -r 1 (If no balanced mutation can be found for a nucleotide don't change it)")
    print("\t -m 1 (select start position randomly)")
    print("\t -s /home/ART/randomorfs (save start stop codons in file)")
    print("CALL:\tpython createAR -s /home/ART/randomorfs -d 12 -o 1 -r 1 -m 1 /home/ART/  random.fasta\r\n")
    print("Case 2:")
    print("\t -f random.fasta (inputfile)")
    print("\t -p /home/ART (outputpath)")
    print("\t -d 12 (minimum distance >= 9)")
    print("\t -o 0 (mutation of start / stop codons is allowed)")
    print("\t -m 0 (start the search for a balanced mutation from left to right in genome)")
    print("\t -r 0 (ignore balanced mutations conditions)")
    print("CALL:\tpython createAR -d 12 -o 0 -r 0 -m 0 /home/ART/ random.fasta \r\n")
    print("Case 3: (use default settings but load previously created orf file)")
    print("\t -f random.fasta (inputfile)")
    print("\t -p /home/ART (outputpath)")
    print("\t -l /home/ART/randomorfs.p (pickle file)")
    print("CALL:\tpython createAR -l /home/ART/randomorfs.p  /home/ART/ random.fasta \r\n")


def Create():
    """
    Function that controls the generation of an artificial reference genome. Subfunction calls, preparation of data structures etc.
    """
    
    usage = """usage: %prog [options] [OUTPUTFOLDER] [INPUT FASTA]...
    Required Arguments:
    
    OUTPUTFOLDER:\t complete path to output destination folder
    INPUT FASTA: \t Single sequence Fasta file.
    
    Script to generate an artificial reference genome (AR) from a given input. The AR fulfills 
    the following conditions by default settings: 
    1) has a nucleotide distribution equal to the input genome
    2) has an amino acid (aa) distribution equal to the input genome
    3) keeps the aa neighborhood similar to the neighborhood in input genome
    4) protects start and stop codons from mutations
    """
    
    parser = OptionParser(usage=usage,version="%prog 1.0")
    parser.add_option("-d", "--distance", type='int',dest="distance",action="store",  default=15,help="distance between 2 mutations on DNA level. The minimum distance will then be d-3. [default: %default]")
    parser.add_option("-o", "--orf", type='string',dest="orf",action="store",  default=1,help="1- protect ORF structure from mutations.\r\n 0 - allow mutations in start / stop codon [default: %default]")
    parser.add_option("-r", "--revsub", type='int',dest="revsub",action="store",  default=1,help="1 - reverse substitution if no suitable counterpart was found (balanced mutation). 0 - keep the unbalanced mutations.[default: %default]")
    parser.add_option("-m", "--random", type='int',dest="random",action="store",  default=1,help=" 1/0 variable. 1 - shuffled starting positions for the mutations. 0 - linear mutation [default: %default]")
    parser.add_option("-s", "--saveorfs", type='string',dest="sorf",action="store",  default="",help="Save found start and stop codons in a pickle file (can be loaded if the input fasta is used again if a filename is specified). [default: %default]")
    parser.add_option("-l", "--loadorfs", type='string',dest="lorf",action="store",  default="",help="Specify filename to a previously created pickle file (contains positions of start/stop codons) [default: %default]")
    parser.add_option("-n", "--name", type='string',dest="name",action="store",  default="",help="Specify name which will be used as header. [default: %default]")
    parser.add_option("-p", "--pexamples", type='int',dest="pexamples",action="store",  default=0,help="set to 1 if you want to print examples")
    parser.description=""
    (options, args) = parser.parse_args() 
    numArgs = len(args)
 
    # check if examples shall be printed
    if options.pexamples == 1:
        # self defined function to print
        printExample()
        # exit program
        sys.exit()
        
    # numArgs has to be equal to # required parameters
    if numArgs ==2 :
        outputpath = args[0]
        inputpath = args[1]
        # adjust options, otherwise default is used
        distance = options.distance
        orfs = options.orf
        rev = options.revsub
        random = options.random
        sorfs = options.sorf
        lorfs = options.lorf
        fastaheader = options.name
    else:
        print("!!!Wrong number of parameters!!!")
        print("Add --help to print help message.")
        #parser.print_help()
        sys.exit(1)


    # adjust to complete path
    #outputpath = os.path.abspath(outputpath+"/")
    print outputpath
    
    # create output directory if necessary
    if not os.path.exists(os.path.dirname(outputpath)):
            os.makedirs(os.path.dirname(outputpath))
    
    
    
    # number of nucleotides in genome
    global bases
    '''0. Step: prepare dictionaries and load DNA'''
  
    print("\r\n###############Your Settings:######################")
    print("outputpath:\t"+ str(outputpath))
    print("inputfile:\t"+ str(inputpath))
    print("distance:\t" + str(distance))
    print("orfs:\t\t" +  str(orfs))
    print("rev:\t\t" + str(rev))
    print("random:\t\t" + str(random))
    print("###################################################")
    print ("\r\nStart:")
    print("\tReading DNA...")
    Reference = RAW.readdna(inputpath)
    
    #adjust reference name to option
    if fastaheader == "":
        Reference.name = ''.join(e for e in Reference.name if e.isalnum())
    else:
        Reference.name = fastaheader
        
    # generate header with all information (which options enabled etc.)
    ArtHead = "AR_%s%s%s_%s_%s"  % (str(orfs),str(rev),str(random),str(distance),Reference.name)

    # INIT OUTFILES
    # This file contains a complete log of the mutation algorithm
    # Position for initial mutation in NUC / AA and the 2nd mutation if it was successful
    fobj2= open(outputpath+ArtHead+"_CompleteLog.txt","w")
    fobj2.close()

    #1. Step :Find Orfs 
   
    # init orf dictionary
    pdic={}
    
    # load orfs= yes?
    if (lorfs != ""):
        pdic = INI.loadpickle(lorfs)
    # else create orfs
    else:
        if (orfs ==1 ):
            #keep orf structure - enabled 
            print("Searching Orfs...")
            
            pdic= ORFS.find_orfs(Reference.seq,pdic)
            if sorfs != "":
                INI.savepickle(pdic,sorfs)
        #keep orf structure  - disabled --> do nothing

    
    #2. Step: Translate DNA 
    print("\tTranslating DNA...")
    AminoA =""
    # dna which cannot be used for AA translation
    rest_DNA = ""
    AminoA,rest_DNA = INI.trans_seq(Reference.seq)
    
        
    
    #3. Step: Mutate DNA /AA
    print("\tMutate DNA...")
    # generate artifical genome sequence
    AR = HTSeq.Sequence(IM.mutate_random(Reference.seq,AminoA,distance,pdic,rev,ArtHead,random,outputpath),ArtHead)
    # write AR to output fasta
    RAW.writefile(AR,outputpath+"/"+ArtHead+".fasta")

        
    '''4. Step: Compare Genomes'''
    # compare hamming distance between REF and ART
    RAW.gethammingdistance(Reference.seq,AR.seq)
    
    # compare NUC and AA distributions of REF and ART
    n1=RAW.nucleotide_dist_seq(Reference.seq,Reference.name+"_dis.txt",0)
    n2=RAW.nucleotide_dist_seq(AR.seq,ArtHead+"_dis.txt",0)
    
    #translate sequence of the artifical reference genome
    AminoA_AR,dummy3=INI.trans_seq(AR.seq)
    
    a1=RAW.aa_dist_seq(AminoA,outputpath+Reference.name+"_dis_protein.txt",0)
    a2=RAW.aa_dist_seq(AminoA_AR,outputpath+ArtHead+"_dis_protein.txt",0)
    
    # write the NUC AA distributions to the Delta file
    RAW.writeoverview(n1,a1,n2,a2,outputpath+ArtHead+"_Delta.txt")
    bases = len(Reference.seq)
   


a = time.time()
Create()
b = time.time()
print ("Finished!")
print ("Processed %d bases in %f seconds!" % (bases,b-a))