Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error
#1
Hello.
This is quite a complex code that I have made.
it is not working however, and I would like to see what I have done wrong.
the following is the code.

the error is shown below the code. Thank you.

import csv
import json
import re
import datetime
import logging
from collections import deque

sam_fname = 'filename'
#sam_fname = 'filename'
w_fname   = 'filname'
l_fname   = 'filename_log.log'
o_w_fname = open(w_fname,'w') #output file

def decipher_cigar(cigar_read):
	#file_writer.write (('Alignment Code : ' + data[0] + ' CIGAR Value : ' + cigar + '   Binary Equivalant : ' + (bin(int(data[1]))[2:]))+'\n')
	#Evaluate the alternative splicing events based on
	#CIGAR value and categerorize them as below
	# Skipped EXON
	# Alternate Donor
	# Alternate Acceptor
	# Retained Intron
	# Other
	#  	Mutually exclusive exons
	#	Skipped Introns : Pattern ...___M___N___M_#___...
	got_intron = 'N'
	cigar = cigar_read[5]
	match = re.findall(r'([0-9]+)([MIDNSHPX=])', cigar)	
	#print (match)
	logging.info (str(datetime.datetime.now()) +': match: '+ str(match) )
	#print(len(match))
	logging.info (str(datetime.datetime.now()) +': match: '+ str(len(match)) )
	if len(match) >= 3:
		#print('Alignment may be an INTRON. Go to next step...')
		logging.info (str(datetime.datetime.now()) +' Alignment may be an INTRON. Go to next step...' )
		bgnPos = cigar_read[3]  	#data[3] - the start position from sam file read. 
		for idx_cigar, cigar_value in enumerate(match, start=0):
                        #logging.info (str(datetime.datetime.now()) +' cigar flag: ' )
			#print('cigar flag: '+ cigar_value[1])
			bgnPos = int(bgnPos) + int(cigar_value[0])
			print('Calculated start pos: '+ str(bgnPos))
			if cigar_value[1] == 'M':
				#check to see if this is the first M match
				if idx_cigar == 0:
					print('This is the first cigar segment...')
				else:
					print('Not the first cigar segment...')	
					if ((save_match[1] == 'I' or save_match[1] == 'N') and first_i_flag == 'N'):
						print('Identified an intron ...')
						StartPos = int(bgnPos) - int(cigar_value[0]) - int(intronLength)
						EndPos = int(StartPos) + int(intronLength)
						ChroName = cigar_read[0]+'_'+cigar_read[1]+'_'+cigar_read[5]
						#Output format:
						#	    'ChromosomeName,Intron,AligStartPos,ChromosomeStartPos,'+
						#		'ChromosomeEndPos,IntronLength,FlagBinValue,Name'+'\n')
						o_w_fname.write(cigar_read[2]+','+'Y,'+ cigar_read[3]+','+str(StartPos)+
								','+ str(EndPos) + ','+
								str(intronLength)+','+decipher_flag(cigar_read[1])+','+ChroName+','+str(cigar_read)+'\n')

					else:
							print('Not an inton...')						
					
			if cigar_value[1] == 'I' or cigar_value[1] == 'N':
				#check to see if this is the first I match
				first_i_flag = 'N'
				if idx_cigar == 0:
					print('This is the first cigar segment...')
					first_i_flag = 'Y'
				else:
					print('Not the first cigar segment...check the prev segment')
					if save_match[1] == 'M':
						print('High possibility of intron...')
						intronLength = cigar_value[0]
					else:
						print('Cannot be an intron....')	
					
			save_match = match[idx_cigar]
			print('Save match: ') 
			print(save_match)
	else:
		print('Alignment NOT an INTRON....')						


def decipher_flag(flag='116'):

	print (' FLAG Value : ' + flag + 
		'   Binary Equivalant : ' + (bin(int(flag))[2:]))
	a = str(bin(int(flag))[2:])
	#print (a)
	a1 = a[::-1]  #reverse the binary code
	#print (a1)
	#bin_value = bin(int(data[1]))[2:]
	bin_value = a1
	print(bin_value)
	print ('Decoding flag.....' )
	for idx, num in enumerate(bin_value):
		print (idx, num)
		if (num == '1'):
			"print ('true')"
			with open('config.json') as jason_config_file:
				data = json.load(jason_config_file)
				print('  true  :' + data['SAM Flag'][str(idx)])
		else:
			print ('false')
			with open('config.json') as jason_config_file:
				data = json.load(jason_config_file)
			print('  false  :' + data['SAM Flag'][str(idx)])
	
	return bin_value

def main():
	
	logging.basicConfig(filename=l_fname,level=logging.INFO)
	logging.info (str(datetime.datetime.now()) +': File Processing Started : ' )
	
	o_w_fname.write('ChromosomeName,Intron,AligStartPos,ChromosomeStartPos,'+
		'ChromosomeEndPos,IntronLength,FlagBinValue,Name,SAMInputRecord'+'\n')

	with open(sam_fname,'r') as file:
		reader = csv.reader(file, delimiter="\t")
		d = list(reader)
		for data in d:
			if ( data[0].find('@') == -1 ):
				#print (data)
				logging.info (str(datetime.datetime.now()) +': Start Record Process : ' + 
					'>>Alignment Code : ' + str(data[0]) + 
					' CIGAR Value : ' + str(data[5])+
					' FLAG Value : ' + data[1])

				#function call to decipher the CIGAR value
				decipher_cigar(data)

				logging.info (str(datetime.datetime.now()) +': End Record Process : ' + 
					'>>Alignment Code : ' + str(data[0]) + 
					' CIGAR Value : ' + str(data[5])+
					' FLAG Value : ' + data[1])

		o_w_fname.close() #close file

		logging.info (str(datetime.datetime.now()) +': File Processing Ended : ' )


main()











resulting output file

Calculated start pos: 32009566
This is the first cigar segment...
Save match: 
('1', 'I')
Calculated start pos: 32009666
Not the first cigar segment...
Not an inton...
Save match: 
('100', 'M')
Calculated start pos: 32010216
Not the first cigar segment...check the prev segment
High possibility of intron...
Save match: 
('550', 'I')
Calculated start pos: 32010246
Not the first cigar segment...
Identified an intron ...
 FLAG Value : 147   Binary Equivalant : 10010011
11001001
Decoding flag.....
(0, '1')

Traceback (most recent call last):
  File "C:/Users/hrithikjha/Desktop/UTD/lets see if this works.py", line 142, in <module>
    main()
  File "C:/Users/hrithikjha/Desktop/UTD/lets see if this works.py", line 130, in main
    decipher_cigar(data)
  File "C:/Users/hrithikjha/Desktop/UTD/lets see if this works.py", line 57, in decipher_cigar
    str(intronLength)+','+decipher_flag(cigar_read[1])+','+ChroName+','+str(cigar_read)+'\n')
  File "C:/Users/hrithikjha/Desktop/UTD/lets see if this works.py", line 99, in decipher_flag
    with open('config.json') as jason_config_file:
IOError: [Errno 2] No such file or directory: 'config.json'
Reply
#2
You're trying to open a file that doesn't exist. Since you are using a relative path to the file, it needs to exist in the current working directory when you run the python file. Usually this is the same directory as the python file, but that's not always the case.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
oh that explains the case, because i made this on another desktop. Thank you ichabod801
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020