annotate MergeFiles.py @ 5:b90313f7bbab draft

Uploaded
author bornea
date Wed, 03 Aug 2016 14:55:17 -0400
parents c1199500c601
children 008bc143e72e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
1 """
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
2 Python-code: Merge Scaffold Samples Report files
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
3 @author = Brent Kuenzi
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
4 @email = Brent.Kuenzi@moffitt.org
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
5 """
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
6 #######################################################################################
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
7 import sys
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
8 import urllib2
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
9 import os.path
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
10 import pandas as pd
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
11 #######################################################################################
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
12 ## Description: ##
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
13 # This program will merge either 2 or 3 scaffold
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
14 # sample report files together
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
15 ## Required input: ##
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
16 infile1 = sys.argv[1] # scaffold report #1 -- filename
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
17 infile2 = sys.argv[2] # scaffold report #2 -- filename
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
18 infile3 = sys.argv[3] # scaffold report #3 -- filename or "False"
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
19 baitfile = sys.argv[4] # Bait file -- filename
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
20 outfile = sys.argv[5] # output filename
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
21 class ScaffoldReturn(object):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
22 def __init__(self, getdata, getproteins, getheader):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
23 self.data = getdata
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
24 self.proteins = getproteins
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
25 self.header = getheader
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
26 class ProteinInfo(object):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
27 def __init__(self, getMW, getDescr):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
28 self.mw = getMW
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
29 self.descr = getDescr
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
30 def readtab(infile):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
31 with open(infile,'r') as x: # read in tab-delim text
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
32 output = []
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
33 for line in x:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
34 line = line.strip()
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
35 temp = line.split('\t')
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
36 output.append(temp)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
37 return output
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
38 def read_scaffold(scaffold_input): # Get data, proteins and header from scaffold output
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
39 dupes = readtab(scaffold_input)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
40 cnt = 0
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
41 for i in dupes:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
42 if "Accession Number" in i: # finds the start of header
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
43 header_start = cnt
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
44 break
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
45 cnt += 1
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
46 header = dupes[header_start]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
47 prot_start = header.index("Accession Number")
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
48 data = dupes[header_start+1:len(dupes)-2] # cut off blank line and END OF FILE
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
49 proteins = []
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
50 for i in data:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
51 i[prot_start] = i[prot_start].split()[0] # removes the (+##) that sometimes is attached
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
52 for protein in data:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
53 proteins.append(protein[prot_start])
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
54 return ScaffoldReturn(data, proteins, header)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
55 def MakeDF(scaffold_input,bait_input):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
56 bait = readtab(bait_input)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
57 data = read_scaffold(scaffold_input).data
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
58 header = read_scaffold(scaffold_input).header
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
59 proteins = read_scaffold(scaffold_input).proteins
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
60 prot_start = header.index("Accession Number")
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
61 bait_index = []
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
62 ind = []
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
63 for i in bait:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
64 if i[0] in header:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
65 bait_index.append(header.index(i[0])) # Find just the baits defined in bait file
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
66 ind.append(i[0])
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
67 frames = {}
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
68 for i in proteins:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
69 protein = i.split()[0]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
70 if protein not in frames:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
71 frames[protein] = [] # create dictionary of proteins for each bait value
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
72 for i in data:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
73 temp = i[prot_start]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
74 protein = temp.split()[0]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
75 for j in bait_index: # create dataframe
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
76 frames[protein].append(i[j])
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
77 df = pd.DataFrame(frames,index=ind)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
78 return df
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
79 def get_info(input1,input2,input3):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
80 files = [input1,input2]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
81 molwt = {}
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
82 protdesc = {}
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
83 if input3 != "False":
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
84 files.append(input3)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
85 for i in files:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
86 data = read_scaffold(i).data
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
87 header = read_scaffold(i).header
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
88 prot_start = header.index("Accession Number") # find header
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
89 prot = 0
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
90 mw = 0
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
91 cnt=0
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
92 for j in header: # find info
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
93 if "Identified" in j:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
94 prot=cnt
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
95 if "Molecular Weight" in j:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
96 mw=cnt
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
97 cnt+=1
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
98 for k in data: # append info
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
99 temp = k[prot_start]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
100 protein = temp.split()[0]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
101 if protein not in protdesc:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
102 protdesc[protein] = k[prot]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
103 molwt[protein] = k[mw]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
104 mw = pd.DataFrame(molwt,index=["Molecular Weight"])
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
105 descr = pd.DataFrame(protdesc,index=["Identified Proteins"])
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
106 return ProteinInfo(mw,descr) # return info as dataframe
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
107 def MakeFile(input1,input2,input3,bait_input):
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
108 files = [input1,input2]
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
109 if input3 != "False":
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
110 files.append(input3)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
111 DFs = []
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
112 DFs.append(get_info(input1,input2,input3).descr)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
113 DFs.append(get_info(input1,input2,input3).mw)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
114 for i in files:
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
115 DFs.append(MakeDF(i,bait_input))
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
116 final_df = pd.concat(DFs)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
117 temp = final_df.T
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
118 temp.index.name = "Accession Number"
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
119 output = temp.fillna(0)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
120 output["Accession Number"] = output.index.get_values()
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
121 output.index = range(1, len(output.index) + 1)
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
122 output.index.name = "#"
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
123 output.to_csv(outfile,sep="\t")
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
124
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
125 #######################################################################################
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
126 MakeFile(input1 = infile1,input2= infile2,input3 = infile3,bait_input=baitfile, outfile="merged.txt")
84564ae1cca1 Uploaded
bornea
parents:
diff changeset
127 #######################################################################################