annotate WeightedAverage.py @ 1:68a40b074399 draft default tip

Uploaded corrected tool requirements definition.
author devteam
date Thu, 03 Apr 2014 09:34:00 -0400
parents e11314245c2a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
2 """
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
3 usage: %prog bed_file_1 bed_file_2 out_file
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
4 -1, --cols1=N,N,N,N: Columns for chr, start, end, strand in first file
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
5 -2, --cols2=N,N,N,N,N: Columns for chr, start, end, strand, name/value in second file
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
6 """
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
7
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
8 import collections
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
9 import sys
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
10 from galaxy.tools.util.galaxyops import *
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
11 from bx.cookbook import doc_optparse
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
12
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
13
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
14 #export PYTHONPATH=~/galaxy/lib/
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
15 #running command python WeightedAverage.py interval_interpolate.bed value_interpolate.bed interpolate_result.bed
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
16
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
17 def stop_err(msg):
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
18 sys.stderr.write(msg)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
19 sys.exit()
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
20
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
21
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
22 def FindRate(chromosome, start_stop, dictType):
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
23 OverlapList = []
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
24 for tempO in dictType[chromosome]:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
25 DatabaseInterval = [tempO[0], tempO[1]]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
26 Overlap = GetOverlap( start_stop, DatabaseInterval )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
27 if Overlap > 0:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
28 OverlapList.append([Overlap, tempO[2]])
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
29
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
30 if len(OverlapList) > 0:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
31 SumRecomb = 0
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
32 SumOverlap = 0
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
33 for member in OverlapList:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
34 SumRecomb += member[0]*member[1]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
35 SumOverlap += member[0]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
36 averageRate = SumRecomb/SumOverlap
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
37 return averageRate
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
38 else:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
39 return 'NA'
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
40
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
41
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
42 def GetOverlap(a, b):
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
43 return min(a[1], b[1])-max(a[0], b[0])
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
44
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
45
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
46 options, args = doc_optparse.parse( __doc__ )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
47
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
48 try:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
49 chr_col_1, start_col_1, end_col_1, strand_col1 = parse_cols_arg( options.cols1 )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
50 chr_col_2, start_col_2, end_col_2, strand_col2, name_col_2 = parse_cols_arg( options.cols2 )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
51 input1, input2, input3 = args
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
52 except Exception, eee:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
53 print eee
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
54 stop_err( "Data issue: click the pencil icon in the history item to correct the metadata attributes." )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
55
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
56 fd2 = open(input2)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
57 lines2 = fd2.readlines()
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
58 RecombChrDict = collections.defaultdict(list)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
59
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
60 skipped = 0
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
61 for line in lines2:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
62 temp = line.strip().split()
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
63 try:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
64 assert float(temp[int(name_col_2)])
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
65 except:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
66 skipped += 1
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
67 continue
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
68 tempIndex = [int(temp[int(start_col_2)]), int(temp[int(end_col_2)]), float(temp[int(name_col_2)])]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
69 RecombChrDict[temp[int(chr_col_2)]].append(tempIndex)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
70
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
71 print "Skipped %d features with invalid values" % (skipped)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
72
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
73 fd1 = open(input1)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
74 lines = fd1.readlines()
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
75 finalProduct = ''
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
76 for line in lines:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
77 temp = line.strip().split('\t')
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
78 chromosome = temp[int(chr_col_1)]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
79 start = int(temp[int(start_col_1)])
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
80 stop = int(temp[int(end_col_1)])
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
81 start_stop = [start, stop]
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
82 RecombRate = FindRate( chromosome, start_stop, RecombChrDict )
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
83 try:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
84 RecombRate = "%.4f" % (float(RecombRate))
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
85 except:
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
86 RecombRate = RecombRate
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
87 finalProduct += line.strip()+'\t'+str(RecombRate)+'\n'
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
88 fdd = open(input3, 'w')
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
89 fdd.writelines(finalProduct)
e11314245c2a Imported from capsule None
devteam
parents:
diff changeset
90 fdd.close()