annotate smRtools.py @ 1:ca3845fb0b31 draft default tip

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie_parser commit 70312b58ba246c07e70cdbd0a097f274f1386d09
author drosofff
date Mon, 18 Apr 2016 10:09:08 -0400
parents b996480cd604
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
1 #!/usr/bin/python
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
2 # version 1 7-5-2012 unification of the SmRNAwindow class
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
3
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
4 import sys, subprocess
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
5 from collections import defaultdict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
6 from numpy import mean, median, std
1
ca3845fb0b31 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie_parser commit 70312b58ba246c07e70cdbd0a097f274f1386d09
drosofff
parents: 0
diff changeset
7 ##Disable scipy import temporarily, as no working scipy on toolshed.
ca3845fb0b31 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie_parser commit 70312b58ba246c07e70cdbd0a097f274f1386d09
drosofff
parents: 0
diff changeset
8 ##from scipy import stats
0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
9
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
10 def get_fasta (index="/home/galaxy/galaxy-dist/bowtie/5.37_Dmel/5.37_Dmel"):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
11 '''This function will return a dictionary containing fasta identifiers as keys and the
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
12 sequence as values. Index must be the path to a fasta file.'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
13 p = subprocess.Popen(args=["bowtie-inspect","-a", "0", index], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # bowtie-inspect outputs sequences on single lines
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
14 outputlines = p.stdout.readlines()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
15 p.wait()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
16 item_dic = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
17 for line in outputlines:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
18 if (line[0] == ">"):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
19 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
20 item_dic[current_item] = "".join(stringlist) # to dump the sequence of the previous item - try because of the keyerror of the first item
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
21 except: pass
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
22 current_item = line[1:].rstrip().split()[0] #take the first word before space because bowtie splits headers !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
23 item_dic[current_item] = ""
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
24 stringlist=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
25 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
26 stringlist.append(line.rstrip() )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
27 item_dic[current_item] = "".join(stringlist) # for the last item
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
28 return item_dic
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
29
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
30 def get_fasta_headers (index):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
31 p = subprocess.Popen(args=["bowtie-inspect","-n", index], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # bowtie-inspect outputs sequences on single lines
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
32 outputlines = p.stdout.readlines()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
33 p.wait()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
34 item_dic = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
35 for line in outputlines:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
36 header = line.rstrip().split()[0] #take the first word before space because bowtie splits headers !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
37 item_dic[header] = 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
38 return item_dic
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
39
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
40
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
41 def get_file_sample (file, numberoflines):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
42 '''import random to use this function'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
43 F=open(file)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
44 fullfile = F.read().splitlines()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
45 F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
46 if len(fullfile) < numberoflines:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
47 return "sample size exceeds file size"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
48 return random.sample(fullfile, numberoflines)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
49
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
50 def get_fasta_from_history (file):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
51 F = open (file, "r")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
52 item_dic = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
53 for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
54 if (line[0] == ">"):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
55 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
56 item_dic[current_item] = "".join(stringlist) # to dump the sequence of the previous item - try because of the keyerror of the first item
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
57 except: pass
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
58 current_item = line[1:-1].split()[0] #take the first word before space because bowtie splits headers !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
59 item_dic[current_item] = ""
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
60 stringlist=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
61 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
62 stringlist.append(line[:-1])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
63 item_dic[current_item] = "".join(stringlist) # for the last item
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
64 return item_dic
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
65
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
66 def antipara (sequence):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
67 antidict = {"A":"T", "T":"A", "G":"C", "C":"G", "N":"N"}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
68 revseq = sequence[::-1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
69 return "".join([antidict[i] for i in revseq])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
70
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
71 def RNAtranslate (sequence):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
72 return "".join([i if i in "AGCN" else "U" for i in sequence])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
73 def DNAtranslate (sequence):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
74 return "".join([i if i in "AGCN" else "T" for i in sequence])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
75
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
76 def RNAfold (sequence_list):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
77 thestring= "\n".join(sequence_list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
78 p = subprocess.Popen(args=["RNAfold","--noPS"], stdin= subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
79 output=p.communicate(thestring)[0]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
80 p.wait()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
81 output=output.split("\n")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
82 if not output[-1]: output = output[:-1] # nasty patch to remove last empty line
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
83 buffer=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
84 for line in output:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
85 if line[0] in ["N","A","T","U","G","C"]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
86 buffer.append(DNAtranslate(line))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
87 if line[0] in ["(",".",")"]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
88 fields=line.split("(")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
89 energy= fields[-1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
90 energy = energy[:-1] # remove the ) parenthesis
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
91 energy=float(energy)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
92 buffer.append(str(energy))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
93 return dict(zip(buffer[::2], buffer[1::2]))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
94
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
95 def extractsubinstance (start, end, instance):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
96 ''' Testing whether this can be an function external to the class to save memory'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
97 subinstance = SmRNAwindow (instance.gene, instance.sequence[start-1:end], start)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
98 subinstance.gene = "%s %s %s" % (subinstance.gene, subinstance.windowoffset, subinstance.windowoffset + subinstance.size - 1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
99 upcoordinate = [i for i in range(start,end+1) if instance.readDict.has_key(i) ]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
100 downcoordinate = [-i for i in range(start,end+1) if instance.readDict.has_key(-i) ]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
101 for i in upcoordinate:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
102 subinstance.readDict[i]=instance.readDict[i]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
103 for i in downcoordinate:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
104 subinstance.readDict[i]=instance.readDict[i]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
105 return subinstance
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
106
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
107 class HandleSmRNAwindows:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
108 def __init__(self, alignmentFile="~", alignmentFileFormat="tabular", genomeRefFile="~", genomeRefFormat="bowtieIndex", biosample="undetermined", size_inf=None, size_sup=1000, norm=1.0):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
109 self.biosample = biosample
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
110 self.alignmentFile = alignmentFile
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
111 self.alignmentFileFormat = alignmentFileFormat # can be "tabular" or "sam"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
112 self.genomeRefFile = genomeRefFile
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
113 self.genomeRefFormat = genomeRefFormat # can be "bowtieIndex" or "fastaSource"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
114 self.alignedReads = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
115 self.instanceDict = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
116 self.size_inf=size_inf
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
117 self.size_sup=size_sup
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
118 self.norm=norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
119 if genomeRefFormat == "bowtieIndex":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
120 self.itemDict = get_fasta (genomeRefFile)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
121 elif genomeRefFormat == "fastaSource":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
122 self.itemDict = get_fasta_from_history (genomeRefFile)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
123 for item in self.itemDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
124 self.instanceDict[item] = SmRNAwindow(item, sequence=self.itemDict[item], windowoffset=1, biosample=self.biosample, norm=self.norm) # create as many instances as there is items
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
125 self.readfile()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
126
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
127 def readfile (self) :
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
128 if self.alignmentFileFormat == "tabular":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
129 F = open (self.alignmentFile, "r")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
130 for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
131 fields = line.split()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
132 polarity = fields[1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
133 gene = fields[2]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
134 offset = int(fields[3])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
135 size = len (fields[4])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
136 if self.size_inf:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
137 if (size>=self.size_inf and size<= self.size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
138 self.instanceDict[gene].addread (polarity, offset+1, size) # to correct to 1-based coordinates of SmRNAwindow
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
139 self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
140 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
141 self.instanceDict[gene].addread (polarity, offset+1, size) # to correct to 1-based coordinates of SmRNAwindow
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
142 self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
143 F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
144 return self.instanceDict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
145 # elif self.alignmentFileFormat == "sam":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
146 # F = open (self.alignmentFile, "r")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
147 # dict = {"0":"+", "16":"-"}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
148 # for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
149 # if line[0]=='@':
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
150 # continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
151 # fields = line.split()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
152 # if fields[2] == "*": continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
153 # polarity = dict[fields[1]]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
154 # gene = fields[2]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
155 # offset = int(fields[3])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
156 # size = len (fields[9])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
157 # if self.size_inf:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
158 # if (size>=self.size_inf and size<= self.size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
159 # self.instanceDict[gene].addread (polarity, offset, size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
160 # self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
161 # else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
162 # self.instanceDict[gene].addread (polarity, offset, size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
163 # self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
164 # F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
165 elif self.alignmentFileFormat == "bam" or self.alignmentFileFormat == "sam":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
166 import pysam
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
167 samfile = pysam.Samfile(self.alignmentFile)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
168 for read in samfile:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
169 if read.tid == -1:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
170 continue # filter out unaligned reads
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
171 if read.is_reverse:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
172 polarity="-"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
173 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
174 polarity="+"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
175 gene = samfile.getrname(read.tid)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
176 offset = read.pos
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
177 size = read.qlen
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
178 if self.size_inf:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
179 if (size>=self.size_inf and size<= self.size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
180 self.instanceDict[gene].addread (polarity, offset+1, size) # to correct to 1-based coordinates of SmRNAwindow
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
181 self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
182 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
183 self.instanceDict[gene].addread (polarity, offset+1, size) # to correct to 1-based coordinates of SmRNAwindow
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
184 self.alignedReads += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
185 return self.instanceDict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
186
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
187 # def size_histogram (self):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
188 # size_dict={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
189 # size_dict['F']= defaultdict (int)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
190 # size_dict['R']= defaultdict (int)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
191 # size_dict['both'] = defaultdict (int)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
192 # for item in self.instanceDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
193 # buffer_dict_F = self.instanceDict[item].size_histogram()['F']
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
194 # buffer_dict_R = self.instanceDict[item].size_histogram()['R']
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
195 # for size in buffer_dict_F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
196 # size_dict['F'][size] += buffer_dict_F[size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
197 # for size in buffer_dict_R:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
198 # size_dict['R'][size] -= buffer_dict_R[size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
199 # allSizeKeys = list (set (size_dict['F'].keys() + size_dict['R'].keys() ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
200 # for size in allSizeKeys:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
201 # size_dict['both'][size] = size_dict['F'][size] + size_dict['R'][size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
202 # return size_dict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
203 def size_histogram (self): # in HandleSmRNAwindows
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
204 '''refactored on 7-9-2014 to debug size_histogram tool'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
205 size_dict={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
206 size_dict['F']= defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
207 size_dict['R']= defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
208 size_dict['both'] = defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
209 for item in self.instanceDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
210 buffer_dict = self.instanceDict[item].size_histogram()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
211 for polarity in ["F", "R"]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
212 for size in buffer_dict[polarity]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
213 size_dict[polarity][size] += buffer_dict[polarity][size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
214 for size in buffer_dict["both"]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
215 size_dict["both"][size] += buffer_dict["F"][size] - buffer_dict["R"][size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
216 return size_dict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
217
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
218 def CountFeatures (self, GFF3="path/to/file"):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
219 featureDict = defaultdict(int)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
220 F = open (GFF3, "r")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
221 for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
222 if line[0] == "#": continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
223 fields = line[:-1].split()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
224 chrom, feature, leftcoord, rightcoord, polarity = fields[0], fields[2], fields[3], fields[4], fields[6]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
225 featureDict[feature] += self.instanceDict[chrom].readcount(upstream_coord=int(leftcoord), downstream_coord=int(rightcoord), polarity="both", method="destructive")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
226 F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
227 return featureDict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
228
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
229 class SmRNAwindow:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
230
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
231 def __init__(self, gene, sequence="ATGC", windowoffset=1, biosample="Undetermined", norm=1.0):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
232 self.biosample = biosample
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
233 self.sequence = sequence
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
234 self.gene = gene
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
235 self.windowoffset = windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
236 self.size = len(sequence)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
237 self.readDict = defaultdict(list) # with a {+/-offset:[size1, size2, ...], ...}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
238 self.matchedreadsUp = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
239 self.matchedreadsDown = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
240 self.norm=norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
241
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
242 def addread (self, polarity, offset, size):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
243 '''ATTENTION ATTENTION ATTENTION'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
244 ''' We removed the conversion from 0 to 1 based offset, as we do this now during readparsing.'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
245 if polarity == "+":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
246 self.readDict[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
247 self.matchedreadsUp += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
248 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
249 self.readDict[-(offset + size -1)].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
250 self.matchedreadsDown += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
251 return
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
252
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
253 def barycenter (self, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
254 '''refactored 24-12-2013 to save memory and introduce offset filtering see readcount method for further discussion on that
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
255 In this version, attempt to replace the dictionary structure by a list of tupple to save memory too'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
256 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
257 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
258 window_size = downstream_coord - upstream_coord +1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
259 def weigthAverage (TuppleList):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
260 weightSum = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
261 PonderWeightSum = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
262 for tuple in TuppleList:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
263 PonderWeightSum += tuple[0] * tuple[1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
264 weightSum += tuple[1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
265 if weightSum > 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
266 return PonderWeightSum / float(weightSum)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
267 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
268 return 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
269 forwardTuppleList = [(k, len(self.readDict[k])) for k in self.readDict.keys() if (k > 0 and abs(k) >= upstream_coord and abs(k) <= downstream_coord)] # both forward and in the proper offset window
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
270 reverseTuppleList = [(-k, len(self.readDict[k])) for k in self.readDict.keys() if (k < 0 and abs(k) >= upstream_coord and abs(k) <= downstream_coord)] # both reverse and in the proper offset window
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
271 Fbarycenter = (weigthAverage (forwardTuppleList) - upstream_coord) / window_size
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
272 Rbarycenter = (weigthAverage (reverseTuppleList) - upstream_coord) / window_size
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
273 return Fbarycenter, Rbarycenter
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
274
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
275 def correlation_mapper (self, reference, window_size):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
276 '''to map correlation with a sliding window 26-2-2013'''
1
ca3845fb0b31 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie_parser commit 70312b58ba246c07e70cdbd0a097f274f1386d09
drosofff
parents: 0
diff changeset
277 from scipy import stats
ca3845fb0b31 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie_parser commit 70312b58ba246c07e70cdbd0a097f274f1386d09
drosofff
parents: 0
diff changeset
278
0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
279 if window_size > self.size:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
280 return []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
281 F=open(reference, "r")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
282 reference_forward = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
283 reference_reverse = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
284 for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
285 fields=line.split()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
286 reference_forward.append(int(float(fields[1])))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
287 reference_reverse.append(int(float(fields[2])))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
288 F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
289 local_object_forward=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
290 local_object_reverse=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
291 ## Dict to list for the local object
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
292 for i in range(1, self.size+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
293 local_object_forward.append(len(self.readDict[i]))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
294 local_object_reverse.append(len(self.readDict[-i]))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
295 ## start compiling results by slides
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
296 results=[]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
297 for coordinate in range(self.size - window_size):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
298 local_forward=local_object_forward[coordinate:coordinate + window_size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
299 local_reverse=local_object_reverse[coordinate:coordinate + window_size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
300 if sum(local_forward) == 0 or sum(local_reverse) == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
301 continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
302 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
303 reference_to_local_cor_forward = stats.spearmanr(local_forward, reference_forward)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
304 reference_to_local_cor_reverse = stats.spearmanr(local_reverse, reference_reverse)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
305 if (reference_to_local_cor_forward[0] > 0.2 or reference_to_local_cor_reverse[0]>0.2):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
306 results.append([coordinate+1, reference_to_local_cor_forward[0], reference_to_local_cor_reverse[0]])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
307 except:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
308 pass
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
309 return results
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
310
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
311 def readcount (self, size_inf=0, size_sup=1000, upstream_coord=None, downstream_coord=None, polarity="both", method="conservative"):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
312 '''refactored 24-12-2013 to save memory and introduce offset filtering
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
313 take a look at the defaut parameters that cannot be defined relatively to the instance are they are defined before instanciation
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
314 the trick is to pass None and then test
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
315 polarity parameter can take "both", "forward" or "reverse" as value'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
316 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
317 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
318 if upstream_coord == 1 and downstream_coord == self.windowoffset+self.size-1 and polarity == "both":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
319 return self.matchedreadsUp + self.matchedreadsDown
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
320 if upstream_coord == 1 and downstream_coord == self.windowoffset+self.size-1 and polarity == "forward":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
321 return self.matchedreadsUp
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
322 if upstream_coord == 1 and downstream_coord == self.windowoffset+self.size-1 and polarity == "reverse":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
323 return self.matchedreadsDown
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
324 n=0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
325 if polarity == "both":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
326 for offset in xrange(upstream_coord, downstream_coord+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
327 if self.readDict.has_key(offset):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
328 for read in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
329 if (read>=size_inf and read<= size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
330 n += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
331 if method != "conservative":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
332 del self.readDict[offset] ## Carefull ! precludes re-use on the self.readDict dictionary !!!!!! TEST
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
333 if self.readDict.has_key(-offset):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
334 for read in self.readDict[-offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
335 if (read>=size_inf and read<= size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
336 n += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
337 if method != "conservative":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
338 del self.readDict[-offset]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
339 return n
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
340 elif polarity == "forward":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
341 for offset in xrange(upstream_coord, downstream_coord+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
342 if self.readDict.has_key(offset):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
343 for read in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
344 if (read>=size_inf and read<= size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
345 n += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
346 return n
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
347 elif polarity == "reverse":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
348 for offset in xrange(upstream_coord, downstream_coord+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
349 if self.readDict.has_key(-offset):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
350 for read in self.readDict[-offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
351 if (read>=size_inf and read<= size_sup):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
352 n += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
353 return n
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
354
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
355 def readsizes (self):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
356 '''return a dictionary of number of reads by size (the keys)'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
357 dicsize = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
358 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
359 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
360 dicsize[size] = dicsize.get(size, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
361 for offset in range (min(dicsize.keys()), max(dicsize.keys())+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
362 dicsize[size] = dicsize.get(size, 0) # to fill offsets with null values
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
363 return dicsize
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
364
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
365 # def size_histogram(self):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
366 # norm=self.norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
367 # hist_dict={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
368 # hist_dict['F']={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
369 # hist_dict['R']={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
370 # for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
371 # for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
372 # if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
373 # hist_dict['R'][size] = hist_dict['R'].get(size, 0) - 1*norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
374 # else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
375 # hist_dict['F'][size] = hist_dict['F'].get(size, 0) + 1*norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
376 # ## patch to avoid missing graphs when parsed by R-lattice. 27-08-2014. Test and validate !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
377 # if not (hist_dict['F']) and (not hist_dict['R']):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
378 # hist_dict['F'][21] = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
379 # hist_dict['R'][21] = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
380 # ##
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
381 # return hist_dict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
382
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
383 def size_histogram(self, minquery=None, maxquery=None): # in SmRNAwindow
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
384 '''refactored on 7-9-2014 to debug size_histogram tool'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
385 norm=self.norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
386 size_dict={}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
387 size_dict['F']= defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
388 size_dict['R']= defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
389 size_dict['both']= defaultdict (float)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
390 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
391 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
392 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
393 size_dict['R'][size] = size_dict['R'][size] - 1*norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
394 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
395 size_dict['F'][size] = size_dict['F'][size] + 1*norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
396 ## patch to avoid missing graphs when parsed by R-lattice. 27-08-2014. Test and validate !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
397 if not (size_dict['F']) and (not size_dict['R']):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
398 size_dict['F'][21] = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
399 size_dict['R'][21] = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
400 ##
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
401 allSizeKeys = list (set (size_dict['F'].keys() + size_dict['R'].keys() ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
402 for size in allSizeKeys:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
403 size_dict['both'][size] = size_dict['F'][size] - size_dict['R'][size]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
404 if minquery:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
405 for polarity in size_dict.keys():
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
406 for size in xrange(minquery, maxquery+1):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
407 if not size in size_dict[polarity].keys():
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
408 size_dict[polarity][size]=0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
409 return size_dict
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
410
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
411 def statsizes (self, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
412 ''' migration to memory saving by specifying possible subcoordinates
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
413 see the readcount method for further discussion'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
414 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
415 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
416 L = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
417 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
418 if (abs(offset) < upstream_coord or abs(offset) > downstream_coord): continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
419 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
420 L.append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
421 meansize = mean(L)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
422 stdv = std(L)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
423 mediansize = median(L)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
424 return meansize, mediansize, stdv
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
425
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
426 def foldEnergy (self, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
427 ''' migration to memory saving by specifying possible subcoordinates
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
428 see the readcount method for further discussion'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
429 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
430 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
431 Energy = RNAfold ([self.sequence[upstream_coord-1:downstream_coord] ])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
432 return float(Energy[self.sequence[upstream_coord-1:downstream_coord]])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
433
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
434 def Ufreq (self, size_scope, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
435 ''' migration to memory saving by specifying possible subcoordinates
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
436 see the readcount method for further discussion. size_scope must be an interable'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
437 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
438 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
439 freqDic = {"A":0,"T":0,"G":0,"C":0, "N":0}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
440 convertDic = {"A":"T","T":"A","G":"C","C":"G","N":"N"}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
441 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
442 if (abs(offset) < upstream_coord or abs(offset) > downstream_coord): continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
443 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
444 if size in size_scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
445 startbase = self.sequence[abs(offset)-self.windowoffset]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
446 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
447 startbase = convertDic[startbase]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
448 freqDic[startbase] += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
449 base_sum = float ( sum( freqDic.values()) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
450 if base_sum == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
451 return "."
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
452 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
453 return freqDic["T"] / base_sum * 100
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
454
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
455 def Ufreq_stranded (self, size_scope, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
456 ''' migration to memory saving by specifying possible subcoordinates
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
457 see the readcount method for further discussion. size_scope must be an interable
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
458 This method is similar to the Ufreq method but take strandness into account'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
459 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
460 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
461 freqDic = {"Afor":0,"Tfor":0,"Gfor":0,"Cfor":0, "Nfor":0,"Arev":0,"Trev":0,"Grev":0,"Crev":0, "Nrev":0}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
462 convertDic = {"A":"T","T":"A","G":"C","C":"G","N":"N"}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
463 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
464 if (abs(offset) < upstream_coord or abs(offset) > downstream_coord): continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
465 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
466 if size in size_scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
467 startbase = self.sequence[abs(offset)-self.windowoffset]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
468 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
469 startbase = convertDic[startbase]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
470 freqDic[startbase+"rev"] += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
471 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
472 freqDic[startbase+"for"] += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
473 forward_sum = float ( freqDic["Afor"]+freqDic["Tfor"]+freqDic["Gfor"]+freqDic["Cfor"]+freqDic["Nfor"])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
474 reverse_sum = float ( freqDic["Arev"]+freqDic["Trev"]+freqDic["Grev"]+freqDic["Crev"]+freqDic["Nrev"])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
475 if forward_sum == 0 and reverse_sum == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
476 return ". | ."
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
477 elif reverse_sum == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
478 return "%s | ." % (freqDic["Tfor"] / forward_sum * 100)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
479 elif forward_sum == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
480 return ". | %s" % (freqDic["Trev"] / reverse_sum * 100)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
481 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
482 return "%s | %s" % (freqDic["Tfor"] / forward_sum * 100, freqDic["Trev"] / reverse_sum * 100)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
483
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
484
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
485 def readplot (self):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
486 norm=self.norm
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
487 readmap = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
488 for offset in self.readDict.keys():
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
489 readmap[abs(offset)] = ( len(self.readDict.get(-abs(offset),[]))*norm , len(self.readDict.get(abs(offset),[]))*norm )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
490 mylist = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
491 for offset in sorted(readmap):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
492 if readmap[offset][1] != 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
493 mylist.append("%s\t%s\t%s\t%s" % (self.gene, offset, readmap[offset][1], "F") )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
494 if readmap[offset][0] != 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
495 mylist.append("%s\t%s\t%s\t%s" % (self.gene, offset, -readmap[offset][0], "R") )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
496 ## patch to avoid missing graphs when parsed by R-lattice. 27-08-2014. Test and validate !
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
497 if not mylist:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
498 mylist.append("%s\t%s\t%s\t%s" % (self.gene, 1, 0, "F") )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
499 ###
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
500 return mylist
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
501
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
502 def readcoverage (self, upstream_coord=None, downstream_coord=None, windowName=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
503 '''Use by MirParser tool'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
504 upstream_coord = upstream_coord or 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
505 downstream_coord = downstream_coord or self.size
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
506 windowName = windowName or "%s_%s_%s" % (self.gene, upstream_coord, downstream_coord)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
507 forORrev_coverage = dict ([(i,0) for i in xrange(1, downstream_coord-upstream_coord+1)])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
508 totalforward = self.readcount(upstream_coord=upstream_coord, downstream_coord=downstream_coord, polarity="forward")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
509 totalreverse = self.readcount(upstream_coord=upstream_coord, downstream_coord=downstream_coord, polarity="reverse")
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
510 if totalforward > totalreverse:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
511 majorcoverage = "forward"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
512 for offset in self.readDict.keys():
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
513 if (offset > 0) and ((offset-upstream_coord+1) in forORrev_coverage.keys() ):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
514 for read in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
515 for i in xrange(read):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
516 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
517 forORrev_coverage[offset-upstream_coord+1+i] += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
518 except KeyError:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
519 continue # a sense read may span over the downstream limit
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
520 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
521 majorcoverage = "reverse"
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
522 for offset in self.readDict.keys():
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
523 if (offset < 0) and (-offset-upstream_coord+1 in forORrev_coverage.keys() ):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
524 for read in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
525 for i in xrange(read):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
526 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
527 forORrev_coverage[-offset-upstream_coord-i] += 1 ## positive coordinates in the instance, with + for forward coverage and - for reverse coverage
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
528 except KeyError:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
529 continue # an antisense read may span over the upstream limit
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
530 output_list = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
531 maximum = max (forORrev_coverage.values()) or 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
532 for n in sorted (forORrev_coverage):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
533 output_list.append("%s\t%s\t%s\t%s\t%s\t%s\t%s" % (self.biosample, windowName, n, float(n)/(downstream_coord-upstream_coord+1), forORrev_coverage[n], float(forORrev_coverage[n])/maximum, majorcoverage))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
534 return "\n".join(output_list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
535
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
536
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
537 def signature (self, minquery, maxquery, mintarget, maxtarget, scope, zscore="no", upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
538 ''' migration to memory saving by specifying possible subcoordinates
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
539 see the readcount method for further discussion
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
540 scope must be a python iterable; scope define the *relative* offset range to be computed'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
541 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
542 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
543 query_range = range (minquery, maxquery+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
544 target_range = range (mintarget, maxtarget+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
545 Query_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
546 Target_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
547 frequency_table = dict ([(i, 0) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
548 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
549 if (abs(offset) < upstream_coord or abs(offset) > downstream_coord): continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
550 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
551 if size in query_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
552 Query_table[offset] = Query_table.get(offset, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
553 if size in target_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
554 Target_table[offset] = Target_table.get(offset, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
555 for offset in Query_table:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
556 for i in scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
557 frequency_table[i] += min(Query_table[offset], Target_table.get(-offset -i +1, 0))
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
558 if minquery==mintarget and maxquery==maxtarget: ## added to incorporate the division by 2 in the method (26/11/2013), see signature_options.py and lattice_signature.py
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
559 frequency_table = dict([(i,frequency_table[i]/2) for i in frequency_table])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
560 if zscore == "yes":
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
561 z_mean = mean(frequency_table.values() )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
562 z_std = std(frequency_table.values() )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
563 if z_std == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
564 frequency_table = dict([(i,0) for i in frequency_table] )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
565 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
566 frequency_table = dict([(i, (frequency_table[i]- z_mean)/z_std) for i in frequency_table] )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
567 return frequency_table
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
568
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
569 def hannon_signature (self, minquery, maxquery, mintarget, maxtarget, scope, upstream_coord=None, downstream_coord=None):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
570 ''' migration to memory saving by specifying possible subcoordinates see the readcount method for further discussion
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
571 note that scope must be an iterable (a list or a tuple), which specifies the relative offsets that will be computed'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
572 upstream_coord = upstream_coord or self.windowoffset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
573 downstream_coord = downstream_coord or self.windowoffset+self.size-1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
574 query_range = range (minquery, maxquery+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
575 target_range = range (mintarget, maxtarget+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
576 Query_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
577 Target_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
578 Total_Query_Numb = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
579 general_frequency_table = dict ([(i,0) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
580 ## filtering the appropriate reads for the study
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
581 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
582 if (abs(offset) < upstream_coord or abs(offset) > downstream_coord): continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
583 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
584 if size in query_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
585 Query_table[offset] = Query_table.get(offset, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
586 Total_Query_Numb += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
587 if size in target_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
588 Target_table[offset] = Target_table.get(offset, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
589 for offset in Query_table:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
590 frequency_table = dict ([(i,0) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
591 number_of_targets = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
592 for i in scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
593 frequency_table[i] += Query_table[offset] * Target_table.get(-offset -i +1, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
594 number_of_targets += Target_table.get(-offset -i +1, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
595 for i in scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
596 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
597 general_frequency_table[i] += (1. / number_of_targets / Total_Query_Numb) * frequency_table[i]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
598 except ZeroDivisionError :
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
599 continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
600 return general_frequency_table
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
601
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
602 def phasing (self, size_range, scope):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
603 ''' to calculate autocorelation like signal - scope must be an python iterable'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
604 read_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
605 total_read_number = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
606 general_frequency_table = dict ([(i, 0) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
607 ## read input filtering
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
608 for offset in self.readDict:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
609 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
610 if size in size_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
611 read_table[offset] = read_table.get(offset, 0) + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
612 total_read_number += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
613 ## per offset read phasing computing
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
614 for offset in read_table:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
615 frequency_table = dict ([(i, 0) for i in scope]) # local frequency table
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
616 number_of_targets = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
617 for i in scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
618 if offset > 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
619 frequency_table[i] += read_table[offset] * read_table.get(offset + i, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
620 number_of_targets += read_table.get(offset + i, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
621 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
622 frequency_table[i] += read_table[offset] * read_table.get(offset - i, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
623 number_of_targets += read_table.get(offset - i, 0)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
624 ## inclusion of local frequency table in the general frequency table (all offsets average)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
625 for i in scope:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
626 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
627 general_frequency_table[i] += (1. / number_of_targets / total_read_number) * frequency_table[i]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
628 except ZeroDivisionError :
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
629 continue
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
630 return general_frequency_table
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
631
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
632
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
633
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
634 def z_signature (self, minquery, maxquery, mintarget, maxtarget, scope):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
635 '''Must do: from numpy import mean, std, to use this method; scope must be a python iterable and defines the relative offsets to compute'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
636 frequency_table = self.signature (minquery, maxquery, mintarget, maxtarget, scope)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
637 z_table = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
638 frequency_list = [frequency_table[i] for i in sorted (frequency_table)]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
639 if std(frequency_list):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
640 meanlist = mean(frequency_list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
641 stdlist = std(frequency_list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
642 z_list = [(i-meanlist)/stdlist for i in frequency_list]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
643 return dict (zip (sorted(frequency_table), z_list) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
644 else:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
645 return dict (zip (sorted(frequency_table), [0 for i in frequency_table]) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
646
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
647 def percent_signature (self, minquery, maxquery, mintarget, maxtarget, scope):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
648 frequency_table = self.signature (minquery, maxquery, mintarget, maxtarget, scope)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
649 total = float(sum ([self.readsizes().get(i,0) for i in set(range(minquery,maxquery)+range(mintarget,maxtarget))]) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
650 if total == 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
651 return dict( [(i,0) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
652 return dict( [(i, frequency_table[i]/total*100) for i in scope])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
653
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
654 def pairer (self, overlap, minquery, maxquery, mintarget, maxtarget):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
655 queryhash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
656 targethash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
657 query_range = range (int(minquery), int(maxquery)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
658 target_range = range (int(mintarget), int(maxtarget)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
659 paired_sequences = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
660 for offset in self.readDict: # selection of data
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
661 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
662 if size in query_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
663 queryhash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
664 if size in target_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
665 targethash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
666 for offset in queryhash:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
667 if offset >= 0: matched_offset = -offset - overlap + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
668 else: matched_offset = -offset - overlap + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
669 if targethash[matched_offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
670 paired = min ( len(queryhash[offset]), len(targethash[matched_offset]) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
671 if offset >= 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
672 for i in range (paired):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
673 paired_sequences.append("+%s" % RNAtranslate ( self.sequence[offset:offset+queryhash[offset][i]]) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
674 paired_sequences.append("-%s" % RNAtranslate (antipara (self.sequence[-matched_offset-targethash[matched_offset][i]+1:-matched_offset+1]) ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
675 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
676 for i in range (paired):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
677 paired_sequences.append("-%s" % RNAtranslate (antipara (self.sequence[-offset-queryhash[offset][i]+1:-offset+1]) ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
678 paired_sequences.append("+%s" % RNAtranslate (self.sequence[matched_offset:matched_offset+targethash[matched_offset][i]] ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
679 return paired_sequences
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
680
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
681 def pairable (self, overlap, minquery, maxquery, mintarget, maxtarget):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
682 queryhash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
683 targethash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
684 query_range = range (int(minquery), int(maxquery)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
685 target_range = range (int(mintarget), int(maxtarget)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
686 paired_sequences = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
687
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
688 for offset in self.readDict: # selection of data
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
689 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
690 if size in query_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
691 queryhash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
692 if size in target_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
693 targethash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
694
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
695 for offset in queryhash:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
696 matched_offset = -offset - overlap + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
697 if targethash[matched_offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
698 if offset >= 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
699 for i in queryhash[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
700 paired_sequences.append("+%s" % RNAtranslate (self.sequence[offset:offset+i]) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
701 for i in targethash[matched_offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
702 paired_sequences.append( "-%s" % RNAtranslate (antipara (self.sequence[-matched_offset-i+1:-matched_offset+1]) ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
703 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
704 for i in queryhash[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
705 paired_sequences.append("-%s" % RNAtranslate (antipara (self.sequence[-offset-i+1:-offset+1]) ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
706 for i in targethash[matched_offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
707 paired_sequences.append("+%s" % RNAtranslate (self.sequence[matched_offset:matched_offset+i] ) )
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
708 return paired_sequences
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
709
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
710 def newpairable_bowtie (self, overlap, minquery, maxquery, mintarget, maxtarget):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
711 ''' revision of pairable on 3-12-2012, with focus on the offset shift problem (bowtie is 1-based cooordinates whereas python strings are 0-based coordinates'''
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
712 queryhash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
713 targethash = defaultdict(list)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
714 query_range = range (int(minquery), int(maxquery)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
715 target_range = range (int(mintarget), int(maxtarget)+1)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
716 bowtie_output = []
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
717
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
718 for offset in self.readDict: # selection of data
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
719 for size in self.readDict[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
720 if size in query_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
721 queryhash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
722 if size in target_range:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
723 targethash[offset].append(size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
724 counter = 0
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
725 for offset in queryhash:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
726 matched_offset = -offset - overlap + 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
727 if targethash[matched_offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
728 if offset >= 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
729 for i in queryhash[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
730 counter += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
731 bowtie_output.append("%s\t%s\t%s\t%s\t%s" % (counter, "+", self.gene, offset-1, self.sequence[offset-1:offset-1+i]) ) # attention a la base 1-0 de l'offset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
732 if offset < 0:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
733 for i in queryhash[offset]:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
734 counter += 1
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
735 bowtie_output.append("%s\t%s\t%s\t%s\t%s" % (counter, "-", self.gene, -offset-i, self.sequence[-offset-i:-offset])) # attention a la base 1-0 de l'offset
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
736 return bowtie_output
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
737
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
738
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
739 def __main__(bowtie_index_path, bowtie_output_path):
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
740 sequenceDic = get_fasta (bowtie_index_path)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
741 objDic = {}
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
742 F = open (bowtie_output_path, "r") # F is the bowtie output taken as input
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
743 for line in F:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
744 fields = line.split()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
745 polarity = fields[1]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
746 gene = fields[2]
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
747 offset = int(fields[3])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
748 size = len (fields[4])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
749 try:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
750 objDic[gene].addread (polarity, offset, size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
751 except KeyError:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
752 objDic[gene] = SmRNAwindow(gene, sequenceDic[gene])
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
753 objDic[gene].addread (polarity, offset, size)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
754 F.close()
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
755 for gene in objDic:
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
756 print gene, objDic[gene].pairer(19,19,23,19,23)
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
757
b996480cd604 planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff changeset
758 if __name__ == "__main__" : __main__(sys.argv[1], sys.argv[2])