view cravat_submit.py @ 0:7ebdd4ac13a2 draft

Uploaded
author rsajulga
date Tue, 10 Apr 2018 15:53:55 -0400
parents
children 676c8be98be4
line wrap: on
line source

import requests
import json
import time
import urllib
import sys
import csv
import re

try:
    input_filename = sys.argv[1]
    input_select_bar = sys.argv[2]
    GRCh_build = sys.argv[3]
    psm_filename = sys.argv[4]
    output_filename = sys.argv[5]
    file_3 = sys.argv[6]
    file_4 = sys.argv[7]
    file_5 = sys.argv[8]
except:
    input_filename = '1.) Galaxy2-[Human_Vcf_MCF7]-minimum.vcf'
    input_filename = 'input/[tgriffin_cguerrer_20160726_MCF7_RNAseq_01_S13_R1_001.vcf].vcf'
    input_select_bar = 'VEST'
    GRCh_build = 'GRCh38'
    output_filename = 'combined_variants.tsv'
    psm_filename = 'input/[ERLIC_MCF7_110kb_R123-CustomProDB_RNA-Seq_cRAP_DB.psm-report].tabular'
    file_3 = 'output/' + time.strftime("%H:%M") + '_Z_Gene_Level_Analysis.tsv'
    file_4 = 'output/' + time.strftime("%H:%M") + '_Z_Variant_Non-coding.Result.tsv'
    file_5 = 'output/' + time.strftime("%H:%M") + '_Z_Input_Errors.Result.tsv'


#in_file = open('input_call.txt', "r")    
#out_file = open('output_call.txt', "w")

write_header = True

GRCh37hg19 = 'off'
if GRCh_build == 'GRCh37':
    GRCh37hg19 = 'on'

# http://staging.cravat.us/CRAVAT/rest/service/submit:

#plugs in params to given URL
submit = requests.post('http://staging.cravat.us/CRAVAT/rest/service/submit', files={'inputfile':open(input_filename)}, data={'email':'znylund@insilico.us.com', 'analyses': input_select_bar, 'hg19': GRCh37hg19})

#Makes the data a json dictionary, takes out only the job ID
jobid = json.loads(submit.text)['jobid']
#out_file.write(jobid)    
submitted = json.loads(submit.text)['status']
#out_file.write('\t' + submitted)

input_file = open(input_filename)
is_comment_line = re.compile(".*#+.*")


#loops until we find a status equal to Success, then breaks
while True:
    check = requests.get('http://staging.cravat.us/CRAVAT/rest/service/status', params={'jobid': jobid})
    status = json.loads(check.text)['status']
    resultfileurl = json.loads(check.text)['resultfileurl']
    #out_file.write(str(status) + ', ')
    if status == 'Success':
        #out_file.write('\t' + resultfileurl)
        break
    else:
        time.sleep(2)

#out_file.write('\n')

#creates three files
file_1 = time.strftime("%H:%M") + '_Z_Variant_Result.tsv'
file_2 = time.strftime("%H:%M") + '_Z_Additional_Details.tsv'
#file_3 = time.strftime("%H:%M") + 'Combined_Variant_Results.tsv'

#Download the two results
urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant.Result.tsv", file_1)
urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Additional_Details.Result.tsv", file_2)
urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Gene_Level_Analysis.Result.tsv", file_3)
urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Non-coding.Result.tsv", file_4)
urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Input_Errors.Result.tsv", file_5)

headers = []
duplicates = []

#opens the Variant Result file and the Variant Additional Details file as csv readers, then opens the output file (galaxy) as a writer
with open(file_1) as tsvin_1, open(file_2) as tsvin_2, open(output_filename, 'wb') as tsvout:
    tsvreader_1 = csv.reader(tsvin_1, delimiter='\t')
    tsvreader_2 = csv.reader(tsvin_2, delimiter='\t')
    
    tsvout = csv.writer(tsvout, delimiter='\t')

    # Processes the PSM report
    if (psm_filename != 'None'):
        tsvin_3 = open(psm_filename)
        psmreader = csv.reader(tsvin_3, delimiter='\t')
        
        psmreader.next()
        peptide_map = {}
        s = re.compile('[A-Z][0-9]+[A-Z]')
        for row in psmreader:
            pro_name = row[1]
            pep_seq = row[2]
            
            prot_seq_changes = s.findall(pro_name)
        
            for change in prot_seq_changes:
                if change in peptide_map:
                    if pep_seq not in peptide_map[change].split(';'):
                        peptide_map[change] = peptide_map[change] + ';' + pep_seq
                else:
                    peptide_map[change] = pep_seq
    
    #loops through each row in the Variant Additional Details file

    print 'Checkpoint 3'
    for row in tsvreader_2:
        #sets row_2 equal to the same row in Variant Result file
        row_2 = tsvreader_1.next()
        #checks if row is empty or if the first term contains '#'
        if row == [] or row[0][0] == '#':
            tsvout.writerow(row)
        else:
        #checks if the row begins with input line
            if row[0] == 'Input line':
                #Goes through each value in the headers list in VAD
                #print 'Original row'
                #print row
                #print row_2
                for value in row:   
                    #Adds each value into headers 
                    headers.append(value)
                #Loops through the Keys in VR
                for value in row_2:
                    #Checks if the value is already in headers
                    if value in headers:
                        continue
                    #else adds the header to headers
                    else:
                        headers.append(value)
                if (psm_filename != 'None'):
                    headers.insert(1, 'Peptide')
                #print headers
                tsvout.writerow(headers)
            else:
                
                cells = []
                #Inserts a peptide column into the row
                if (psm_filename != 'None'):
                    if row[12] in peptide_map:
                        row.insert(1, peptide_map[row[12]])
                    else:
                        row.insert(1, '')
                    
                #Goes through each value in the next list
                for i,value in enumerate(row):
                    #adds it to cells
                    cells.append(value)
                #Goes through each value from the VR file after position 11 (After it is done repeating from VAD file)
                for i,value in enumerate(row_2[11:]):
                    #adds in the rest of the values to cells

                    # Skips the 2nd VEST p-value
                    if (i != 49 - 11):
                        cells.append(value)
                    
                print  cells
                tsvout.writerow(cells)



    
    
    

            
            
            
            

            
            
            
            
  
    
            
        
         
        
        
            
            
            
       
            
    

#a = 'col1\tcol2\tcol3'
#header_list = a.split('\t')

#loop through the two results, when you first hit header you print out the headers in tabular form
#Print out each header only once
#Combine both headers into one output file
#loop through the rest of the data and assign each value to its assigned header
#combine this all into one output file