0
|
1 #!/usr/bin/python
|
|
2 # yac = yet another clipper
|
|
3 # v 0.9.1
|
|
4 # Usage yac.py $input $output $adapter_to_clip $min $max $Nmode
|
|
5 # Christophe Antoniewski <drosofff@gmail.com>
|
|
6
|
|
7 import sys, string
|
|
8
|
|
9 class Clip:
|
|
10 def __init__(self, inputfile, outputfile, adapter, minsize, maxsize):
|
|
11 self.inputfile = inputfile
|
|
12 self.outputfile = outputfile
|
|
13 self.adapter = adapter
|
|
14 self.minsize = int(minsize)
|
|
15 self.maxsize = int(maxsize)
|
|
16 def motives (sequence):
|
|
17 '''return a list of motives for perfect (6nt) or imperfect (7nt with one mismatch) search on import string module'''
|
|
18 sequencevariants = [sequence[0:6]] # initializes the list with the 6mer perfect match
|
|
19 dicsubst= {"A":"TGCN", "T":"AGCN", "G":"TACN", "C":"GATN"}
|
|
20 for pos in enumerate(sequence[:6]):
|
|
21 for subst in dicsubst[pos[1]]:
|
|
22 sequencevariants.append(sequence[:pos[0]]+ subst + sequence[pos[0]+1:7])
|
|
23 return sequencevariants
|
|
24 self.adaptmotifs= motives(self.adapter)
|
|
25
|
|
26 def scanadapt(self, adaptmotives=[], sequence=""):
|
|
27 if sequence.rfind(adaptmotives[0]) != -1:
|
|
28 return sequence[:sequence.rfind(adaptmotives[0])]
|
|
29 for motif in adaptmotives[1:]:
|
|
30 if sequence.rfind(motif) != -1:
|
|
31 return sequence[:sequence.rfind(motif)]
|
|
32 return sequence
|
|
33
|
|
34 def clip_with_N (self):
|
|
35 iterator = 0
|
|
36 id = 0
|
|
37 F = open (self.inputfile, "r")
|
|
38 O = open (self.outputfile, "w")
|
|
39 for line in F:
|
|
40 iterator += 1
|
|
41 if iterator % 4 == 2:
|
|
42 trim = self.scanadapt (self.adaptmotifs, line.rstrip() )
|
|
43 if self.minsize <= len(trim) <= self.maxsize:
|
|
44 id += 1
|
|
45 print >> O, ">%i\n%s" % (id, trim)
|
|
46 F.close()
|
|
47 O.close()
|
|
48 def clip_without_N (self):
|
|
49 iterator = 0
|
|
50 id = 0
|
|
51 F = open (self.inputfile, "r")
|
|
52 O = open (self.outputfile, "w")
|
|
53 for line in F:
|
|
54 iterator += 1
|
|
55 if iterator % 4 == 2:
|
|
56 trim = self.scanadapt (self.adaptmotifs, line.rstrip() )
|
|
57 if "N" in trim: continue
|
|
58 if self.minsize <= len(trim) <= self.maxsize:
|
|
59 id += 1
|
|
60 print >> O, ">%i\n%s" % (id, trim)
|
|
61 F.close()
|
|
62 O.close()
|
|
63
|
|
64 def __main__ (inputfile, outputfile, adapter, minsize, maxsize, Nmode):
|
|
65 instanceClip = Clip (inputfile, outputfile, adapter, minsize, maxsize)
|
|
66 if Nmode == "accepted":
|
|
67 instanceClip.clip_with_N()
|
|
68 else:
|
|
69 instanceClip.clip_without_N()
|
|
70
|
|
71 if __name__ == "__main__" :
|
|
72 input = sys.argv[1]
|
|
73 output = sys.argv[2]
|
|
74 adapter = sys.argv[3]
|
|
75 minsize = sys.argv[4]
|
|
76 maxsize = sys.argv[5]
|
|
77 Nmode = sys.argv[6]
|
|
78 __main__(input, output, adapter, minsize, maxsize, Nmode)
|