annotate MergeFiles.py @ 0:84564ae1cca1 draft

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